rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 1 | /* |
uwe | b25f1ea | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 2 | * This file is part of the flashrom project. |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 3 | * |
uwe | 555dd97 | 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 | 4475e90 | 2009-05-19 14:14:21 +0000 | [diff] [blame] | 6 | * Copyright (C) 2005-2008 coresystems GmbH |
hailfinger | 2306011 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 7 | * Copyright (C) 2008,2009 Carl-Daniel Hailfinger |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 8 | * |
uwe | b25f1ea | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 13 | * |
uwe | b25f1ea | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 18 | * |
uwe | b25f1ea | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
hailfinger | a83a5fe | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
stepan | 1da96c0 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
oxygene | 5027589 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 26 | #ifndef __LIBPAYLOAD__ |
| 27 | #include <fcntl.h> |
stepan | 1da96c0 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 28 | #include <sys/stat.h> |
oxygene | 5027589 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 29 | #endif |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 30 | #include <string.h> |
| 31 | #include <stdlib.h> |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 32 | #include <ctype.h> |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 33 | #include <getopt.h> |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 34 | #if HAVE_UTSNAME == 1 |
| 35 | #include <sys/utsname.h> |
| 36 | #endif |
Vincent Palatin | 7ab2393 | 2014-10-01 12:09:16 -0700 | [diff] [blame] | 37 | #include <unistd.h> |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 38 | #include "flash.h" |
hailfinger | 66966da | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 39 | #include "flashchips.h" |
Simon Glass | 9ad06c1 | 2013-07-03 22:08:17 +0900 | [diff] [blame] | 40 | #include "layout.h" |
hailfinger | 428f685 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 41 | #include "programmer.h" |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 42 | |
krause | 2eb7621 | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 43 | const char flashrom_version[] = FLASHROM_VERSION; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 44 | char *chip_to_probe = NULL; |
stuge | 98c09aa | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 45 | int verbose = 0; |
hailfinger | 80422e2 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 46 | |
David Hendricks | 5477739 | 2015-01-11 18:55:14 -0800 | [diff] [blame] | 47 | unsigned int required_erase_size = 0; /* see comment in flash.h */ |
| 48 | |
David Hendricks | 9ba79fb | 2015-04-03 12:06:16 -0700 | [diff] [blame] | 49 | /* Set if any erase/write operation is to be done. This will be used to |
| 50 | * decide if final verification is needed. */ |
| 51 | static int content_has_changed = 0; |
| 52 | |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 53 | /* error handling stuff */ |
| 54 | enum error_action access_denied_action = error_ignore; |
| 55 | |
| 56 | int ignore_error(int err) { |
| 57 | int rc = 0; |
| 58 | |
| 59 | switch(err) { |
| 60 | case ACCESS_DENIED: |
| 61 | if (access_denied_action == error_ignore) |
| 62 | rc = 1; |
| 63 | break; |
| 64 | default: |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | return rc; |
| 69 | } |
| 70 | |
hailfinger | 969e2f3 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 71 | static enum programmer programmer = PROGRAMMER_INVALID; |
hailfinger | 80422e2 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 72 | |
hailfinger | ddeb4ac | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 73 | static char *programmer_param = NULL; |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 74 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 75 | /* Supported buses for the current programmer. */ |
| 76 | enum chipbustype buses_supported; |
hailfinger | 80422e2 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 77 | |
uwe | e15beb9 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 78 | /* |
hailfinger | 80422e2 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 79 | * Programmers supporting multiple buses can have differing size limits on |
| 80 | * each bus. Store the limits for each bus in a common struct. |
| 81 | */ |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 82 | struct decode_sizes max_rom_decode; |
| 83 | |
| 84 | /* If nonzero, used as the start address of bottom-aligned flash. */ |
| 85 | unsigned long flashbase; |
hailfinger | 80422e2 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 86 | |
hailfinger | 5828baf | 2010-07-03 12:14:25 +0000 | [diff] [blame] | 87 | /* Is writing allowed with this programmer? */ |
| 88 | int programmer_may_write; |
| 89 | |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 90 | const struct programmer_entry programmer_table[] = { |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 91 | #if CONFIG_INTERNAL == 1 |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 92 | { |
hailfinger | 3548a9a | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 93 | .name = "internal", |
hailfinger | 6c69ab0 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 94 | .init = internal_init, |
hailfinger | 11ae3c4 | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 95 | .map_flash_region = physmap, |
| 96 | .unmap_flash_region = physunmap, |
hailfinger | e5829f6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 97 | .delay = internal_delay, |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 98 | }, |
hailfinger | 80422e2 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 99 | #endif |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 100 | |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 101 | #if CONFIG_DUMMY == 1 |
hailfinger | a9df33c | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 102 | { |
hailfinger | 3548a9a | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 103 | .name = "dummy", |
hailfinger | 6c69ab0 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 104 | .init = dummy_init, |
hailfinger | 11ae3c4 | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 105 | .map_flash_region = dummy_map, |
| 106 | .unmap_flash_region = dummy_unmap, |
hailfinger | e5829f6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 107 | .delay = internal_delay, |
hailfinger | a9df33c | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 108 | }, |
hailfinger | 571a6b3 | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 109 | #endif |
hailfinger | a9df33c | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 110 | |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 111 | #if CONFIG_NIC3COM == 1 |
uwe | 0f5a3a2 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 112 | { |
hailfinger | 3548a9a | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 113 | .name = "nic3com", |
uwe | 0f5a3a2 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 114 | .init = nic3com_init, |
uwe | 3e656bd | 2009-05-17 23:12:17 +0000 | [diff] [blame] | 115 | .map_flash_region = fallback_map, |
| 116 | .unmap_flash_region = fallback_unmap, |
hailfinger | e5829f6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 117 | .delay = internal_delay, |
uwe | 0f5a3a2 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 118 | }, |
hailfinger | 571a6b3 | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 119 | #endif |
uwe | 0f5a3a2 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 120 | |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 121 | #if CONFIG_NICREALTEK == 1 |
hailfinger | 5aa3698 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 122 | { |
hailfinger | 0d703d4 | 2011-03-07 01:08:09 +0000 | [diff] [blame] | 123 | /* This programmer works for Realtek RTL8139 and SMC 1211. */ |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 124 | .name = "nicrealtek", |
| 125 | //.name = "nicsmc1211", |
| 126 | .init = nicrealtek_init, |
| 127 | .map_flash_region = fallback_map, |
| 128 | .unmap_flash_region = fallback_unmap, |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 129 | .delay = internal_delay, |
hailfinger | 5aa3698 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 130 | }, |
hailfinger | 5aa3698 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 131 | #endif |
| 132 | |
hailfinger | f0a368f | 2010-06-07 22:37:54 +0000 | [diff] [blame] | 133 | #if CONFIG_NICNATSEMI == 1 |
| 134 | { |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 135 | .name = "nicnatsemi", |
| 136 | .init = nicnatsemi_init, |
| 137 | .map_flash_region = fallback_map, |
| 138 | .unmap_flash_region = fallback_unmap, |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 139 | .delay = internal_delay, |
hailfinger | f0a368f | 2010-06-07 22:37:54 +0000 | [diff] [blame] | 140 | }, |
| 141 | #endif |
hailfinger | 5aa3698 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 142 | |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 143 | #if CONFIG_GFXNVIDIA == 1 |
uwe | ff4576d | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 144 | { |
| 145 | .name = "gfxnvidia", |
| 146 | .init = gfxnvidia_init, |
uwe | ff4576d | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 147 | .map_flash_region = fallback_map, |
| 148 | .unmap_flash_region = fallback_unmap, |
uwe | ff4576d | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 149 | .delay = internal_delay, |
| 150 | }, |
| 151 | #endif |
| 152 | |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 153 | #if CONFIG_DRKAISER == 1 |
ruik | da922a1 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 154 | { |
uwe | e2f95ef | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 155 | .name = "drkaiser", |
| 156 | .init = drkaiser_init, |
uwe | e2f95ef | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 157 | .map_flash_region = fallback_map, |
| 158 | .unmap_flash_region = fallback_unmap, |
uwe | e2f95ef | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 159 | .delay = internal_delay, |
| 160 | }, |
hailfinger | 571a6b3 | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 161 | #endif |
uwe | e2f95ef | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 162 | |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 163 | #if CONFIG_SATASII == 1 |
uwe | e2f95ef | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 164 | { |
hailfinger | 3548a9a | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 165 | .name = "satasii", |
ruik | da922a1 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 166 | .init = satasii_init, |
uwe | 3e656bd | 2009-05-17 23:12:17 +0000 | [diff] [blame] | 167 | .map_flash_region = fallback_map, |
| 168 | .unmap_flash_region = fallback_unmap, |
hailfinger | e5829f6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 169 | .delay = internal_delay, |
ruik | da922a1 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 170 | }, |
hailfinger | 571a6b3 | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 171 | #endif |
ruik | da922a1 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 172 | |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 173 | #if CONFIG_ATAHPT == 1 |
uwe | 7e627c8 | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 174 | { |
| 175 | .name = "atahpt", |
| 176 | .init = atahpt_init, |
uwe | 7e627c8 | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 177 | .map_flash_region = fallback_map, |
| 178 | .unmap_flash_region = fallback_unmap, |
uwe | 7e627c8 | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 179 | .delay = internal_delay, |
| 180 | }, |
| 181 | #endif |
| 182 | |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 183 | #if CONFIG_FT2232_SPI == 1 |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 184 | { |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 185 | .name = "ft2232_spi", |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 186 | .init = ft2232_spi_init, |
hailfinger | 6fe23d6 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 187 | .map_flash_region = fallback_map, |
| 188 | .unmap_flash_region = fallback_unmap, |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 189 | .delay = internal_delay, |
| 190 | }, |
hailfinger | d9dcfbd | 2009-08-19 13:27:58 +0000 | [diff] [blame] | 191 | #endif |
hailfinger | 6fe23d6 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 192 | |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 193 | #if CONFIG_SERPROG == 1 |
hailfinger | 37b4fbf | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 194 | { |
hailfinger | 3548a9a | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 195 | .name = "serprog", |
hailfinger | 37b4fbf | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 196 | .init = serprog_init, |
hailfinger | 37b4fbf | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 197 | .map_flash_region = fallback_map, |
| 198 | .unmap_flash_region = fallback_unmap, |
hailfinger | 37b4fbf | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 199 | .delay = serprog_delay, |
| 200 | }, |
hailfinger | 74d88a7 | 2009-08-12 16:17:41 +0000 | [diff] [blame] | 201 | #endif |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 202 | |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 203 | #if CONFIG_BUSPIRATE_SPI == 1 |
hailfinger | 9c5add7 | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 204 | { |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 205 | .name = "buspirate_spi", |
hailfinger | 9c5add7 | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 206 | .init = buspirate_spi_init, |
hailfinger | 9c5add7 | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 207 | .map_flash_region = fallback_map, |
| 208 | .unmap_flash_region = fallback_unmap, |
hailfinger | 9c5add7 | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 209 | .delay = internal_delay, |
| 210 | }, |
| 211 | #endif |
| 212 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 213 | #if CONFIG_RAIDEN_DEBUG_SPI == 1 |
| 214 | { |
| 215 | .name = "raiden_debug_spi", |
| 216 | .init = raiden_debug_spi_init, |
| 217 | .map_flash_region = fallback_map, |
| 218 | .unmap_flash_region = fallback_unmap, |
| 219 | .delay = internal_delay, |
| 220 | }, |
| 221 | #endif |
| 222 | |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 223 | #if CONFIG_DEDIPROG == 1 |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 224 | { |
| 225 | .name = "dediprog", |
| 226 | .init = dediprog_init, |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 227 | .map_flash_region = fallback_map, |
| 228 | .unmap_flash_region = fallback_unmap, |
hailfinger | dfb32a0 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 229 | .delay = internal_delay, |
| 230 | }, |
| 231 | #endif |
| 232 | |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 233 | #if CONFIG_RAYER_SPI == 1 |
| 234 | { |
| 235 | .name = "rayer_spi", |
| 236 | .init = rayer_spi_init, |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 237 | .map_flash_region = fallback_map, |
| 238 | .unmap_flash_region = fallback_unmap, |
hailfinger | 52c4fa0 | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 239 | .delay = internal_delay, |
| 240 | }, |
| 241 | #endif |
| 242 | |
hailfinger | 7949b65 | 2011-05-08 00:24:18 +0000 | [diff] [blame] | 243 | #if CONFIG_NICINTEL == 1 |
| 244 | { |
| 245 | .name = "nicintel", |
| 246 | .init = nicintel_init, |
hailfinger | 7949b65 | 2011-05-08 00:24:18 +0000 | [diff] [blame] | 247 | .map_flash_region = fallback_map, |
| 248 | .unmap_flash_region = fallback_unmap, |
hailfinger | 7949b65 | 2011-05-08 00:24:18 +0000 | [diff] [blame] | 249 | .delay = internal_delay, |
| 250 | }, |
| 251 | #endif |
| 252 | |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 253 | #if CONFIG_NICINTEL_SPI == 1 |
| 254 | { |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 255 | .name = "nicintel_spi", |
| 256 | .init = nicintel_spi_init, |
| 257 | .map_flash_region = fallback_map, |
| 258 | .unmap_flash_region = fallback_unmap, |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 259 | .delay = internal_delay, |
uwe | 6764e92 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 260 | }, |
| 261 | #endif |
| 262 | |
hailfinger | fb1f31f | 2010-12-03 14:48:11 +0000 | [diff] [blame] | 263 | #if CONFIG_OGP_SPI == 1 |
| 264 | { |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 265 | .name = "ogp_spi", |
| 266 | .init = ogp_spi_init, |
| 267 | .map_flash_region = fallback_map, |
| 268 | .unmap_flash_region = fallback_unmap, |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 269 | .delay = internal_delay, |
hailfinger | fb1f31f | 2010-12-03 14:48:11 +0000 | [diff] [blame] | 270 | }, |
| 271 | #endif |
| 272 | |
hailfinger | 935365d | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 273 | #if CONFIG_SATAMV == 1 |
| 274 | { |
| 275 | .name = "satamv", |
| 276 | .init = satamv_init, |
hailfinger | 935365d | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 277 | .map_flash_region = fallback_map, |
| 278 | .unmap_flash_region = fallback_unmap, |
hailfinger | 935365d | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 279 | .delay = internal_delay, |
| 280 | }, |
| 281 | #endif |
| 282 | |
David Hendricks | cebee89 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 283 | #if CONFIG_LINUX_MTD == 1 |
| 284 | { |
| 285 | .name = "linux_mtd", |
| 286 | .init = linux_mtd_init, |
| 287 | .map_flash_region = fallback_map, |
| 288 | .unmap_flash_region = fallback_unmap, |
| 289 | .delay = internal_delay, |
| 290 | }, |
| 291 | #endif |
| 292 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 293 | #if CONFIG_LINUX_SPI == 1 |
| 294 | { |
| 295 | .name = "linux_spi", |
| 296 | .init = linux_spi_init, |
| 297 | .map_flash_region = fallback_map, |
| 298 | .unmap_flash_region = fallback_unmap, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 299 | .delay = internal_delay, |
| 300 | }, |
| 301 | #endif |
| 302 | |
hailfinger | 3548a9a | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 303 | {}, /* This entry corresponds to PROGRAMMER_INVALID. */ |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 304 | }; |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 305 | |
David Hendricks | bf36f09 | 2010-11-02 23:39:29 -0700 | [diff] [blame] | 306 | #define CHIP_RESTORE_MAXFN 4 |
| 307 | static int chip_restore_fn_count = 0; |
| 308 | struct chip_restore_func_data { |
| 309 | CHIP_RESTORE_CALLBACK; |
| 310 | struct flashchip *flash; |
| 311 | uint8_t status; |
| 312 | } static chip_restore_fn[CHIP_RESTORE_MAXFN]; |
| 313 | |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 314 | |
hailfinger | f31cbdc | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 315 | #define SHUTDOWN_MAXFN 32 |
hailfinger | dc6f797 | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 316 | static int shutdown_fn_count = 0; |
| 317 | struct shutdown_func_data { |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 318 | int (*func) (void *data); |
hailfinger | dc6f797 | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 319 | void *data; |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 320 | } static shutdown_fn[SHUTDOWN_MAXFN]; |
| 321 | /* Initialize to 0 to make sure nobody registers a shutdown function before |
| 322 | * programmer init. |
| 323 | */ |
| 324 | static int may_register_shutdown = 0; |
hailfinger | dc6f797 | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 325 | |
stefanct | 569dbb6 | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 326 | static int check_block_eraser(const struct flashchip *flash, int k, int log); |
| 327 | |
hailfinger | dc6f797 | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 328 | /* Register a function to be executed on programmer shutdown. |
| 329 | * The advantage over atexit() is that you can supply a void pointer which will |
| 330 | * be used as parameter to the registered function upon programmer shutdown. |
| 331 | * This pointer can point to arbitrary data used by said function, e.g. undo |
| 332 | * information for GPIO settings etc. If unneeded, set data=NULL. |
| 333 | * Please note that the first (void *data) belongs to the function signature of |
| 334 | * the function passed as first parameter. |
| 335 | */ |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 336 | int register_shutdown(int (*function) (void *data), void *data) |
hailfinger | dc6f797 | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 337 | { |
| 338 | if (shutdown_fn_count >= SHUTDOWN_MAXFN) { |
hailfinger | 63932d4 | 2010-06-04 23:20:21 +0000 | [diff] [blame] | 339 | msg_perr("Tried to register more than %i shutdown functions.\n", |
hailfinger | dc6f797 | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 340 | SHUTDOWN_MAXFN); |
| 341 | return 1; |
| 342 | } |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 343 | if (!may_register_shutdown) { |
| 344 | msg_perr("Tried to register a shutdown function before " |
| 345 | "programmer init.\n"); |
| 346 | return 1; |
| 347 | } |
hailfinger | dc6f797 | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 348 | shutdown_fn[shutdown_fn_count].func = function; |
| 349 | shutdown_fn[shutdown_fn_count].data = data; |
| 350 | shutdown_fn_count++; |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
David Hendricks | bf36f09 | 2010-11-02 23:39:29 -0700 | [diff] [blame] | 355 | //int register_chip_restore(int (*function) (void *data), void *data) |
| 356 | int register_chip_restore(CHIP_RESTORE_CALLBACK, |
| 357 | struct flashchip *flash, uint8_t status) |
| 358 | { |
| 359 | if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) { |
| 360 | msg_perr("Tried to register more than %i chip restore" |
| 361 | " functions.\n", CHIP_RESTORE_MAXFN); |
| 362 | return 1; |
| 363 | } |
| 364 | chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */ |
| 365 | chip_restore_fn[chip_restore_fn_count].flash = flash; |
| 366 | chip_restore_fn[chip_restore_fn_count].status = status; |
| 367 | chip_restore_fn_count++; |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
hailfinger | 969e2f3 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 372 | int programmer_init(enum programmer prog, char *param) |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 373 | { |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 374 | int ret; |
hailfinger | 969e2f3 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 375 | |
| 376 | if (prog >= PROGRAMMER_INVALID) { |
| 377 | msg_perr("Invalid programmer specified!\n"); |
| 378 | return -1; |
| 379 | } |
| 380 | programmer = prog; |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 381 | /* Initialize all programmer specific data. */ |
| 382 | /* Default to unlimited decode sizes. */ |
| 383 | max_rom_decode = (const struct decode_sizes) { |
| 384 | .parallel = 0xffffffff, |
| 385 | .lpc = 0xffffffff, |
| 386 | .fwh = 0xffffffff, |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 387 | .spi = 0xffffffff, |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 388 | }; |
hailfinger | e1e41ea | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 389 | buses_supported = BUS_NONE; |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 390 | /* Default to top aligned flash at 4 GB. */ |
| 391 | flashbase = 0; |
| 392 | /* Registering shutdown functions is now allowed. */ |
| 393 | may_register_shutdown = 1; |
hailfinger | 5828baf | 2010-07-03 12:14:25 +0000 | [diff] [blame] | 394 | /* Default to allowing writes. Broken programmers set this to 0. */ |
| 395 | programmer_may_write = 1; |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 396 | |
| 397 | programmer_param = param; |
| 398 | msg_pdbg("Initializing %s programmer\n", |
| 399 | programmer_table[programmer].name); |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 400 | ret = programmer_table[programmer].init(); |
| 401 | if (programmer_param && strlen(programmer_param)) { |
| 402 | msg_perr("Unhandled programmer parameters: %s\n", |
| 403 | programmer_param); |
| 404 | /* Do not error out here, the init itself was successful. */ |
| 405 | } |
| 406 | return ret; |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 407 | } |
| 408 | |
David Hendricks | bf36f09 | 2010-11-02 23:39:29 -0700 | [diff] [blame] | 409 | int chip_restore() |
| 410 | { |
| 411 | int rc = 0; |
| 412 | |
| 413 | while (chip_restore_fn_count > 0) { |
| 414 | int i = --chip_restore_fn_count; |
| 415 | rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash, |
| 416 | chip_restore_fn[i].status); |
| 417 | } |
| 418 | |
| 419 | return rc; |
| 420 | } |
| 421 | |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 422 | int programmer_shutdown(void) |
| 423 | { |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 424 | int ret = 0; |
| 425 | |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 426 | /* Registering shutdown functions is no longer allowed. */ |
| 427 | may_register_shutdown = 0; |
| 428 | while (shutdown_fn_count > 0) { |
| 429 | int i = --shutdown_fn_count; |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 430 | ret |= shutdown_fn[i].func(shutdown_fn[i].data); |
hailfinger | 1ff33dc | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 431 | } |
dhendrix | 0ffc2eb | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 432 | return ret; |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void *programmer_map_flash_region(const char *descr, unsigned long phys_addr, |
| 436 | size_t len) |
| 437 | { |
| 438 | return programmer_table[programmer].map_flash_region(descr, |
| 439 | phys_addr, len); |
| 440 | } |
| 441 | |
| 442 | void programmer_unmap_flash_region(void *virt_addr, size_t len) |
| 443 | { |
| 444 | programmer_table[programmer].unmap_flash_region(virt_addr, len); |
| 445 | } |
| 446 | |
| 447 | void chip_writeb(uint8_t val, chipaddr addr) |
| 448 | { |
hailfinger | 76bb7e9 | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 449 | par_programmer->chip_writeb(val, addr); |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void chip_writew(uint16_t val, chipaddr addr) |
| 453 | { |
hailfinger | 76bb7e9 | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 454 | par_programmer->chip_writew(val, addr); |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | void chip_writel(uint32_t val, chipaddr addr) |
| 458 | { |
hailfinger | 76bb7e9 | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 459 | par_programmer->chip_writel(val, addr); |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 460 | } |
| 461 | |
hailfinger | 9d987ef | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 462 | void chip_writen(uint8_t *buf, chipaddr addr, size_t len) |
| 463 | { |
hailfinger | 76bb7e9 | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 464 | par_programmer->chip_writen(buf, addr, len); |
hailfinger | 9d987ef | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 465 | } |
| 466 | |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 467 | uint8_t chip_readb(const chipaddr addr) |
| 468 | { |
hailfinger | 76bb7e9 | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 469 | return par_programmer->chip_readb(addr); |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | uint16_t chip_readw(const chipaddr addr) |
| 473 | { |
hailfinger | 76bb7e9 | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 474 | return par_programmer->chip_readw(addr); |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | uint32_t chip_readl(const chipaddr addr) |
| 478 | { |
hailfinger | 76bb7e9 | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 479 | return par_programmer->chip_readl(addr); |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 480 | } |
| 481 | |
hailfinger | 9d987ef | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 482 | void chip_readn(uint8_t *buf, chipaddr addr, size_t len) |
| 483 | { |
hailfinger | 76bb7e9 | 2011-11-09 23:40:00 +0000 | [diff] [blame] | 484 | par_programmer->chip_readn(buf, addr, len); |
hailfinger | 9d987ef | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 485 | } |
| 486 | |
hailfinger | e5829f6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 487 | void programmer_delay(int usecs) |
| 488 | { |
| 489 | programmer_table[programmer].delay(usecs); |
| 490 | } |
| 491 | |
stuge | 5ff0e6c | 2009-01-26 00:39:57 +0000 | [diff] [blame] | 492 | void map_flash_registers(struct flashchip *flash) |
stepan | 15e64bc | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 493 | { |
stepan | 15e64bc | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 494 | size_t size = flash->total_size * 1024; |
hailfinger | 8577ad1 | 2009-05-11 14:01:17 +0000 | [diff] [blame] | 495 | /* Flash registers live 4 MByte below the flash. */ |
hailfinger | 0459e1c | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 496 | /* FIXME: This is incorrect for nonstandard flashbase. */ |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 497 | flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size); |
stepan | 15e64bc | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 498 | } |
| 499 | |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 500 | int read_memmapped(struct flashchip *flash, uint8_t *buf, unsigned int start, int unsigned len) |
hailfinger | 2306011 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 501 | { |
hailfinger | 0f08b7a | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 502 | chip_readn(buf, flash->virtual_memory + start, len); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 503 | |
hailfinger | 2306011 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | |
hailfinger | 7b41474 | 2009-06-13 12:04:03 +0000 | [diff] [blame] | 507 | int min(int a, int b) |
| 508 | { |
| 509 | return (a < b) ? a : b; |
| 510 | } |
| 511 | |
hailfinger | 7af8369 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 512 | int max(int a, int b) |
| 513 | { |
| 514 | return (a > b) ? a : b; |
| 515 | } |
| 516 | |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 517 | int bitcount(unsigned long a) |
| 518 | { |
| 519 | int i = 0; |
| 520 | for (; a != 0; a >>= 1) |
| 521 | if (a & 1) |
| 522 | i++; |
| 523 | return i; |
| 524 | } |
| 525 | |
hailfinger | f76cc32 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 526 | void tolower_string(char *str) |
| 527 | { |
| 528 | for (; *str != '\0'; str++) |
| 529 | *str = (char)tolower((unsigned char)*str); |
| 530 | } |
| 531 | |
hailfinger | a916b42 | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 532 | char *strcat_realloc(char *dest, const char *src) |
| 533 | { |
| 534 | dest = realloc(dest, strlen(dest) + strlen(src) + 1); |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 535 | if (!dest) { |
| 536 | msg_gerr("Out of memory!\n"); |
hailfinger | a916b42 | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 537 | return NULL; |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 538 | } |
hailfinger | a916b42 | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 539 | strcat(dest, src); |
| 540 | return dest; |
| 541 | } |
| 542 | |
hailfinger | 6e5a52a | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 543 | /* This is a somewhat hacked function similar in some ways to strtok(). |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 544 | * It will look for needle with a subsequent '=' in haystack, return a copy of |
| 545 | * needle and remove everything from the first occurrence of needle to the next |
| 546 | * delimiter from haystack. |
hailfinger | 6e5a52a | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 547 | */ |
stefanct | 5270028 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 548 | char *extract_param(char **haystack, const char *needle, const char *delim) |
hailfinger | 6e5a52a | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 549 | { |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 550 | char *param_pos, *opt_pos, *rest; |
| 551 | char *opt = NULL; |
| 552 | int optlen; |
hailfinger | f4aaccc | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 553 | int needlelen; |
hailfinger | 6e5a52a | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 554 | |
hailfinger | f4aaccc | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 555 | needlelen = strlen(needle); |
| 556 | if (!needlelen) { |
| 557 | msg_gerr("%s: empty needle! Please report a bug at " |
| 558 | "flashrom@flashrom.org\n", __func__); |
| 559 | return NULL; |
| 560 | } |
| 561 | /* No programmer parameters given. */ |
| 562 | if (*haystack == NULL) |
| 563 | return NULL; |
hailfinger | 6e5a52a | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 564 | param_pos = strstr(*haystack, needle); |
| 565 | do { |
| 566 | if (!param_pos) |
| 567 | return NULL; |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 568 | /* Needle followed by '='? */ |
| 569 | if (param_pos[needlelen] == '=') { |
Simon Glass | 8dc8273 | 2013-07-16 10:13:51 -0600 | [diff] [blame] | 570 | |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 571 | /* Beginning of the string? */ |
| 572 | if (param_pos == *haystack) |
| 573 | break; |
| 574 | /* After a delimiter? */ |
| 575 | if (strchr(delim, *(param_pos - 1))) |
| 576 | break; |
| 577 | } |
hailfinger | 6e5a52a | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 578 | /* Continue searching. */ |
| 579 | param_pos++; |
| 580 | param_pos = strstr(param_pos, needle); |
| 581 | } while (1); |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 582 | |
hailfinger | 6e5a52a | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 583 | if (param_pos) { |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 584 | /* Get the string after needle and '='. */ |
| 585 | opt_pos = param_pos + needlelen + 1; |
| 586 | optlen = strcspn(opt_pos, delim); |
| 587 | /* Return an empty string if the parameter was empty. */ |
| 588 | opt = malloc(optlen + 1); |
| 589 | if (!opt) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 590 | msg_gerr("Out of memory!\n"); |
hailfinger | 6e5a52a | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 591 | exit(1); |
| 592 | } |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 593 | strncpy(opt, opt_pos, optlen); |
| 594 | opt[optlen] = '\0'; |
| 595 | rest = opt_pos + optlen; |
| 596 | /* Skip all delimiters after the current parameter. */ |
| 597 | rest += strspn(rest, delim); |
| 598 | memmove(param_pos, rest, strlen(rest) + 1); |
| 599 | /* We could shrink haystack, but the effort is not worth it. */ |
hailfinger | 6e5a52a | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 600 | } |
hailfinger | 6e5a52a | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 601 | |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 602 | return opt; |
hailfinger | 6e5a52a | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 603 | } |
| 604 | |
stefanct | 5270028 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 605 | char *extract_programmer_param(const char *param_name) |
hailfinger | ddeb4ac | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 606 | { |
| 607 | return extract_param(&programmer_param, param_name, ","); |
| 608 | } |
| 609 | |
stefanct | e1c5acf | 2011-07-04 07:27:17 +0000 | [diff] [blame] | 610 | /* Returns the number of well-defined erasers for a chip. */ |
| 611 | static unsigned int count_usable_erasers(const struct flashchip *flash) |
stefanct | 569dbb6 | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 612 | { |
| 613 | unsigned int usable_erasefunctions = 0; |
| 614 | int k; |
| 615 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
| 616 | if (!check_block_eraser(flash, k, 0)) |
| 617 | usable_erasefunctions++; |
| 618 | } |
| 619 | return usable_erasefunctions; |
| 620 | } |
| 621 | |
hailfinger | 7af8369 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 622 | /* start is an offset to the base address of the flash chip */ |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 623 | int check_erased_range(struct flashchip *flash, unsigned int start, unsigned int len) |
hailfinger | 7af8369 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 624 | { |
| 625 | int ret; |
| 626 | uint8_t *cmpbuf = malloc(len); |
| 627 | |
| 628 | if (!cmpbuf) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 629 | msg_gerr("Could not allocate memory!\n"); |
hailfinger | 7af8369 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 630 | exit(1); |
| 631 | } |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 632 | memset(cmpbuf, flash_erase_value(flash), len); |
hailfinger | 7af8369 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 633 | ret = verify_range(flash, cmpbuf, start, len, "ERASE"); |
| 634 | free(cmpbuf); |
| 635 | return ret; |
| 636 | } |
| 637 | |
uwe | e15beb9 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 638 | /* |
hailfinger | 7af3d19 | 2009-11-25 17:05:52 +0000 | [diff] [blame] | 639 | * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 640 | * flash content at location start |
hailfinger | 7af8369 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 641 | * @start offset to the base address of the flash chip |
| 642 | * @len length of the verified area |
| 643 | * @message string to print in the "FAILED" message |
| 644 | * @return 0 for success, -1 for failure |
| 645 | */ |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 646 | int verify_range(struct flashchip *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len, |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 647 | const char *message) |
hailfinger | 7af8369 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 648 | { |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 649 | unsigned int i; |
hailfinger | 8cb6ece | 2010-11-16 17:21:58 +0000 | [diff] [blame] | 650 | uint8_t *readbuf = malloc(len); |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 651 | int ret = 0, failcount = 0; |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 652 | unsigned int chunksize; |
hailfinger | 7af8369 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 653 | |
| 654 | if (!len) |
| 655 | goto out_free; |
| 656 | |
hailfinger | b0f4d12 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 657 | if (!flash->read) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 658 | msg_cerr("ERROR: flashrom has no read function for this flash chip.\n"); |
hailfinger | b0f4d12 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 659 | return 1; |
| 660 | } |
hailfinger | 7af8369 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 661 | if (!readbuf) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 662 | msg_gerr("Could not allocate memory!\n"); |
hailfinger | 7af8369 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 663 | exit(1); |
| 664 | } |
| 665 | |
| 666 | if (start + len > flash->total_size * 1024) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 667 | msg_gerr("Error: %s called with start 0x%x + len 0x%x >" |
hailfinger | 7af8369 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 668 | " total_size 0x%x\n", __func__, start, len, |
| 669 | flash->total_size * 1024); |
| 670 | ret = -1; |
| 671 | goto out_free; |
| 672 | } |
| 673 | if (!message) |
| 674 | message = "VERIFY"; |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 675 | |
David Hendricks | 758e2fb | 2014-08-19 20:34:43 -0700 | [diff] [blame] | 676 | for (i = 0, chunksize = 0; i < len; i += chunksize) { |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 677 | int tmp, j; |
| 678 | |
David Hendricks | 758e2fb | 2014-08-19 20:34:43 -0700 | [diff] [blame] | 679 | chunksize = min(flash->page_size, len - i); |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 680 | tmp = flash->read(flash, readbuf + i, start + i, chunksize); |
| 681 | |
David Hendricks | e345194 | 2013-03-21 17:23:29 -0700 | [diff] [blame] | 682 | /* Since this function explicitly compares the bytes, we need |
| 683 | to handle errors manually */ |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 684 | if (tmp) { |
| 685 | ret = tmp; |
David Hendricks | 758e2fb | 2014-08-19 20:34:43 -0700 | [diff] [blame] | 686 | if (ignore_error(tmp)) |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 687 | continue; |
David Hendricks | 758e2fb | 2014-08-19 20:34:43 -0700 | [diff] [blame] | 688 | else |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 689 | goto out_free; |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | for (j = 0; j < chunksize; j++) { |
| 693 | if (cmpbuf[i + j] != readbuf[i + j]) { |
| 694 | /* Only print the first failure. */ |
| 695 | if (!failcount++) |
| 696 | msg_cerr("%s FAILED at 0x%08x! " |
| 697 | "Expected=0x%02x, Read=0x%02x,", |
| 698 | message, start + i + j, cmpbuf[i + j], |
David Hendricks | 595bd0f | 2015-12-02 12:20:03 -0800 | [diff] [blame^] | 699 | readbuf[i + j]); |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 700 | } |
| 701 | } |
hailfinger | 8cb6ece | 2010-11-16 17:21:58 +0000 | [diff] [blame] | 702 | } |
| 703 | |
hailfinger | 5be6c0f | 2009-07-23 01:42:56 +0000 | [diff] [blame] | 704 | if (failcount) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 705 | msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n", |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 706 | start, start + len - 1, failcount); |
hailfinger | 5be6c0f | 2009-07-23 01:42:56 +0000 | [diff] [blame] | 707 | ret = -1; |
| 708 | } |
hailfinger | 7af8369 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 709 | |
| 710 | out_free: |
| 711 | free(readbuf); |
| 712 | return ret; |
| 713 | } |
| 714 | |
uwe | e15beb9 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 715 | /* |
hailfinger | b247c7a | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 716 | * Check if the buffer @have can be programmed to the content of @want without |
| 717 | * erasing. This is only possible if all chunks of size @gran are either kept |
| 718 | * as-is or changed from an all-ones state to any other state. |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 719 | * |
hailfinger | b247c7a | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 720 | * The following write granularities (enum @gran) are known: |
| 721 | * - 1 bit. Each bit can be cleared individually. |
| 722 | * - 1 byte. A byte can be written once. Further writes to an already written |
| 723 | * byte cause the contents to be either undefined or to stay unchanged. |
| 724 | * - 128 bytes. If less than 128 bytes are written, the rest will be |
| 725 | * erased. Each write to a 128-byte region will trigger an automatic erase |
| 726 | * before anything is written. Very uncommon behaviour and unsupported by |
| 727 | * this function. |
| 728 | * - 256 bytes. If less than 256 bytes are written, the contents of the |
| 729 | * unwritten bytes are undefined. |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 730 | * Warning: This function assumes that @have and @want point to naturally |
| 731 | * aligned regions. |
hailfinger | b247c7a | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 732 | * |
| 733 | * @have buffer with current content |
| 734 | * @want buffer with desired content |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 735 | * @len length of the checked area |
hailfinger | b247c7a | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 736 | * @gran write granularity (enum, not count) |
| 737 | * @return 0 if no erase is needed, 1 otherwise |
| 738 | */ |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 739 | static int need_erase(struct flashchip *flash, uint8_t *have, uint8_t *want, |
| 740 | unsigned int len, enum write_granularity gran) |
hailfinger | b247c7a | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 741 | { |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 742 | int result = 0; |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 743 | unsigned int i, j, limit; |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 744 | int erase_value = flash_erase_value(flash); |
hailfinger | b247c7a | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 745 | |
| 746 | switch (gran) { |
| 747 | case write_gran_1bit: |
| 748 | for (i = 0; i < len; i++) |
| 749 | if ((have[i] & want[i]) != want[i]) { |
| 750 | result = 1; |
| 751 | break; |
| 752 | } |
| 753 | break; |
| 754 | case write_gran_1byte: |
| 755 | for (i = 0; i < len; i++) |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 756 | if ((have[i] != want[i]) && (have[i] != erase_value)) { |
hailfinger | b247c7a | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 757 | result = 1; |
| 758 | break; |
| 759 | } |
| 760 | break; |
| 761 | case write_gran_256bytes: |
| 762 | for (j = 0; j < len / 256; j++) { |
| 763 | limit = min (256, len - j * 256); |
uwe | f6f94d4 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 764 | /* Are 'have' and 'want' identical? */ |
hailfinger | b247c7a | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 765 | if (!memcmp(have + j * 256, want + j * 256, limit)) |
| 766 | continue; |
| 767 | /* have needs to be in erased state. */ |
| 768 | for (i = 0; i < limit; i++) |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 769 | if (have[j * 256 + i] != erase_value) { |
hailfinger | b247c7a | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 770 | result = 1; |
| 771 | break; |
| 772 | } |
| 773 | if (result) |
| 774 | break; |
| 775 | } |
| 776 | break; |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 777 | default: |
| 778 | msg_cerr("%s: Unsupported granularity! Please report a bug at " |
| 779 | "flashrom@flashrom.org\n", __func__); |
hailfinger | b247c7a | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 780 | } |
| 781 | return result; |
| 782 | } |
| 783 | |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 784 | /** |
| 785 | * Check if the buffer @have needs to be programmed to get the content of @want. |
| 786 | * If yes, return 1 and fill in first_start with the start address of the |
| 787 | * write operation and first_len with the length of the first to-be-written |
| 788 | * chunk. If not, return 0 and leave first_start and first_len undefined. |
| 789 | * |
| 790 | * Warning: This function assumes that @have and @want point to naturally |
| 791 | * aligned regions. |
| 792 | * |
| 793 | * @have buffer with current content |
| 794 | * @want buffer with desired content |
| 795 | * @len length of the checked area |
| 796 | * @gran write granularity (enum, not count) |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 797 | * @first_start offset of the first byte which needs to be written (passed in |
| 798 | * value is increased by the offset of the first needed write |
| 799 | * relative to have/want or unchanged if no write is needed) |
| 800 | * @return length of the first contiguous area which needs to be written |
| 801 | * 0 if no write is needed |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 802 | * |
| 803 | * FIXME: This function needs a parameter which tells it about coalescing |
| 804 | * in relation to the max write length of the programmer and the max write |
| 805 | * length of the chip. |
| 806 | */ |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 807 | static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len, |
| 808 | unsigned int *first_start, |
| 809 | enum write_granularity gran) |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 810 | { |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 811 | int need_write = 0; |
| 812 | unsigned int rel_start = 0, first_len = 0; |
| 813 | unsigned int i, limit, stride; |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 814 | |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 815 | switch (gran) { |
| 816 | case write_gran_1bit: |
| 817 | case write_gran_1byte: |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 818 | stride = 1; |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 819 | break; |
| 820 | case write_gran_256bytes: |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 821 | stride = 256; |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 822 | break; |
| 823 | default: |
| 824 | msg_cerr("%s: Unsupported granularity! Please report a bug at " |
| 825 | "flashrom@flashrom.org\n", __func__); |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 826 | /* Claim that no write was needed. A write with unknown |
| 827 | * granularity is too dangerous to try. |
| 828 | */ |
| 829 | return 0; |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 830 | } |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 831 | for (i = 0; i < len / stride; i++) { |
| 832 | limit = min(stride, len - i * stride); |
| 833 | /* Are 'have' and 'want' identical? */ |
| 834 | if (memcmp(have + i * stride, want + i * stride, limit)) { |
| 835 | if (!need_write) { |
| 836 | /* First location where have and want differ. */ |
| 837 | need_write = 1; |
| 838 | rel_start = i * stride; |
| 839 | } |
| 840 | } else { |
| 841 | if (need_write) { |
| 842 | /* First location where have and want |
| 843 | * do not differ anymore. |
| 844 | */ |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 845 | break; |
| 846 | } |
| 847 | } |
| 848 | } |
hailfinger | ffb7f38 | 2010-12-06 13:05:44 +0000 | [diff] [blame] | 849 | if (need_write) |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 850 | first_len = min(i * stride - rel_start, len); |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 851 | *first_start += rel_start; |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 852 | return first_len; |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 853 | } |
| 854 | |
hailfinger | 0c51535 | 2009-11-23 12:55:31 +0000 | [diff] [blame] | 855 | /* This function generates various test patterns useful for testing controller |
| 856 | * and chip communication as well as chip behaviour. |
| 857 | * |
| 858 | * If a byte can be written multiple times, each time keeping 0-bits at 0 |
| 859 | * and changing 1-bits to 0 if the new value for that bit is 0, the effect |
| 860 | * is essentially an AND operation. That's also the reason why this function |
| 861 | * provides the result of AND between various patterns. |
| 862 | * |
| 863 | * Below is a list of patterns (and their block length). |
| 864 | * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes) |
| 865 | * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes) |
| 866 | * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes) |
| 867 | * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes) |
| 868 | * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes) |
| 869 | * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes) |
| 870 | * Pattern 6 is 00 (1 Byte) |
| 871 | * Pattern 7 is ff (1 Byte) |
| 872 | * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256 |
| 873 | * byte block. |
| 874 | * |
| 875 | * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B) |
| 876 | * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B) |
| 877 | * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter) |
| 878 | * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards) |
| 879 | * Pattern 12 is 00 (1 Byte) |
| 880 | * Pattern 13 is ff (1 Byte) |
| 881 | * Patterns 8-13 have no block number. |
| 882 | * |
| 883 | * Patterns 0-3 are created to detect and efficiently diagnose communication |
| 884 | * slips like missed bits or bytes and their repetitive nature gives good visual |
| 885 | * cues to the person inspecting the results. In addition, the following holds: |
| 886 | * AND Pattern 0/1 == Pattern 4 |
| 887 | * AND Pattern 2/3 == Pattern 5 |
| 888 | * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6 |
| 889 | * A weakness of pattern 0-5 is the inability to detect swaps/copies between |
| 890 | * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc. |
| 891 | * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes. |
| 892 | * 0x5 and 0xa were picked because they are 0101 and 1010 binary. |
| 893 | * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes. |
| 894 | * Besides that, they provide for bit testing of the last two bytes of every |
| 895 | * 256 byte block which contains the block number for patterns 0-6. |
| 896 | * Patterns 10-11 are special purpose for detecting subblock aliasing with |
| 897 | * block sizes >256 bytes (some Dataflash chips etc.) |
| 898 | * AND Pattern 8/9 == Pattern 12 |
| 899 | * AND Pattern 10/11 == Pattern 12 |
| 900 | * Pattern 13 is the completely erased state. |
| 901 | * None of the patterns can detect aliasing at boundaries which are a multiple |
| 902 | * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI). |
| 903 | */ |
| 904 | int generate_testpattern(uint8_t *buf, uint32_t size, int variant) |
| 905 | { |
| 906 | int i; |
| 907 | |
| 908 | if (!buf) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 909 | msg_gerr("Invalid buffer!\n"); |
hailfinger | 0c51535 | 2009-11-23 12:55:31 +0000 | [diff] [blame] | 910 | return 1; |
| 911 | } |
| 912 | |
| 913 | switch (variant) { |
| 914 | case 0: |
| 915 | for (i = 0; i < size; i++) |
| 916 | buf[i] = (i & 0xf) << 4 | 0x5; |
| 917 | break; |
| 918 | case 1: |
| 919 | for (i = 0; i < size; i++) |
| 920 | buf[i] = (i & 0xf) << 4 | 0xa; |
| 921 | break; |
| 922 | case 2: |
| 923 | for (i = 0; i < size; i++) |
| 924 | buf[i] = 0x50 | (i & 0xf); |
| 925 | break; |
| 926 | case 3: |
| 927 | for (i = 0; i < size; i++) |
| 928 | buf[i] = 0xa0 | (i & 0xf); |
| 929 | break; |
| 930 | case 4: |
| 931 | for (i = 0; i < size; i++) |
| 932 | buf[i] = (i & 0xf) << 4; |
| 933 | break; |
| 934 | case 5: |
| 935 | for (i = 0; i < size; i++) |
| 936 | buf[i] = i & 0xf; |
| 937 | break; |
| 938 | case 6: |
| 939 | memset(buf, 0x00, size); |
| 940 | break; |
| 941 | case 7: |
| 942 | memset(buf, 0xff, size); |
| 943 | break; |
| 944 | case 8: |
| 945 | for (i = 0; i < size; i++) |
| 946 | buf[i] = i & 0xff; |
| 947 | break; |
| 948 | case 9: |
| 949 | for (i = 0; i < size; i++) |
| 950 | buf[i] = ~(i & 0xff); |
| 951 | break; |
| 952 | case 10: |
| 953 | for (i = 0; i < size % 2; i++) { |
| 954 | buf[i * 2] = (i >> 8) & 0xff; |
| 955 | buf[i * 2 + 1] = i & 0xff; |
| 956 | } |
| 957 | if (size & 0x1) |
| 958 | buf[i * 2] = (i >> 8) & 0xff; |
| 959 | break; |
| 960 | case 11: |
| 961 | for (i = 0; i < size % 2; i++) { |
| 962 | buf[i * 2] = ~((i >> 8) & 0xff); |
| 963 | buf[i * 2 + 1] = ~(i & 0xff); |
| 964 | } |
| 965 | if (size & 0x1) |
| 966 | buf[i * 2] = ~((i >> 8) & 0xff); |
| 967 | break; |
| 968 | case 12: |
| 969 | memset(buf, 0x00, size); |
| 970 | break; |
| 971 | case 13: |
| 972 | memset(buf, 0xff, size); |
| 973 | break; |
| 974 | } |
| 975 | |
| 976 | if ((variant >= 0) && (variant <= 7)) { |
| 977 | /* Write block number in the last two bytes of each 256-byte |
| 978 | * block, big endian for easier reading of the hexdump. |
| 979 | * Note that this wraps around for chips larger than 2^24 bytes |
| 980 | * (16 MB). |
| 981 | */ |
| 982 | for (i = 0; i < size / 256; i++) { |
| 983 | buf[i * 256 + 254] = (i >> 8) & 0xff; |
| 984 | buf[i * 256 + 255] = i & 0xff; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | return 0; |
| 989 | } |
| 990 | |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 991 | int check_max_decode(enum chipbustype buses, uint32_t size) |
| 992 | { |
| 993 | int limitexceeded = 0; |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 994 | |
| 995 | if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) { |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 996 | limitexceeded++; |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 997 | msg_pdbg("Chip size %u kB is bigger than supported " |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 998 | "size %u kB of chipset/board/programmer " |
| 999 | "for %s interface, " |
| 1000 | "probe/read/erase/write may fail. ", size / 1024, |
| 1001 | max_rom_decode.parallel / 1024, "Parallel"); |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1002 | } |
hailfinger | e1e41ea | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 1003 | if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) { |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1004 | limitexceeded++; |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1005 | msg_pdbg("Chip size %u kB is bigger than supported " |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1006 | "size %u kB of chipset/board/programmer " |
| 1007 | "for %s interface, " |
| 1008 | "probe/read/erase/write may fail. ", size / 1024, |
| 1009 | max_rom_decode.lpc / 1024, "LPC"); |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1010 | } |
hailfinger | e1e41ea | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 1011 | if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) { |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1012 | limitexceeded++; |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1013 | msg_pdbg("Chip size %u kB is bigger than supported " |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1014 | "size %u kB of chipset/board/programmer " |
| 1015 | "for %s interface, " |
| 1016 | "probe/read/erase/write may fail. ", size / 1024, |
| 1017 | max_rom_decode.fwh / 1024, "FWH"); |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1018 | } |
hailfinger | e1e41ea | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 1019 | if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) { |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1020 | limitexceeded++; |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1021 | msg_pdbg("Chip size %u kB is bigger than supported " |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1022 | "size %u kB of chipset/board/programmer " |
| 1023 | "for %s interface, " |
| 1024 | "probe/read/erase/write may fail. ", size / 1024, |
| 1025 | max_rom_decode.spi / 1024, "SPI"); |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1026 | } |
| 1027 | if (!limitexceeded) |
| 1028 | return 0; |
| 1029 | /* Sometimes chip and programmer have more than one bus in common, |
| 1030 | * and the limit is not exceeded on all buses. Tell the user. |
| 1031 | */ |
| 1032 | if (bitcount(buses) > limitexceeded) |
hailfinger | 92cd8e3 | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 1033 | /* FIXME: This message is designed towards CLI users. */ |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1034 | msg_pdbg("There is at least one common chip/programmer " |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1035 | "interface which can support a chip of this size. " |
| 1036 | "You can try --force at your own risk.\n"); |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1037 | return 1; |
| 1038 | } |
| 1039 | |
hailfinger | 48ed3e2 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1040 | int probe_flash(int startchip, struct flashchip *fill_flash, int force) |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 1041 | { |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 1042 | const struct flashchip *flash, *flash_list; |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1043 | unsigned long base = 0; |
stepan | 3e7aeae | 2011-01-19 06:21:54 +0000 | [diff] [blame] | 1044 | char location[64]; |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1045 | uint32_t size; |
| 1046 | enum chipbustype buses_common; |
hailfinger | a916b42 | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1047 | char *tmp; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 1048 | |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 1049 | /* Based on the host controller interface that a platform |
| 1050 | * needs to use (hwseq or swseq), |
| 1051 | * set the flashchips list here. |
| 1052 | */ |
| 1053 | switch (ich_generation) { |
| 1054 | case CHIPSET_100_SERIES_SUNRISE_POINT: |
| 1055 | flash_list = flashchips_hwseq; |
| 1056 | break; |
| 1057 | default: |
| 1058 | flash_list = flashchips; |
| 1059 | break; |
| 1060 | } |
| 1061 | |
| 1062 | for (flash = flash_list + startchip; flash && flash->name; flash++) { |
stuge | c1e55fe | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1063 | if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1064 | continue; |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1065 | buses_common = buses_supported & flash->bustype; |
| 1066 | if (!buses_common) { |
hailfinger | 18bd4cc | 2011-06-17 22:38:53 +0000 | [diff] [blame] | 1067 | msg_gspew("Probing for %s %s, %d kB: skipped. ", |
| 1068 | flash->vendor, flash->name, flash->total_size); |
hailfinger | a916b42 | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1069 | tmp = flashbuses_to_text(buses_supported); |
hailfinger | 18bd4cc | 2011-06-17 22:38:53 +0000 | [diff] [blame] | 1070 | msg_gspew("Host bus type %s ", tmp); |
hailfinger | a916b42 | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1071 | free(tmp); |
| 1072 | tmp = flashbuses_to_text(flash->bustype); |
hailfinger | 327d252 | 2010-03-22 23:43:51 +0000 | [diff] [blame] | 1073 | msg_gspew("and chip bus type %s are incompatible.", |
| 1074 | tmp); |
hailfinger | a916b42 | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1075 | free(tmp); |
hailfinger | 18bd4cc | 2011-06-17 22:38:53 +0000 | [diff] [blame] | 1076 | msg_gspew("\n"); |
| 1077 | continue; |
| 1078 | } |
| 1079 | msg_gdbg("Probing for %s %s, %d kB: ", |
| 1080 | flash->vendor, flash->name, flash->total_size); |
| 1081 | if (!flash->probe && !force) { |
| 1082 | msg_gdbg("failed! flashrom has no probe function for " |
| 1083 | "this flash chip.\n"); |
hailfinger | a916b42 | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1084 | continue; |
| 1085 | } |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 1086 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1087 | size = flash->total_size * 1024; |
hailfinger | aec9c96 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1088 | check_max_decode(buses_common, size); |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 1089 | |
hailfinger | 48ed3e2 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1090 | /* Start filling in the dynamic data. */ |
| 1091 | *fill_flash = *flash; |
| 1092 | |
hailfinger | 72d3b98 | 2009-05-09 07:27:23 +0000 | [diff] [blame] | 1093 | base = flashbase ? flashbase : (0xffffffff - size + 1); |
hailfinger | 48ed3e2 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1094 | fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 1095 | |
stuge | c1e55fe | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1096 | if (force) |
| 1097 | break; |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 1098 | |
hailfinger | 48ed3e2 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1099 | if (fill_flash->probe(fill_flash) != 1) |
stuge | 56300c3 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 1100 | goto notfound; |
| 1101 | |
hailfinger | 48ed3e2 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1102 | /* If this is the first chip found, accept it. |
| 1103 | * If this is not the first chip found, accept it only if it is |
| 1104 | * a non-generic match. |
| 1105 | * We could either make chipcount global or provide it as |
| 1106 | * parameter, or we assume that startchip==0 means this call to |
| 1107 | * probe_flash() is the first one and thus no chip has been |
| 1108 | * found before. |
| 1109 | */ |
| 1110 | if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID) |
stuge | c1e55fe | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1111 | break; |
| 1112 | |
stuge | 56300c3 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 1113 | notfound: |
hailfinger | 48ed3e2 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1114 | programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 1115 | } |
uwe | be4477b | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 1116 | |
stuge | c1e55fe | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1117 | if (!flash || !flash->name) |
hailfinger | 48ed3e2 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1118 | return -1; |
stuge | c1e55fe | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1119 | |
hailfinger | e11396b | 2011-03-08 00:09:11 +0000 | [diff] [blame] | 1120 | #if CONFIG_INTERNAL == 1 |
| 1121 | if (programmer_table[programmer].map_flash_region == physmap) |
stepan | 3e7aeae | 2011-01-19 06:21:54 +0000 | [diff] [blame] | 1122 | snprintf(location, sizeof(location), "at physical address 0x%lx", base); |
hailfinger | e11396b | 2011-03-08 00:09:11 +0000 | [diff] [blame] | 1123 | else |
| 1124 | #endif |
stepan | 3e7aeae | 2011-01-19 06:21:54 +0000 | [diff] [blame] | 1125 | snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name); |
stepan | 3e7aeae | 2011-01-19 06:21:54 +0000 | [diff] [blame] | 1126 | |
stefanct | 588b6d2 | 2011-06-26 20:45:35 +0000 | [diff] [blame] | 1127 | tmp = flashbuses_to_text(flash->bustype); |
stefanct | dfd5883 | 2011-07-25 20:38:52 +0000 | [diff] [blame] | 1128 | msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n", |
| 1129 | force ? "Assuming" : "Found", fill_flash->vendor, |
| 1130 | fill_flash->name, fill_flash->total_size, tmp, location); |
stefanct | 588b6d2 | 2011-06-26 20:45:35 +0000 | [diff] [blame] | 1131 | free(tmp); |
uwe | 9e6811e | 2009-06-28 21:47:57 +0000 | [diff] [blame] | 1132 | |
hailfinger | 0f4c395 | 2010-12-02 21:59:42 +0000 | [diff] [blame] | 1133 | /* Flash registers will not be mapped if the chip was forced. Lock info |
| 1134 | * may be stored in registers, so avoid lock info printing. |
| 1135 | */ |
| 1136 | if (!force) |
hailfinger | 48ed3e2 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1137 | if (fill_flash->printlock) |
| 1138 | fill_flash->printlock(fill_flash); |
snelson | 1ee293c | 2010-02-19 00:52:10 +0000 | [diff] [blame] | 1139 | |
hailfinger | 48ed3e2 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1140 | /* Return position of matching chip. */ |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 1141 | return flash - flash_list; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Louis Yung-Chieh Lo | 5d95f04 | 2011-09-01 17:33:06 +0800 | [diff] [blame] | 1144 | int verify_flash(struct flashchip *flash, uint8_t *buf, int verify_it) |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 1145 | { |
hailfinger | b0f4d12 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 1146 | int ret; |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 1147 | unsigned int total_size = flash->total_size * 1024; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 1148 | |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1149 | msg_cinfo("Verifying flash... "); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1150 | |
Louis Yung-Chieh Lo | 5d95f04 | 2011-09-01 17:33:06 +0800 | [diff] [blame] | 1151 | if (specified_partition() && verify_it == VERIFY_PARTIAL) { |
| 1152 | ret = handle_partial_verify(flash, buf, verify_range); |
| 1153 | } else { |
| 1154 | ret = verify_range(flash, buf, 0, total_size, NULL); |
| 1155 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1156 | |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 1157 | if (ret == ACCESS_DENIED) { |
| 1158 | msg_gdbg("Could not fully verify due to access error, "); |
| 1159 | if (access_denied_action == error_ignore) { |
| 1160 | msg_gdbg("ignoring\n"); |
| 1161 | ret = 0; |
| 1162 | } else { |
| 1163 | msg_gdbg("aborting\n"); |
| 1164 | } |
| 1165 | } |
| 1166 | |
hailfinger | b0f4d12 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 1167 | if (!ret) |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1168 | msg_cinfo("VERIFIED. \n"); |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 1169 | |
hailfinger | b0f4d12 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 1170 | return ret; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1173 | int read_buf_from_file(unsigned char *buf, unsigned long size, |
| 1174 | const char *filename) |
hailfinger | 771fc18 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1175 | { |
| 1176 | unsigned long numbytes; |
| 1177 | FILE *image; |
| 1178 | struct stat image_stat; |
| 1179 | |
Vincent Palatin | 7ab2393 | 2014-10-01 12:09:16 -0700 | [diff] [blame] | 1180 | if (!strncmp(filename, "-", sizeof("-"))) |
| 1181 | image = fdopen(STDIN_FILENO, "rb"); |
| 1182 | else |
| 1183 | image = fopen(filename, "rb"); |
| 1184 | if (image == NULL) { |
hailfinger | 771fc18 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1185 | perror(filename); |
| 1186 | return 1; |
| 1187 | } |
| 1188 | if (fstat(fileno(image), &image_stat) != 0) { |
| 1189 | perror(filename); |
| 1190 | fclose(image); |
| 1191 | return 1; |
| 1192 | } |
Vincent Palatin | 7ab2393 | 2014-10-01 12:09:16 -0700 | [diff] [blame] | 1193 | if ((image_stat.st_size != size) && |
| 1194 | (strncmp(filename, "-", sizeof("-")))) { |
Gwendal Grignou | 94e87d6 | 2014-11-25 15:34:15 -0800 | [diff] [blame] | 1195 | msg_gerr("Error: Image size doesn't match: stat %ld bytes, " |
| 1196 | "wanted %ld!\n", image_stat.st_size, size); |
hailfinger | 771fc18 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1197 | fclose(image); |
| 1198 | return 1; |
| 1199 | } |
| 1200 | numbytes = fread(buf, 1, size, image); |
| 1201 | if (fclose(image)) { |
| 1202 | perror(filename); |
| 1203 | return 1; |
| 1204 | } |
| 1205 | if (numbytes != size) { |
| 1206 | msg_gerr("Error: Failed to read complete file. Got %ld bytes, " |
| 1207 | "wanted %ld!\n", numbytes, size); |
| 1208 | return 1; |
| 1209 | } |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1213 | int write_buf_to_file(unsigned char *buf, unsigned long size, |
| 1214 | const char *filename) |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1215 | { |
| 1216 | unsigned long numbytes; |
| 1217 | FILE *image; |
hailfinger | de34586 | 2009-06-01 22:07:52 +0000 | [diff] [blame] | 1218 | |
| 1219 | if (!filename) { |
hailfinger | 42a850a | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1220 | msg_gerr("No filename specified.\n"); |
hailfinger | de34586 | 2009-06-01 22:07:52 +0000 | [diff] [blame] | 1221 | return 1; |
| 1222 | } |
Vincent Palatin | 7ab2393 | 2014-10-01 12:09:16 -0700 | [diff] [blame] | 1223 | if (!strncmp(filename, "-", sizeof("-"))) |
| 1224 | image = fdopen(STDOUT_FILENO, "wb"); |
| 1225 | else |
| 1226 | image = fopen(filename, "wb"); |
| 1227 | if (image == NULL) { |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1228 | perror(filename); |
hailfinger | 2306011 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 1229 | return 1; |
hailfinger | 42a850a | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1230 | } |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1231 | |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1232 | numbytes = fwrite(buf, 1, size, image); |
| 1233 | fclose(image); |
hailfinger | 42a850a | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1234 | if (numbytes != size) { |
| 1235 | msg_gerr("File %s could not be written completely.\n", |
| 1236 | filename); |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1237 | return 1; |
hailfinger | 42a850a | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1238 | } |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1239 | return 0; |
| 1240 | } |
| 1241 | |
David Hendricks | e345194 | 2013-03-21 17:23:29 -0700 | [diff] [blame] | 1242 | /* |
| 1243 | * read_flash - wrapper for flash->read() with additional high-level policy |
| 1244 | * |
| 1245 | * @flash flash chip |
| 1246 | * @buf buffer to store data in |
| 1247 | * @start start address |
| 1248 | * @len number of bytes to read |
| 1249 | * |
| 1250 | * This wrapper simplifies most cases when the flash chip needs to be read |
| 1251 | * since policy decisions such as non-fatal error handling is centralized. |
| 1252 | */ |
| 1253 | int read_flash(struct flashchip *flash, uint8_t *buf, |
| 1254 | unsigned int start, unsigned int len) |
| 1255 | { |
David Hendricks | 4e76fdc | 2013-05-13 16:05:36 -0700 | [diff] [blame] | 1256 | int ret; |
David Hendricks | e345194 | 2013-03-21 17:23:29 -0700 | [diff] [blame] | 1257 | |
| 1258 | if (!flash || !flash->read) |
| 1259 | return -1; |
| 1260 | |
David Hendricks | 4e76fdc | 2013-05-13 16:05:36 -0700 | [diff] [blame] | 1261 | ret = flash->read(flash, buf, start, len); |
David Hendricks | e345194 | 2013-03-21 17:23:29 -0700 | [diff] [blame] | 1262 | if (ret) { |
| 1263 | if (ignore_error(ret)) { |
| 1264 | msg_gdbg("ignoring error when reading 0x%x-0x%x\n", |
| 1265 | start, start + len - 1); |
| 1266 | ret = 0; |
| 1267 | } else { |
| 1268 | msg_gdbg("failed to read 0x%x-0x%x\n", |
| 1269 | start, start + len - 1); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | return ret; |
| 1274 | } |
| 1275 | |
David Hendricks | 7c8a161 | 2013-04-26 19:14:44 -0700 | [diff] [blame] | 1276 | /* |
| 1277 | * write_flash - wrapper for flash->write() with additional high-level policy |
| 1278 | * |
| 1279 | * @flash flash chip |
| 1280 | * @buf buffer to write to flash |
| 1281 | * @start start address in flash |
| 1282 | * @len number of bytes to write |
| 1283 | * |
| 1284 | * TODO: Look up regions that are write-protected and avoid attempt to write |
| 1285 | * to them at all. |
| 1286 | */ |
| 1287 | int write_flash(struct flashchip *flash, uint8_t *buf, |
| 1288 | unsigned int start, unsigned int len) |
| 1289 | { |
| 1290 | if (!flash || !flash->write) |
| 1291 | return -1; |
| 1292 | |
| 1293 | return flash->write(flash, buf, start, len); |
| 1294 | } |
| 1295 | |
stefanct | 5270028 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 1296 | int read_flash_to_file(struct flashchip *flash, const char *filename) |
hailfinger | 42a850a | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1297 | { |
| 1298 | unsigned long size = flash->total_size * 1024; |
| 1299 | unsigned char *buf = calloc(size, sizeof(char)); |
| 1300 | int ret = 0; |
| 1301 | |
| 1302 | msg_cinfo("Reading flash... "); |
| 1303 | if (!buf) { |
| 1304 | msg_gerr("Memory allocation failed!\n"); |
| 1305 | msg_cinfo("FAILED.\n"); |
| 1306 | return 1; |
| 1307 | } |
Louis Yung-Chieh Lo | 9c7525f | 2011-03-04 12:32:02 +0800 | [diff] [blame] | 1308 | |
| 1309 | /* To support partial read, fill buffer to all 0xFF at beginning to make |
| 1310 | * debug easier. */ |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 1311 | memset(buf, flash_erase_value(flash), size); |
Louis Yung-Chieh Lo | 9c7525f | 2011-03-04 12:32:02 +0800 | [diff] [blame] | 1312 | |
hailfinger | 42a850a | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1313 | if (!flash->read) { |
| 1314 | msg_cerr("No read function available for this flash chip.\n"); |
| 1315 | ret = 1; |
| 1316 | goto out_free; |
| 1317 | } |
Louis Yung-Chieh Lo | 9c7525f | 2011-03-04 12:32:02 +0800 | [diff] [blame] | 1318 | |
| 1319 | /* First try to handle partial read case, rather than read the whole |
| 1320 | * flash, which is slow. */ |
David Hendricks | e345194 | 2013-03-21 17:23:29 -0700 | [diff] [blame] | 1321 | ret = handle_partial_read(flash, buf, read_flash, 1); |
Louis Yung-Chieh Lo | 9c7525f | 2011-03-04 12:32:02 +0800 | [diff] [blame] | 1322 | if (ret < 0) { |
| 1323 | msg_cerr("Partial read operation failed!\n"); |
| 1324 | ret = 1; |
| 1325 | goto out_free; |
| 1326 | } else if (ret > 0) { |
David Hendricks | df29a83 | 2013-06-28 14:33:51 -0700 | [diff] [blame] | 1327 | int num_regions = get_num_include_args(); |
| 1328 | |
| 1329 | if (ret != num_regions) { |
| 1330 | msg_cerr("Requested %d regions, but only read %d\n", |
| 1331 | num_regions, ret); |
| 1332 | ret = 1; |
| 1333 | goto out_free; |
| 1334 | } |
| 1335 | |
| 1336 | ret = 0; |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 1337 | } else { |
David Hendricks | e345194 | 2013-03-21 17:23:29 -0700 | [diff] [blame] | 1338 | if (read_flash(flash, buf, 0, size)) { |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 1339 | msg_cerr("Read operation failed!\n"); |
| 1340 | ret = 1; |
| 1341 | goto out_free; |
| 1342 | } |
hailfinger | 42a850a | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
David Hendricks | df29a83 | 2013-06-28 14:33:51 -0700 | [diff] [blame] | 1345 | if (filename) |
| 1346 | ret = write_buf_to_file(buf, size, filename); |
| 1347 | |
hailfinger | 42a850a | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1348 | out_free: |
| 1349 | free(buf); |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1350 | if (ret) |
| 1351 | msg_cerr("FAILED."); |
| 1352 | else |
| 1353 | msg_cdbg("done."); |
hailfinger | 42a850a | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1354 | return ret; |
| 1355 | } |
| 1356 | |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1357 | /* This function shares a lot of its structure with erase_and_write_flash() and |
| 1358 | * walk_eraseregions(). |
hailfinger | 9fed35d | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1359 | * Even if an error is found, the function will keep going and check the rest. |
| 1360 | */ |
hailfinger | 48ed3e2 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1361 | static int selfcheck_eraseblocks(const struct flashchip *flash) |
hailfinger | 4517787 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1362 | { |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 1363 | int i, j, k; |
| 1364 | int ret = 0; |
hailfinger | 4517787 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1365 | |
| 1366 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
| 1367 | unsigned int done = 0; |
| 1368 | struct block_eraser eraser = flash->block_erasers[k]; |
| 1369 | |
| 1370 | for (i = 0; i < NUM_ERASEREGIONS; i++) { |
| 1371 | /* Blocks with zero size are bugs in flashchips.c. */ |
| 1372 | if (eraser.eraseblocks[i].count && |
| 1373 | !eraser.eraseblocks[i].size) { |
| 1374 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1375 | "%i region %i has size 0. Please report" |
| 1376 | " a bug at flashrom@flashrom.org\n", |
| 1377 | flash->name, k, i); |
hailfinger | 9fed35d | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1378 | ret = 1; |
hailfinger | 4517787 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1379 | } |
| 1380 | /* Blocks with zero count are bugs in flashchips.c. */ |
| 1381 | if (!eraser.eraseblocks[i].count && |
| 1382 | eraser.eraseblocks[i].size) { |
| 1383 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1384 | "%i region %i has count 0. Please report" |
| 1385 | " a bug at flashrom@flashrom.org\n", |
| 1386 | flash->name, k, i); |
hailfinger | 9fed35d | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1387 | ret = 1; |
hailfinger | 4517787 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1388 | } |
| 1389 | done += eraser.eraseblocks[i].count * |
| 1390 | eraser.eraseblocks[i].size; |
| 1391 | } |
hailfinger | 9fed35d | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1392 | /* Empty eraseblock definition with erase function. */ |
| 1393 | if (!done && eraser.block_erase) |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1394 | msg_gspew("Strange: Empty eraseblock definition with " |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1395 | "non-empty erase function. Not an error.\n"); |
hailfinger | 4517787 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1396 | if (!done) |
| 1397 | continue; |
| 1398 | if (done != flash->total_size * 1024) { |
| 1399 | msg_gerr("ERROR: Flash chip %s erase function %i " |
| 1400 | "region walking resulted in 0x%06x bytes total," |
| 1401 | " expected 0x%06x bytes. Please report a bug at" |
| 1402 | " flashrom@flashrom.org\n", flash->name, k, |
| 1403 | done, flash->total_size * 1024); |
hailfinger | 9fed35d | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1404 | ret = 1; |
hailfinger | 4517787 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1405 | } |
hailfinger | 9fed35d | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1406 | if (!eraser.block_erase) |
| 1407 | continue; |
| 1408 | /* Check if there are identical erase functions for different |
| 1409 | * layouts. That would imply "magic" erase functions. The |
| 1410 | * easiest way to check this is with function pointers. |
| 1411 | */ |
uwe | f6f94d4 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 1412 | for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) { |
hailfinger | 9fed35d | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1413 | if (eraser.block_erase == |
| 1414 | flash->block_erasers[j].block_erase) { |
| 1415 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1416 | "%i and %i are identical. Please report" |
| 1417 | " a bug at flashrom@flashrom.org\n", |
| 1418 | flash->name, k, j); |
| 1419 | ret = 1; |
| 1420 | } |
uwe | f6f94d4 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 1421 | } |
hailfinger | 4517787 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1422 | } |
hailfinger | 9fed35d | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1423 | return ret; |
hailfinger | 4517787 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1426 | static int erase_and_write_block_helper(struct flashchip *flash, |
| 1427 | unsigned int start, unsigned int len, |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1428 | uint8_t *curcontents, |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1429 | uint8_t *newcontents, |
| 1430 | int (*erasefn) (struct flashchip *flash, |
| 1431 | unsigned int addr, |
| 1432 | unsigned int len)) |
| 1433 | { |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 1434 | unsigned int starthere = 0, lenhere = 0; |
| 1435 | int ret = 0, skip = 1, writecount = 0; |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1436 | enum write_granularity gran = write_gran_256bytes; /* FIXME */ |
| 1437 | |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1438 | /* curcontents and newcontents are opaque to walk_eraseregions, and |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1439 | * need to be adjusted here to keep the impression of proper abstraction |
| 1440 | */ |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1441 | curcontents += start; |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1442 | newcontents += start; |
| 1443 | msg_cdbg(":"); |
| 1444 | /* FIXME: Assume 256 byte granularity for now to play it safe. */ |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 1445 | if (need_erase(flash, curcontents, newcontents, len, gran)) { |
David Hendricks | 9ba79fb | 2015-04-03 12:06:16 -0700 | [diff] [blame] | 1446 | content_has_changed |= 1; |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1447 | msg_cdbg("E"); |
| 1448 | ret = erasefn(flash, start, len); |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 1449 | if (ret) { |
| 1450 | if (ret == ACCESS_DENIED) |
| 1451 | msg_cdbg("D"); |
| 1452 | else |
| 1453 | msg_cerr("ERASE FAILED!\n"); |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1454 | return ret; |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 1455 | } |
| 1456 | |
hailfinger | ac8e318 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 1457 | if (check_erased_range(flash, start, len)) { |
| 1458 | msg_cerr("ERASE FAILED!\n"); |
| 1459 | return -1; |
| 1460 | } |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1461 | /* Erase was successful. Adjust curcontents. */ |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 1462 | memset(curcontents, flash_erase_value(flash), len); |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1463 | skip = 0; |
| 1464 | } |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1465 | /* get_next_write() sets starthere to a new value after the call. */ |
| 1466 | while ((lenhere = get_next_write(curcontents + starthere, |
| 1467 | newcontents + starthere, |
| 1468 | len - starthere, &starthere, gran))) { |
David Hendricks | 9ba79fb | 2015-04-03 12:06:16 -0700 | [diff] [blame] | 1469 | content_has_changed |= 1; |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1470 | if (!writecount++) |
| 1471 | msg_cdbg("W"); |
| 1472 | /* Needs the partial write function signature. */ |
David Hendricks | 7c8a161 | 2013-04-26 19:14:44 -0700 | [diff] [blame] | 1473 | ret = write_flash(flash, newcontents + starthere, |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1474 | start + starthere, lenhere); |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 1475 | if (ret) { |
| 1476 | if (ret == ACCESS_DENIED) |
| 1477 | msg_cdbg("D"); |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1478 | return ret; |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 1479 | } |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1480 | starthere += lenhere; |
| 1481 | skip = 0; |
| 1482 | } |
| 1483 | if (skip) |
| 1484 | msg_cdbg("S"); |
| 1485 | return ret; |
| 1486 | } |
| 1487 | |
hailfinger | 83541b3 | 2010-07-13 00:42:00 +0000 | [diff] [blame] | 1488 | static int walk_eraseregions(struct flashchip *flash, int erasefunction, |
| 1489 | int (*do_something) (struct flashchip *flash, |
| 1490 | unsigned int addr, |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1491 | unsigned int len, |
| 1492 | uint8_t *param1, |
| 1493 | uint8_t *param2, |
| 1494 | int (*erasefn) ( |
| 1495 | struct flashchip *flash, |
| 1496 | unsigned int addr, |
| 1497 | unsigned int len)), |
| 1498 | void *param1, void *param2) |
hailfinger | 2b8c938 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1499 | { |
David Hendricks | 605544b | 2015-08-15 16:32:58 -0700 | [diff] [blame] | 1500 | int i, j, rc = -1; |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 1501 | unsigned int start = 0; |
| 1502 | unsigned int len; |
hailfinger | 2b8c938 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1503 | struct block_eraser eraser = flash->block_erasers[erasefunction]; |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1504 | |
hailfinger | 2b8c938 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1505 | for (i = 0; i < NUM_ERASEREGIONS; i++) { |
David Hendricks | 605544b | 2015-08-15 16:32:58 -0700 | [diff] [blame] | 1506 | /* count==0 and size==0 for all automatically initialized array |
hailfinger | 2b8c938 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1507 | * members so the loop below won't be executed for them. |
| 1508 | */ |
| 1509 | len = eraser.eraseblocks[i].size; |
David Hendricks | 605544b | 2015-08-15 16:32:58 -0700 | [diff] [blame] | 1510 | if (!len) |
| 1511 | continue; |
| 1512 | |
| 1513 | if (required_erase_size && (len != required_erase_size)) { |
| 1514 | msg_cdbg("%u does not meet erase alignment", len); |
| 1515 | rc = -1; |
| 1516 | break; |
| 1517 | } |
| 1518 | |
hailfinger | 2b8c938 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1519 | for (j = 0; j < eraser.eraseblocks[i].count; j++) { |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1520 | /* Print this for every block except the first one. */ |
| 1521 | if (i || j) |
| 1522 | msg_cdbg(", "); |
| 1523 | msg_cdbg("0x%06x-0x%06x", start, |
hailfinger | 2b8c938 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1524 | start + len - 1); |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 1525 | rc = do_something(flash, start, len, param1, param2, |
| 1526 | eraser.block_erase); |
| 1527 | if (rc) { |
| 1528 | if (ignore_error(rc)) |
| 1529 | rc = 0; |
| 1530 | else |
| 1531 | return rc; |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1532 | } |
hailfinger | 2b8c938 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1533 | start += len; |
| 1534 | } |
| 1535 | } |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1536 | msg_cdbg("\n"); |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 1537 | return rc; |
hailfinger | 2b8c938 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
stefanct | 569dbb6 | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 1540 | static int check_block_eraser(const struct flashchip *flash, int k, int log) |
hailfinger | cf848f1 | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1541 | { |
| 1542 | struct block_eraser eraser = flash->block_erasers[k]; |
| 1543 | |
| 1544 | if (!eraser.block_erase && !eraser.eraseblocks[0].count) { |
| 1545 | if (log) |
| 1546 | msg_cdbg("not defined. "); |
| 1547 | return 1; |
| 1548 | } |
| 1549 | if (!eraser.block_erase && eraser.eraseblocks[0].count) { |
| 1550 | if (log) |
| 1551 | msg_cdbg("eraseblock layout is known, but matching " |
stefanct | 9e6b98a | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 1552 | "block erase function is not implemented. "); |
hailfinger | cf848f1 | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1553 | return 1; |
| 1554 | } |
| 1555 | if (eraser.block_erase && !eraser.eraseblocks[0].count) { |
| 1556 | if (log) |
| 1557 | msg_cdbg("block erase function found, but " |
stefanct | 9e6b98a | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 1558 | "eraseblock layout is not defined. "); |
hailfinger | cf848f1 | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1559 | return 1; |
| 1560 | } |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1564 | int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents, |
| 1565 | uint8_t *newcontents) |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1566 | { |
hailfinger | 8f524a8 | 2011-07-21 21:21:04 +0000 | [diff] [blame] | 1567 | int k, ret = 1; |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1568 | uint8_t *curcontents; |
| 1569 | unsigned long size = flash->total_size * 1024; |
stefanct | e1c5acf | 2011-07-04 07:27:17 +0000 | [diff] [blame] | 1570 | unsigned int usable_erasefunctions = count_usable_erasers(flash); |
hailfinger | cf848f1 | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1571 | |
hailfinger | cf848f1 | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1572 | msg_cinfo("Erasing and writing flash chip... "); |
stefanct | d611e8f | 2011-07-12 22:35:21 +0000 | [diff] [blame] | 1573 | curcontents = malloc(size); |
| 1574 | if (!curcontents) { |
| 1575 | msg_gerr("Out of memory!\n"); |
| 1576 | exit(1); |
| 1577 | } |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1578 | /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */ |
| 1579 | memcpy(curcontents, oldcontents, size); |
| 1580 | |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1581 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
stefanct | f0bc471 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1582 | if (k != 0) |
| 1583 | msg_cdbg("Looking for another erase function.\n"); |
hailfinger | 8f524a8 | 2011-07-21 21:21:04 +0000 | [diff] [blame] | 1584 | if (!usable_erasefunctions) { |
| 1585 | msg_cdbg("No usable erase functions left.\n"); |
| 1586 | break; |
| 1587 | } |
stefanct | f0bc471 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1588 | msg_cdbg("Trying erase function %i... ", k); |
| 1589 | if (check_block_eraser(flash, k, 1)) |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1590 | continue; |
hailfinger | cf848f1 | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1591 | usable_erasefunctions--; |
stefanct | f0bc471 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1592 | ret = walk_eraseregions(flash, k, &erase_and_write_block_helper, |
| 1593 | curcontents, newcontents); |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1594 | /* If everything is OK, don't try another erase function. */ |
| 1595 | if (!ret) |
| 1596 | break; |
hailfinger | 6237f5e | 2010-12-02 02:41:55 +0000 | [diff] [blame] | 1597 | /* Write/erase failed, so try to find out what the current chip |
hailfinger | 8f524a8 | 2011-07-21 21:21:04 +0000 | [diff] [blame] | 1598 | * contents are. If no usable erase functions remain, we can |
| 1599 | * skip this: the next iteration will break immediately anyway. |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1600 | */ |
hailfinger | cf848f1 | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1601 | if (!usable_erasefunctions) |
| 1602 | continue; |
stefanct | f0bc471 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1603 | /* Reading the whole chip may take a while, inform the user even |
| 1604 | * in non-verbose mode. |
| 1605 | */ |
| 1606 | msg_cinfo("Reading current flash chip contents... "); |
David Hendricks | e345194 | 2013-03-21 17:23:29 -0700 | [diff] [blame] | 1607 | if (read_flash(flash, curcontents, 0, size)) { |
hailfinger | 6237f5e | 2010-12-02 02:41:55 +0000 | [diff] [blame] | 1608 | /* Now we are truly screwed. Read failed as well. */ |
stefanct | f0bc471 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1609 | msg_cerr("Can't read anymore! Aborting.\n"); |
hailfinger | 6237f5e | 2010-12-02 02:41:55 +0000 | [diff] [blame] | 1610 | /* We have no idea about the flash chip contents, so |
| 1611 | * retrying with another erase function is pointless. |
| 1612 | */ |
| 1613 | break; |
| 1614 | } |
David Hendricks | 89a45e5 | 2011-11-22 16:56:22 -0800 | [diff] [blame] | 1615 | msg_cdbg("done. "); |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1616 | } |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1617 | /* Free the scratchpad. */ |
| 1618 | free(curcontents); |
hailfinger | 1e9ee0f | 2009-05-08 17:15:15 +0000 | [diff] [blame] | 1619 | |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1620 | if (ret) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1621 | msg_cerr("FAILED!\n"); |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1622 | } else { |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1623 | msg_cdbg("SUCCESS.\n"); |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1624 | } |
| 1625 | return ret; |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
hailfinger | 4c47e9d | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1628 | void nonfatal_help_message(void) |
| 1629 | { |
| 1630 | msg_gerr("Writing to the flash chip apparently didn't do anything.\n" |
| 1631 | "This means we have to add special support for your board, " |
| 1632 | "programmer or flash chip.\n" |
| 1633 | "Please report this on IRC at irc.freenode.net (channel " |
| 1634 | "#flashrom) or\n" |
| 1635 | "mail flashrom@flashrom.org!\n" |
| 1636 | "-------------------------------------------------------------" |
| 1637 | "------------------\n" |
| 1638 | "You may now reboot or simply leave the machine running.\n"); |
| 1639 | } |
| 1640 | |
uwe | b34ec9f | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 1641 | void emergency_help_message(void) |
hailfinger | 0459e1c | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1642 | { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1643 | msg_gerr("Your flash chip is in an unknown state.\n" |
uwe | b34ec9f | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 1644 | "Get help on IRC at irc.freenode.net (channel #flashrom) or\n" |
hailfinger | 5bae233 | 2010-10-08 11:03:02 +0000 | [diff] [blame] | 1645 | "mail flashrom@flashrom.org with FAILED: your board name in " |
| 1646 | "the subject line!\n" |
hailfinger | 74819ad | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1647 | "-------------------------------------------------------------" |
| 1648 | "------------------\n" |
hailfinger | 0459e1c | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1649 | "DO NOT REBOOT OR POWEROFF!\n"); |
| 1650 | } |
| 1651 | |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1652 | /* The way to go if you want a delimited list of programmers */ |
stefanct | 5270028 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 1653 | void list_programmers(const char *delim) |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1654 | { |
| 1655 | enum programmer p; |
| 1656 | for (p = 0; p < PROGRAMMER_INVALID; p++) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1657 | msg_ginfo("%s", programmer_table[p].name); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1658 | if (p < PROGRAMMER_INVALID - 1) |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1659 | msg_ginfo("%s", delim); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1660 | } |
Simon Glass | 8dc8273 | 2013-07-16 10:13:51 -0600 | [diff] [blame] | 1661 | msg_ginfo("\n"); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
hailfinger | f79d171 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1664 | void list_programmers_linebreak(int startcol, int cols, int paren) |
| 1665 | { |
| 1666 | const char *pname; |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 1667 | int pnamelen; |
| 1668 | int remaining = 0, firstline = 1; |
hailfinger | f79d171 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1669 | enum programmer p; |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 1670 | int i; |
hailfinger | f79d171 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1671 | |
| 1672 | for (p = 0; p < PROGRAMMER_INVALID; p++) { |
| 1673 | pname = programmer_table[p].name; |
| 1674 | pnamelen = strlen(pname); |
| 1675 | if (remaining - pnamelen - 2 < 0) { |
| 1676 | if (firstline) |
| 1677 | firstline = 0; |
| 1678 | else |
| 1679 | printf("\n"); |
| 1680 | for (i = 0; i < startcol; i++) |
| 1681 | printf(" "); |
| 1682 | remaining = cols - startcol; |
| 1683 | } else { |
| 1684 | printf(" "); |
| 1685 | remaining--; |
| 1686 | } |
| 1687 | if (paren && (p == 0)) { |
| 1688 | printf("("); |
| 1689 | remaining--; |
| 1690 | } |
| 1691 | printf("%s", pname); |
| 1692 | remaining -= pnamelen; |
| 1693 | if (p < PROGRAMMER_INVALID - 1) { |
| 1694 | printf(","); |
| 1695 | remaining--; |
| 1696 | } else { |
| 1697 | if (paren) |
| 1698 | printf(")"); |
| 1699 | printf("\n"); |
| 1700 | } |
| 1701 | } |
| 1702 | } |
| 1703 | |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1704 | void print_sysinfo(void) |
| 1705 | { |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1706 | /* send to stderr for chromium os */ |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1707 | #if HAVE_UTSNAME == 1 |
| 1708 | struct utsname osinfo; |
| 1709 | uname(&osinfo); |
| 1710 | |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1711 | msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release, |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1712 | osinfo.machine); |
| 1713 | #else |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1714 | msg_gerr(" on unknown machine"); |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1715 | #endif |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1716 | msg_gerr(", built with"); |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1717 | #if NEED_PCI == 1 |
| 1718 | #ifdef PCILIB_VERSION |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1719 | msg_gerr(" libpci %s,", PCILIB_VERSION); |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1720 | #else |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1721 | msg_gerr(" unknown PCI library,"); |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1722 | #endif |
| 1723 | #endif |
| 1724 | #ifdef __clang__ |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1725 | msg_gerr(" LLVM Clang"); |
hailfinger | 3cc85ad | 2010-07-17 14:49:30 +0000 | [diff] [blame] | 1726 | #ifdef __clang_version__ |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1727 | msg_gerr(" %s,", __clang_version__); |
hailfinger | 3cc85ad | 2010-07-17 14:49:30 +0000 | [diff] [blame] | 1728 | #else |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1729 | msg_gerr(" unknown version (before r102686),"); |
hailfinger | 3cc85ad | 2010-07-17 14:49:30 +0000 | [diff] [blame] | 1730 | #endif |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1731 | #elif defined(__GNUC__) |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1732 | msg_gerr(" GCC"); |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1733 | #ifdef __VERSION__ |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1734 | msg_gerr(" %s,", __VERSION__); |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1735 | #else |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1736 | msg_gerr(" unknown version,"); |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1737 | #endif |
| 1738 | #else |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1739 | msg_gerr(" unknown compiler,"); |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1740 | #endif |
| 1741 | #if defined (__FLASHROM_LITTLE_ENDIAN__) |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1742 | msg_gerr(" little endian"); |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1743 | #else |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1744 | msg_gerr(" big endian"); |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1745 | #endif |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1746 | msg_gerr("\n"); |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
uwe | fdeca09 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 1749 | void print_version(void) |
| 1750 | { |
David Hendricks | c6c9f82 | 2010-11-03 15:07:01 -0700 | [diff] [blame] | 1751 | /* send to stderr for chromium os */ |
| 1752 | msg_gerr("flashrom v%s", flashrom_version); |
hailfinger | 3b47163 | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1753 | print_sysinfo(); |
uwe | fdeca09 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
hailfinger | 74819ad | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1756 | void print_banner(void) |
| 1757 | { |
| 1758 | msg_ginfo("flashrom is free software, get the source code at " |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1759 | "http://www.flashrom.org\n"); |
hailfinger | 74819ad | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1760 | msg_ginfo("\n"); |
| 1761 | } |
| 1762 | |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1763 | int selfcheck(void) |
| 1764 | { |
hailfinger | 4517787 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1765 | int ret = 0; |
hailfinger | 48ed3e2 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1766 | const struct flashchip *flash; |
hailfinger | 4517787 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1767 | |
| 1768 | /* Safety check. Instead of aborting after the first error, check |
| 1769 | * if more errors exist. |
| 1770 | */ |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1771 | if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) { |
snelson | e42c380 | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1772 | msg_gerr("Programmer table miscompilation!\n"); |
hailfinger | 4517787 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1773 | ret = 1; |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1774 | } |
stefanct | 8632c92 | 2011-07-26 14:33:46 +0000 | [diff] [blame] | 1775 | /* It would be favorable if we could also check for correct termination |
stefanct | dfd5883 | 2011-07-25 20:38:52 +0000 | [diff] [blame] | 1776 | * of the following arrays, but we don't know their sizes in here... |
stefanct | 6d836ba | 2011-05-26 01:35:19 +0000 | [diff] [blame] | 1777 | * For 'flashchips' we check the first element to be non-null. In the |
| 1778 | * other cases there exist use cases where the first element can be |
| 1779 | * null. */ |
Yunlian Jiang | 945c339 | 2015-01-20 15:56:44 -0800 | [diff] [blame] | 1780 | if (flashchips[0].vendor == NULL) { |
stefanct | 6d836ba | 2011-05-26 01:35:19 +0000 | [diff] [blame] | 1781 | msg_gerr("Flashchips table miscompilation!\n"); |
| 1782 | ret = 1; |
| 1783 | } |
hailfinger | 4517787 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1784 | for (flash = flashchips; flash && flash->name; flash++) |
| 1785 | if (selfcheck_eraseblocks(flash)) |
| 1786 | ret = 1; |
stefanct | 6d836ba | 2011-05-26 01:35:19 +0000 | [diff] [blame] | 1787 | |
hailfinger | 4517787 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1788 | return ret; |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
hailfinger | 48ed3e2 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1791 | void check_chip_supported(const struct flashchip *flash) |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1792 | { |
| 1793 | if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1794 | msg_cdbg("===\n"); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1795 | if (flash->tested & TEST_BAD_MASK) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1796 | msg_cdbg("This flash part has status NOT WORKING for operations:"); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1797 | if (flash->tested & TEST_BAD_PROBE) |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1798 | msg_cdbg(" PROBE"); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1799 | if (flash->tested & TEST_BAD_READ) |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1800 | msg_cdbg(" READ"); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1801 | if (flash->tested & TEST_BAD_ERASE) |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1802 | msg_cdbg(" ERASE"); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1803 | if (flash->tested & TEST_BAD_WRITE) |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1804 | msg_cdbg(" WRITE"); |
Duncan Laurie | 06ffd52 | 2015-10-26 12:56:08 -0700 | [diff] [blame] | 1805 | if (flash->tested & TEST_BAD_UREAD) |
| 1806 | msg_cdbg(" UNBOUNDED READ"); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1807 | msg_cdbg("\n"); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1808 | } |
| 1809 | if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) || |
| 1810 | (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) || |
| 1811 | (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) || |
| 1812 | (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) { |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1813 | msg_cdbg("This flash part has status UNTESTED for operations:"); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1814 | if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1815 | msg_cdbg(" PROBE"); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1816 | if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1817 | msg_cdbg(" READ"); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1818 | if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1819 | msg_cdbg(" ERASE"); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1820 | if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE)) |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1821 | msg_cdbg(" WRITE"); |
Duncan Laurie | 06ffd52 | 2015-10-26 12:56:08 -0700 | [diff] [blame] | 1822 | if (!(flash->tested & TEST_BAD_UREAD) && !(flash->tested & TEST_OK_UREAD)) |
| 1823 | msg_cdbg(" UNBOUNDED READ"); |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1824 | msg_cdbg("\n"); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1825 | } |
hailfinger | 92cd8e3 | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 1826 | /* FIXME: This message is designed towards CLI users. */ |
David Hendricks | c801adb | 2010-12-09 16:58:56 -0800 | [diff] [blame] | 1827 | msg_cdbg("The test status of this chip may have been updated " |
hailfinger | 74819ad | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1828 | "in the latest development\n" |
| 1829 | "version of flashrom. If you are running the latest " |
| 1830 | "development version,\n" |
| 1831 | "please email a report to flashrom@flashrom.org if " |
| 1832 | "any of the above operations\n" |
| 1833 | "work correctly for you with this flash part. Please " |
| 1834 | "include the flashrom\n" |
| 1835 | "output with the additional -V option for all " |
| 1836 | "operations you tested (-V, -Vr,\n" |
| 1837 | "-Vw, -VE), and mention which mainboard or " |
| 1838 | "programmer you tested.\n" |
hailfinger | 5bae233 | 2010-10-08 11:03:02 +0000 | [diff] [blame] | 1839 | "Please mention your board in the subject line. " |
| 1840 | "Thanks for your help!\n"); |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1841 | } |
| 1842 | } |
| 1843 | |
hailfinger | 771fc18 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1844 | /* FIXME: This function signature needs to be improved once doit() has a better |
| 1845 | * function signature. |
| 1846 | */ |
stefanct | 0211658 | 2011-05-18 01:30:56 +0000 | [diff] [blame] | 1847 | int chip_safety_check(struct flashchip *flash, int force, int read_it, int write_it, int erase_it, int verify_it) |
hailfinger | 771fc18 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1848 | { |
| 1849 | if (!programmer_may_write && (write_it || erase_it)) { |
| 1850 | msg_perr("Write/erase is not working yet on your programmer in " |
| 1851 | "its current configuration.\n"); |
| 1852 | /* --force is the wrong approach, but it's the best we can do |
| 1853 | * until the generic programmer parameter parser is merged. |
| 1854 | */ |
| 1855 | if (!force) |
| 1856 | return 1; |
| 1857 | msg_cerr("Continuing anyway.\n"); |
| 1858 | } |
| 1859 | |
| 1860 | if (read_it || erase_it || write_it || verify_it) { |
| 1861 | /* Everything needs read. */ |
| 1862 | if (flash->tested & TEST_BAD_READ) { |
| 1863 | msg_cerr("Read is not working on this chip. "); |
| 1864 | if (!force) |
| 1865 | return 1; |
| 1866 | msg_cerr("Continuing anyway.\n"); |
| 1867 | } |
| 1868 | if (!flash->read) { |
| 1869 | msg_cerr("flashrom has no read function for this " |
| 1870 | "flash chip.\n"); |
| 1871 | return 1; |
| 1872 | } |
| 1873 | } |
| 1874 | if (erase_it || write_it) { |
| 1875 | /* Write needs erase. */ |
| 1876 | if (flash->tested & TEST_BAD_ERASE) { |
| 1877 | msg_cerr("Erase is not working on this chip. "); |
| 1878 | if (!force) |
| 1879 | return 1; |
| 1880 | msg_cerr("Continuing anyway.\n"); |
| 1881 | } |
stefanct | e1c5acf | 2011-07-04 07:27:17 +0000 | [diff] [blame] | 1882 | if(count_usable_erasers(flash) == 0) { |
stefanct | 569dbb6 | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 1883 | msg_cerr("flashrom has no erase function for this " |
| 1884 | "flash chip.\n"); |
| 1885 | return 1; |
| 1886 | } |
hailfinger | 771fc18 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1887 | } |
| 1888 | if (write_it) { |
| 1889 | if (flash->tested & TEST_BAD_WRITE) { |
| 1890 | msg_cerr("Write is not working on this chip. "); |
| 1891 | if (!force) |
| 1892 | return 1; |
| 1893 | msg_cerr("Continuing anyway.\n"); |
| 1894 | } |
| 1895 | if (!flash->write) { |
| 1896 | msg_cerr("flashrom has no write function for this " |
| 1897 | "flash chip.\n"); |
| 1898 | return 1; |
| 1899 | } |
| 1900 | } |
| 1901 | return 0; |
| 1902 | } |
| 1903 | |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1904 | /* This function signature is horrible. We need to design a better interface, |
| 1905 | * but right now it allows us to split off the CLI code. |
hailfinger | d217d12 | 2010-10-08 18:52:29 +0000 | [diff] [blame] | 1906 | * Besides that, the function itself is a textbook example of abysmal code flow. |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1907 | */ |
Simon Glass | 9ad06c1 | 2013-07-03 22:08:17 +0900 | [diff] [blame] | 1908 | int doit(struct flashchip *flash, int force, const char *filename, int read_it, |
| 1909 | int write_it, int erase_it, int verify_it, int extract_it, |
| 1910 | const char *diff_file) |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1911 | { |
hailfinger | 4c47e9d | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1912 | uint8_t *oldcontents; |
| 1913 | uint8_t *newcontents; |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1914 | int ret = 0; |
hailfinger | 4c47e9d | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1915 | unsigned long size = flash->total_size * 1024; |
hailfinger | c77acb5 | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1916 | |
stefanct | 0211658 | 2011-05-18 01:30:56 +0000 | [diff] [blame] | 1917 | if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) { |
hailfinger | 771fc18 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1918 | msg_cerr("Aborting.\n"); |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1919 | ret = 1; |
| 1920 | goto out_nofree; |
hailfinger | 771fc18 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
hailfinger | 771fc18 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1923 | /* Given the existence of read locks, we want to unlock for read, |
| 1924 | * erase and write. |
| 1925 | */ |
| 1926 | if (flash->unlock) |
| 1927 | flash->unlock(flash); |
| 1928 | |
David Hendricks | 6d62d75 | 2011-03-07 21:20:22 -0800 | [diff] [blame] | 1929 | /* add entries for regions specified in flashmap */ |
Louis Yung-Chieh Lo | 1f6bbf5 | 2011-04-06 10:24:38 +0800 | [diff] [blame] | 1930 | if (!set_ignore_fmap && add_fmap_entries(flash) < 0) { |
David Hendricks | 6d62d75 | 2011-03-07 21:20:22 -0800 | [diff] [blame] | 1931 | ret = 1; |
| 1932 | goto out_nofree; |
| 1933 | } |
| 1934 | |
Simon Glass | 9ad06c1 | 2013-07-03 22:08:17 +0900 | [diff] [blame] | 1935 | if (extract_it) { |
| 1936 | ret = extract_regions(flash); |
| 1937 | goto out_nofree; |
| 1938 | } |
| 1939 | |
David Hendricks | d0ea9ed | 2011-03-04 17:31:57 -0800 | [diff] [blame] | 1940 | /* mark entries included using -i argument as "included" if they are |
| 1941 | found in the master rom_entries list */ |
| 1942 | if (process_include_args() < 0) { |
| 1943 | ret = 1; |
| 1944 | goto out_nofree; |
| 1945 | } |
| 1946 | |
hailfinger | 771fc18 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1947 | if (read_it) { |
| 1948 | ret = read_flash_to_file(flash, filename); |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1949 | goto out_nofree; |
hailfinger | 5828baf | 2010-07-03 12:14:25 +0000 | [diff] [blame] | 1950 | } |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1951 | |
stefanct | d611e8f | 2011-07-12 22:35:21 +0000 | [diff] [blame] | 1952 | oldcontents = malloc(size); |
| 1953 | if (!oldcontents) { |
| 1954 | msg_gerr("Out of memory!\n"); |
| 1955 | exit(1); |
| 1956 | } |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 1957 | /* Assume worst case: All blocks are not erased. */ |
| 1958 | memset(oldcontents, flash_unerased_value(flash), size); |
stefanct | d611e8f | 2011-07-12 22:35:21 +0000 | [diff] [blame] | 1959 | newcontents = malloc(size); |
| 1960 | if (!newcontents) { |
| 1961 | msg_gerr("Out of memory!\n"); |
| 1962 | exit(1); |
| 1963 | } |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 1964 | /* Assume best case: All blocks are erased. */ |
| 1965 | memset(newcontents, flash_erase_value(flash), size); |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1966 | /* Side effect of the assumptions above: Default write action is erase |
| 1967 | * because newcontents looks like a completely erased chip, and |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 1968 | * oldcontents being completely unerased means we have to erase |
| 1969 | * everything before we can write. |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1970 | */ |
| 1971 | |
ollie | 0eb62d6 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 1972 | if (erase_it) { |
hailfinger | 4c47e9d | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1973 | /* FIXME: Do we really want the scary warning if erase failed? |
| 1974 | * After all, after erase the chip is either blank or partially |
| 1975 | * blank or it has the old contents. A blank chip won't boot, |
| 1976 | * so if the user wanted erase and reboots afterwards, the user |
| 1977 | * knows very well that booting won't work. |
| 1978 | */ |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1979 | if (erase_and_write_flash(flash, oldcontents, newcontents)) { |
hailfinger | 0459e1c | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1980 | emergency_help_message(); |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1981 | ret = 1; |
hailfinger | 0459e1c | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1982 | } |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1983 | goto out; |
hailfinger | d217d12 | 2010-10-08 18:52:29 +0000 | [diff] [blame] | 1984 | } |
hailfinger | 771fc18 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1985 | |
hailfinger | d217d12 | 2010-10-08 18:52:29 +0000 | [diff] [blame] | 1986 | if (write_it || verify_it) { |
David Hendricks | df29a83 | 2013-06-28 14:33:51 -0700 | [diff] [blame] | 1987 | /* |
| 1988 | * Note: This must be done before any files specified by -i |
| 1989 | * arguments are processed merged into the newcontents since |
| 1990 | * -i files take priority. See http://crbug.com/263495. |
| 1991 | */ |
| 1992 | if (filename) { |
| 1993 | if (read_buf_from_file(newcontents, size, filename)) { |
| 1994 | ret = 1; |
| 1995 | goto out; |
| 1996 | } |
| 1997 | } else { |
| 1998 | /* Content will be read from -i args, so they must |
| 1999 | * not overlap. */ |
| 2000 | if (included_regions_overlap()) { |
| 2001 | msg_gerr("Error: Included regions must " |
| 2002 | "not overlap.\n"); |
| 2003 | ret = 1; |
| 2004 | goto out; |
| 2005 | } |
stepan | 1da96c0 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
David Hendricks | ac82cac | 2012-06-19 10:29:37 -0700 | [diff] [blame] | 2008 | #if 0 |
| 2009 | /* |
| 2010 | * FIXME: show_id() causes failure if vendor:mainboard do not |
| 2011 | * match. This may happen if codenames are in flux. |
| 2012 | * See chrome-os-partner:10414. |
| 2013 | */ |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 2014 | #if CONFIG_INTERNAL == 1 |
hailfinger | 4c47e9d | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 2015 | if (programmer == PROGRAMMER_INTERNAL) |
| 2016 | show_id(newcontents, size, force); |
hailfinger | 80422e2 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 2017 | #endif |
David Hendricks | ac82cac | 2012-06-19 10:29:37 -0700 | [diff] [blame] | 2018 | #endif |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
David Hendricks | c44d7a0 | 2011-10-17 11:28:43 -0700 | [diff] [blame] | 2021 | /* Obtain a reference image so that we can check whether regions need |
| 2022 | * to be erased and to give better diagnostics in case write fails. |
David Hendricks | 52ddff0 | 2013-07-23 15:05:14 -0700 | [diff] [blame] | 2023 | * If --fast-verify is used then only the regions which are included |
| 2024 | * using -i will be read. |
hailfinger | 4c47e9d | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 2025 | */ |
David Hendricks | c44d7a0 | 2011-10-17 11:28:43 -0700 | [diff] [blame] | 2026 | if (diff_file) { |
| 2027 | msg_cdbg("Reading old contents from file... "); |
| 2028 | if (read_buf_from_file(oldcontents, size, diff_file)) { |
| 2029 | ret = 1; |
| 2030 | msg_cdbg("FAILED.\n"); |
| 2031 | goto out; |
| 2032 | } |
| 2033 | } else { |
| 2034 | msg_cdbg("Reading old contents from flash chip... "); |
David Hendricks | d4e712c | 2013-08-02 17:06:16 -0700 | [diff] [blame] | 2035 | if (verify_it == VERIFY_PARTIAL) { |
| 2036 | if (handle_partial_read(flash, oldcontents, |
| 2037 | read_flash, 0) < 0) { |
David Hendricks | 52ddff0 | 2013-07-23 15:05:14 -0700 | [diff] [blame] | 2038 | ret = 1; |
| 2039 | msg_cdbg("FAILED.\n"); |
| 2040 | goto out; |
| 2041 | } |
David Hendricks | d4e712c | 2013-08-02 17:06:16 -0700 | [diff] [blame] | 2042 | } else { |
| 2043 | if (read_flash(flash, oldcontents, 0, size)) { |
David Hendricks | 52ddff0 | 2013-07-23 15:05:14 -0700 | [diff] [blame] | 2044 | ret = 1; |
| 2045 | msg_cdbg("FAILED.\n"); |
| 2046 | goto out; |
| 2047 | } |
David Hendricks | c44d7a0 | 2011-10-17 11:28:43 -0700 | [diff] [blame] | 2048 | } |
hailfinger | 4c47e9d | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 2049 | } |
David Hendricks | 89a45e5 | 2011-11-22 16:56:22 -0800 | [diff] [blame] | 2050 | msg_cdbg("done.\n"); |
hailfinger | 4c47e9d | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 2051 | |
David Hendricks | df29a83 | 2013-06-28 14:33:51 -0700 | [diff] [blame] | 2052 | /* |
| 2053 | * Note: This must be done after reading the file specified for the |
| 2054 | * -w/-v argument, if any, so that files specified using -i end up |
| 2055 | * in the "newcontents" buffer before being written. |
| 2056 | * See http://crbug.com/263495. |
| 2057 | */ |
Louis Yung-Chieh Lo | 404470d | 2011-09-06 16:59:40 +0800 | [diff] [blame] | 2058 | if (handle_romentries(flash, oldcontents, newcontents)) { |
| 2059 | ret = 1; |
David Hendricks | 5d8ea57 | 2013-07-26 14:03:05 -0700 | [diff] [blame] | 2060 | msg_cerr("Error handling ROM entries.\n"); |
Louis Yung-Chieh Lo | 404470d | 2011-09-06 16:59:40 +0800 | [diff] [blame] | 2061 | goto out; |
| 2062 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 2063 | |
stuge | 8ce3a3c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 2064 | if (write_it) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 2065 | // parse the new fmap |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 2066 | if ((ret = cros_ec_prepare(newcontents, size))) { |
| 2067 | msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 2068 | goto out; |
| 2069 | } |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 2070 | |
hailfinger | b437e28 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 2071 | if (erase_and_write_flash(flash, oldcontents, newcontents)) { |
| 2072 | msg_cerr("Uh oh. Erase/write failed. Checking if " |
| 2073 | "anything changed.\n"); |
David Hendricks | e345194 | 2013-03-21 17:23:29 -0700 | [diff] [blame] | 2074 | if (!read_flash(flash, newcontents, 0, size)) { |
hailfinger | 4c47e9d | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 2075 | if (!memcmp(oldcontents, newcontents, size)) { |
| 2076 | msg_cinfo("Good. It seems nothing was " |
| 2077 | "changed.\n"); |
| 2078 | nonfatal_help_message(); |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 2079 | ret = 1; |
| 2080 | goto out; |
hailfinger | 4c47e9d | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 2081 | } |
| 2082 | } |
hailfinger | d217d12 | 2010-10-08 18:52:29 +0000 | [diff] [blame] | 2083 | emergency_help_message(); |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 2084 | ret = 1; |
| 2085 | goto out; |
stuge | 8ce3a3c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 2086 | } |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 2087 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 2088 | ret = cros_ec_need_2nd_pass(); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 2089 | if (ret < 0) { |
| 2090 | // Jump failed |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 2091 | msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n"); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 2092 | emergency_help_message(); |
| 2093 | ret = 1; |
| 2094 | goto out; |
| 2095 | } else if (ret > 0) { |
| 2096 | // Need 2nd pass. Get the just written content. |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 2097 | msg_pdbg("CROS_EC needs 2nd pass.\n"); |
David Hendricks | e345194 | 2013-03-21 17:23:29 -0700 | [diff] [blame] | 2098 | if (read_flash(flash, oldcontents, 0, size)) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 2099 | msg_cerr("Uh oh. Cannot get latest content.\n"); |
| 2100 | emergency_help_message(); |
| 2101 | ret = 1; |
| 2102 | goto out; |
| 2103 | } |
| 2104 | // write 2nd pass |
| 2105 | if (erase_and_write_flash(flash, oldcontents, |
| 2106 | newcontents)) { |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 2107 | msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n"); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame] | 2108 | emergency_help_message(); |
| 2109 | ret = 1; |
| 2110 | goto out; |
| 2111 | } |
| 2112 | ret = 0; |
| 2113 | } |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 2114 | |
David Hendricks | b907de3 | 2014-08-11 16:47:09 -0700 | [diff] [blame] | 2115 | if (cros_ec_finish() < 0) { |
| 2116 | msg_cerr("cros_ec_finish() failed. Stop.\n"); |
Louis Yung-Chieh Lo | deefd82 | 2012-07-09 17:07:43 +0800 | [diff] [blame] | 2117 | emergency_help_message(); |
| 2118 | ret = 1; |
| 2119 | goto out; |
| 2120 | } |
stuge | 8ce3a3c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 2121 | } |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 2122 | |
hailfinger | 0459e1c | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 2123 | if (verify_it) { |
David Hendricks | 9ba79fb | 2015-04-03 12:06:16 -0700 | [diff] [blame] | 2124 | if ((write_it || erase_it) && !content_has_changed) { |
| 2125 | msg_gdbg("Nothing was erased or written, skipping " |
| 2126 | "verification\n"); |
| 2127 | } else { |
| 2128 | /* Work around chips which need some time to calm down. */ |
| 2129 | if (write_it && verify_it != VERIFY_PARTIAL) |
| 2130 | programmer_delay(1000*1000); |
Louis Yung-Chieh Lo | 5d95f04 | 2011-09-01 17:33:06 +0800 | [diff] [blame] | 2131 | |
David Hendricks | 9ba79fb | 2015-04-03 12:06:16 -0700 | [diff] [blame] | 2132 | ret = verify_flash(flash, newcontents, verify_it); |
Louis Yung-Chieh Lo | 5d95f04 | 2011-09-01 17:33:06 +0800 | [diff] [blame] | 2133 | |
David Hendricks | 9ba79fb | 2015-04-03 12:06:16 -0700 | [diff] [blame] | 2134 | /* If we tried to write, and verification now fails, we |
| 2135 | * might have an emergency situation. |
| 2136 | */ |
| 2137 | if (ret && write_it) |
| 2138 | emergency_help_message(); |
| 2139 | } |
hailfinger | 0459e1c | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 2140 | } |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 2141 | |
hailfinger | 90fcf9b | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 2142 | out: |
| 2143 | free(oldcontents); |
| 2144 | free(newcontents); |
| 2145 | out_nofree: |
David Hendricks | bf36f09 | 2010-11-02 23:39:29 -0700 | [diff] [blame] | 2146 | chip_restore(); /* must be done before programmer_shutdown() */ |
David Hendricks | 668f29d | 2011-01-27 18:51:45 -0800 | [diff] [blame] | 2147 | /* |
| 2148 | * programmer_shutdown() call is moved to cli_mfg() in chromium os |
| 2149 | * tree. This is because some operations, such as write protection, |
| 2150 | * requires programmer_shutdown() but does not call doit(). |
| 2151 | */ |
| 2152 | // programmer_shutdown(); |
stepan | 83eca25 | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 2153 | return ret; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 2154 | } |