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