Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1 | /* |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 2 | * This file is part of the flashrom project. |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 3 | * |
Uwe Hermann | d22a1d4 | 2007-09-09 20:21:05 +0000 | [diff] [blame] | 4 | * Copyright (C) 2000 Silicon Integrated System Corporation |
| 5 | * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com> |
Uwe Hermann | c7e8a0c | 2009-05-19 14:14:21 +0000 | [diff] [blame] | 6 | * Copyright (C) 2005-2008 coresystems GmbH |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 7 | * Copyright (C) 2008,2009 Carl-Daniel Hailfinger |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 8 | * |
Uwe Hermann | d110764 | 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. |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 13 | * |
Uwe Hermann | d110764 | 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. |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 18 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 28 | #include <string.h> |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 30 | #include <getopt.h> |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 31 | #if HAVE_UTSNAME == 1 |
| 32 | #include <sys/utsname.h> |
| 33 | #endif |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 34 | #include "flash.h" |
Carl-Daniel Hailfinger | 0845464 | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 35 | #include "flashchips.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame^] | 36 | #include "programmer.h" |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 37 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 38 | const char * const flashrom_version = FLASHROM_VERSION; |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 39 | char *chip_to_probe = NULL; |
Peter Stuge | 7ffbc6f | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 40 | int verbose = 0; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 41 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 42 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 43 | enum programmer programmer = PROGRAMMER_INTERNAL; |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 44 | #elif CONFIG_DUMMY == 1 |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 45 | enum programmer programmer = PROGRAMMER_DUMMY; |
| 46 | #else |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 47 | /* If neither internal nor dummy are selected, we must pick a sensible default. |
| 48 | * Since there is no reason to prefer a particular external programmer, we fail |
| 49 | * if more than one of them is selected. If only one is selected, it is clear |
| 50 | * that the user wants that one to become the default. |
| 51 | */ |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 52 | #if CONFIG_NIC3COM+CONFIG_NICREALTEK+CONFIG_NICNATSEMI+CONFIG_GFXNVIDIA+CONFIG_DRKAISER+CONFIG_SATASII+CONFIG_ATAHPT+CONFIG_FT2232_SPI+CONFIG_SERPROG+CONFIG_BUSPIRATE_SPI+CONFIG_DEDIPROG+CONFIG_RAYER_SPI > 1 |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 53 | #error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all programmers except one. |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 54 | #endif |
| 55 | enum programmer programmer = |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 56 | #if CONFIG_NIC3COM == 1 |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 57 | PROGRAMMER_NIC3COM |
| 58 | #endif |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 59 | #if CONFIG_NICREALTEK == 1 |
Joerg Fischer | 5665ef3 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 60 | PROGRAMMER_NICREALTEK |
| 61 | PROGRAMMER_NICREALTEK2 |
| 62 | #endif |
Andrew Morgan | c29c2e7 | 2010-06-07 22:37:54 +0000 | [diff] [blame] | 63 | #if CONFIG_NICNATSEMI == 1 |
| 64 | PROGRAMMER_NICNATSEMI |
| 65 | #endif |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 66 | #if CONFIG_GFXNVIDIA == 1 |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 67 | PROGRAMMER_GFXNVIDIA |
| 68 | #endif |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 69 | #if CONFIG_DRKAISER == 1 |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 70 | PROGRAMMER_DRKAISER |
| 71 | #endif |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 72 | #if CONFIG_SATASII == 1 |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 73 | PROGRAMMER_SATASII |
| 74 | #endif |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 75 | #if CONFIG_ATAHPT == 1 |
Uwe Hermann | ddd5c9e | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 76 | PROGRAMMER_ATAHPT |
| 77 | #endif |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 78 | #if CONFIG_FT2232_SPI == 1 |
| 79 | PROGRAMMER_FT2232_SPI |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 80 | #endif |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 81 | #if CONFIG_SERPROG == 1 |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 82 | PROGRAMMER_SERPROG |
| 83 | #endif |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 84 | #if CONFIG_BUSPIRATE_SPI == 1 |
| 85 | PROGRAMMER_BUSPIRATE_SPI |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 86 | #endif |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 87 | #if CONFIG_DEDIPROG == 1 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 88 | PROGRAMMER_DEDIPROG |
| 89 | #endif |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 90 | #if CONFIG_RAYER_SPI == 1 |
| 91 | PROGRAMMER_RAYER_SPI |
| 92 | #endif |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 93 | ; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 94 | #endif |
| 95 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 96 | static char *programmer_param = NULL; |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 97 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 98 | /* Supported buses for the current programmer. */ |
| 99 | enum chipbustype buses_supported; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Programmers supporting multiple buses can have differing size limits on |
| 103 | * each bus. Store the limits for each bus in a common struct. |
| 104 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 105 | struct decode_sizes max_rom_decode; |
| 106 | |
| 107 | /* If nonzero, used as the start address of bottom-aligned flash. */ |
| 108 | unsigned long flashbase; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 109 | |
Carl-Daniel Hailfinger | d1be52d | 2010-07-03 12:14:25 +0000 | [diff] [blame] | 110 | /* Is writing allowed with this programmer? */ |
| 111 | int programmer_may_write; |
| 112 | |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 113 | const struct programmer_entry programmer_table[] = { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 114 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 115 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 116 | .name = "internal", |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 117 | .init = internal_init, |
| 118 | .shutdown = internal_shutdown, |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 119 | .map_flash_region = physmap, |
| 120 | .unmap_flash_region = physunmap, |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 121 | .chip_readb = internal_chip_readb, |
| 122 | .chip_readw = internal_chip_readw, |
| 123 | .chip_readl = internal_chip_readl, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 124 | .chip_readn = internal_chip_readn, |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 125 | .chip_writeb = internal_chip_writeb, |
| 126 | .chip_writew = internal_chip_writew, |
| 127 | .chip_writel = internal_chip_writel, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 128 | .chip_writen = fallback_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 129 | .delay = internal_delay, |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 130 | }, |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 131 | #endif |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 132 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 133 | #if CONFIG_DUMMY == 1 |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 134 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 135 | .name = "dummy", |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 136 | .init = dummy_init, |
| 137 | .shutdown = dummy_shutdown, |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 138 | .map_flash_region = dummy_map, |
| 139 | .unmap_flash_region = dummy_unmap, |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 140 | .chip_readb = dummy_chip_readb, |
| 141 | .chip_readw = dummy_chip_readw, |
| 142 | .chip_readl = dummy_chip_readl, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 143 | .chip_readn = dummy_chip_readn, |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 144 | .chip_writeb = dummy_chip_writeb, |
| 145 | .chip_writew = dummy_chip_writew, |
| 146 | .chip_writel = dummy_chip_writel, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 147 | .chip_writen = dummy_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 148 | .delay = internal_delay, |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 149 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 150 | #endif |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 151 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 152 | #if CONFIG_NIC3COM == 1 |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 153 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 154 | .name = "nic3com", |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 155 | .init = nic3com_init, |
| 156 | .shutdown = nic3com_shutdown, |
Uwe Hermann | c691593 | 2009-05-17 23:12:17 +0000 | [diff] [blame] | 157 | .map_flash_region = fallback_map, |
| 158 | .unmap_flash_region = fallback_unmap, |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 159 | .chip_readb = nic3com_chip_readb, |
Carl-Daniel Hailfinger | 9ee1077 | 2009-05-16 01:23:55 +0000 | [diff] [blame] | 160 | .chip_readw = fallback_chip_readw, |
| 161 | .chip_readl = fallback_chip_readl, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 162 | .chip_readn = fallback_chip_readn, |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 163 | .chip_writeb = nic3com_chip_writeb, |
Carl-Daniel Hailfinger | 9ee1077 | 2009-05-16 01:23:55 +0000 | [diff] [blame] | 164 | .chip_writew = fallback_chip_writew, |
| 165 | .chip_writel = fallback_chip_writel, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 166 | .chip_writen = fallback_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 167 | .delay = internal_delay, |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 168 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 169 | #endif |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 170 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 171 | #if CONFIG_NICREALTEK == 1 |
Joerg Fischer | 5665ef3 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 172 | { |
| 173 | .name = "nicrealtek", |
| 174 | .init = nicrealtek_init, |
| 175 | .shutdown = nicrealtek_shutdown, |
| 176 | .map_flash_region = fallback_map, |
| 177 | .unmap_flash_region = fallback_unmap, |
| 178 | .chip_readb = nicrealtek_chip_readb, |
| 179 | .chip_readw = fallback_chip_readw, |
| 180 | .chip_readl = fallback_chip_readl, |
| 181 | .chip_readn = fallback_chip_readn, |
| 182 | .chip_writeb = nicrealtek_chip_writeb, |
| 183 | .chip_writew = fallback_chip_writew, |
| 184 | .chip_writel = fallback_chip_writel, |
| 185 | .chip_writen = fallback_chip_writen, |
| 186 | .delay = internal_delay, |
| 187 | }, |
| 188 | { |
| 189 | .name = "nicsmc1211", |
| 190 | .init = nicsmc1211_init, |
| 191 | .shutdown = nicrealtek_shutdown, |
| 192 | .map_flash_region = fallback_map, |
| 193 | .unmap_flash_region = fallback_unmap, |
| 194 | .chip_readb = nicrealtek_chip_readb, |
| 195 | .chip_readw = fallback_chip_readw, |
| 196 | .chip_readl = fallback_chip_readl, |
| 197 | .chip_readn = fallback_chip_readn, |
| 198 | .chip_writeb = nicrealtek_chip_writeb, |
| 199 | .chip_writew = fallback_chip_writew, |
| 200 | .chip_writel = fallback_chip_writel, |
| 201 | .chip_writen = fallback_chip_writen, |
| 202 | .delay = internal_delay, |
| 203 | }, |
| 204 | #endif |
| 205 | |
Andrew Morgan | c29c2e7 | 2010-06-07 22:37:54 +0000 | [diff] [blame] | 206 | #if CONFIG_NICNATSEMI == 1 |
| 207 | { |
| 208 | .name = "nicnatsemi", |
| 209 | .init = nicnatsemi_init, |
| 210 | .shutdown = nicnatsemi_shutdown, |
| 211 | .map_flash_region = fallback_map, |
| 212 | .unmap_flash_region = fallback_unmap, |
| 213 | .chip_readb = nicnatsemi_chip_readb, |
| 214 | .chip_readw = fallback_chip_readw, |
| 215 | .chip_readl = fallback_chip_readl, |
| 216 | .chip_readn = fallback_chip_readn, |
| 217 | .chip_writeb = nicnatsemi_chip_writeb, |
| 218 | .chip_writew = fallback_chip_writew, |
| 219 | .chip_writel = fallback_chip_writel, |
| 220 | .chip_writen = fallback_chip_writen, |
| 221 | .delay = internal_delay, |
| 222 | }, |
| 223 | #endif |
Joerg Fischer | 5665ef3 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 224 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 225 | #if CONFIG_GFXNVIDIA == 1 |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 226 | { |
| 227 | .name = "gfxnvidia", |
| 228 | .init = gfxnvidia_init, |
| 229 | .shutdown = gfxnvidia_shutdown, |
| 230 | .map_flash_region = fallback_map, |
| 231 | .unmap_flash_region = fallback_unmap, |
| 232 | .chip_readb = gfxnvidia_chip_readb, |
| 233 | .chip_readw = fallback_chip_readw, |
| 234 | .chip_readl = fallback_chip_readl, |
| 235 | .chip_readn = fallback_chip_readn, |
| 236 | .chip_writeb = gfxnvidia_chip_writeb, |
| 237 | .chip_writew = fallback_chip_writew, |
| 238 | .chip_writel = fallback_chip_writel, |
| 239 | .chip_writen = fallback_chip_writen, |
| 240 | .delay = internal_delay, |
| 241 | }, |
| 242 | #endif |
| 243 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 244 | #if CONFIG_DRKAISER == 1 |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 245 | { |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 246 | .name = "drkaiser", |
| 247 | .init = drkaiser_init, |
| 248 | .shutdown = drkaiser_shutdown, |
| 249 | .map_flash_region = fallback_map, |
| 250 | .unmap_flash_region = fallback_unmap, |
| 251 | .chip_readb = drkaiser_chip_readb, |
| 252 | .chip_readw = fallback_chip_readw, |
| 253 | .chip_readl = fallback_chip_readl, |
| 254 | .chip_readn = fallback_chip_readn, |
| 255 | .chip_writeb = drkaiser_chip_writeb, |
| 256 | .chip_writew = fallback_chip_writew, |
| 257 | .chip_writel = fallback_chip_writel, |
| 258 | .chip_writen = fallback_chip_writen, |
| 259 | .delay = internal_delay, |
| 260 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 261 | #endif |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 262 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 263 | #if CONFIG_SATASII == 1 |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 264 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 265 | .name = "satasii", |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 266 | .init = satasii_init, |
| 267 | .shutdown = satasii_shutdown, |
Uwe Hermann | c691593 | 2009-05-17 23:12:17 +0000 | [diff] [blame] | 268 | .map_flash_region = fallback_map, |
| 269 | .unmap_flash_region = fallback_unmap, |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 270 | .chip_readb = satasii_chip_readb, |
| 271 | .chip_readw = fallback_chip_readw, |
| 272 | .chip_readl = fallback_chip_readl, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 273 | .chip_readn = fallback_chip_readn, |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 274 | .chip_writeb = satasii_chip_writeb, |
| 275 | .chip_writew = fallback_chip_writew, |
| 276 | .chip_writel = fallback_chip_writel, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 277 | .chip_writen = fallback_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 278 | .delay = internal_delay, |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 279 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 280 | #endif |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 281 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 282 | #if CONFIG_ATAHPT == 1 |
Uwe Hermann | ddd5c9e | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 283 | { |
| 284 | .name = "atahpt", |
| 285 | .init = atahpt_init, |
| 286 | .shutdown = atahpt_shutdown, |
| 287 | .map_flash_region = fallback_map, |
| 288 | .unmap_flash_region = fallback_unmap, |
| 289 | .chip_readb = atahpt_chip_readb, |
| 290 | .chip_readw = fallback_chip_readw, |
| 291 | .chip_readl = fallback_chip_readl, |
| 292 | .chip_readn = fallback_chip_readn, |
| 293 | .chip_writeb = atahpt_chip_writeb, |
| 294 | .chip_writew = fallback_chip_writew, |
| 295 | .chip_writel = fallback_chip_writel, |
| 296 | .chip_writen = fallback_chip_writen, |
| 297 | .delay = internal_delay, |
| 298 | }, |
| 299 | #endif |
| 300 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 301 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 302 | #if defined(__i386__) || defined(__x86_64__) |
Carl-Daniel Hailfinger | b8afecd | 2009-05-31 18:00:57 +0000 | [diff] [blame] | 303 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 304 | .name = "it87spi", |
Carl-Daniel Hailfinger | b8afecd | 2009-05-31 18:00:57 +0000 | [diff] [blame] | 305 | .init = it87spi_init, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 306 | .shutdown = noop_shutdown, |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 307 | .map_flash_region = fallback_map, |
| 308 | .unmap_flash_region = fallback_unmap, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 309 | .chip_readb = noop_chip_readb, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 310 | .chip_readw = fallback_chip_readw, |
| 311 | .chip_readl = fallback_chip_readl, |
| 312 | .chip_readn = fallback_chip_readn, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 313 | .chip_writeb = noop_chip_writeb, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 314 | .chip_writew = fallback_chip_writew, |
| 315 | .chip_writel = fallback_chip_writel, |
| 316 | .chip_writen = fallback_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 317 | .delay = internal_delay, |
Carl-Daniel Hailfinger | b8afecd | 2009-05-31 18:00:57 +0000 | [diff] [blame] | 318 | }, |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 319 | #endif |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 320 | #endif |
Carl-Daniel Hailfinger | b8afecd | 2009-05-31 18:00:57 +0000 | [diff] [blame] | 321 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 322 | #if CONFIG_FT2232_SPI == 1 |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 323 | { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 324 | .name = "ft2232_spi", |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 325 | .init = ft2232_spi_init, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 326 | .shutdown = noop_shutdown, /* Missing shutdown */ |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 327 | .map_flash_region = fallback_map, |
| 328 | .unmap_flash_region = fallback_unmap, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 329 | .chip_readb = noop_chip_readb, |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 330 | .chip_readw = fallback_chip_readw, |
| 331 | .chip_readl = fallback_chip_readl, |
| 332 | .chip_readn = fallback_chip_readn, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 333 | .chip_writeb = noop_chip_writeb, |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 334 | .chip_writew = fallback_chip_writew, |
| 335 | .chip_writel = fallback_chip_writel, |
| 336 | .chip_writen = fallback_chip_writen, |
| 337 | .delay = internal_delay, |
| 338 | }, |
Carl-Daniel Hailfinger | 3426ef6 | 2009-08-19 13:27:58 +0000 | [diff] [blame] | 339 | #endif |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 340 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 341 | #if CONFIG_SERPROG == 1 |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 342 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 343 | .name = "serprog", |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 344 | .init = serprog_init, |
| 345 | .shutdown = serprog_shutdown, |
| 346 | .map_flash_region = fallback_map, |
| 347 | .unmap_flash_region = fallback_unmap, |
| 348 | .chip_readb = serprog_chip_readb, |
| 349 | .chip_readw = fallback_chip_readw, |
| 350 | .chip_readl = fallback_chip_readl, |
| 351 | .chip_readn = serprog_chip_readn, |
| 352 | .chip_writeb = serprog_chip_writeb, |
| 353 | .chip_writew = fallback_chip_writew, |
| 354 | .chip_writel = fallback_chip_writel, |
| 355 | .chip_writen = fallback_chip_writen, |
| 356 | .delay = serprog_delay, |
| 357 | }, |
Carl-Daniel Hailfinger | 6be7411 | 2009-08-12 16:17:41 +0000 | [diff] [blame] | 358 | #endif |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 359 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 360 | #if CONFIG_BUSPIRATE_SPI == 1 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 361 | { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 362 | .name = "buspirate_spi", |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 363 | .init = buspirate_spi_init, |
| 364 | .shutdown = buspirate_spi_shutdown, |
| 365 | .map_flash_region = fallback_map, |
| 366 | .unmap_flash_region = fallback_unmap, |
| 367 | .chip_readb = noop_chip_readb, |
| 368 | .chip_readw = fallback_chip_readw, |
| 369 | .chip_readl = fallback_chip_readl, |
| 370 | .chip_readn = fallback_chip_readn, |
| 371 | .chip_writeb = noop_chip_writeb, |
| 372 | .chip_writew = fallback_chip_writew, |
| 373 | .chip_writel = fallback_chip_writel, |
| 374 | .chip_writen = fallback_chip_writen, |
| 375 | .delay = internal_delay, |
| 376 | }, |
| 377 | #endif |
| 378 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 379 | #if CONFIG_DEDIPROG == 1 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 380 | { |
| 381 | .name = "dediprog", |
| 382 | .init = dediprog_init, |
| 383 | .shutdown = dediprog_shutdown, |
| 384 | .map_flash_region = fallback_map, |
| 385 | .unmap_flash_region = fallback_unmap, |
| 386 | .chip_readb = noop_chip_readb, |
| 387 | .chip_readw = fallback_chip_readw, |
| 388 | .chip_readl = fallback_chip_readl, |
| 389 | .chip_readn = fallback_chip_readn, |
| 390 | .chip_writeb = noop_chip_writeb, |
| 391 | .chip_writew = fallback_chip_writew, |
| 392 | .chip_writel = fallback_chip_writel, |
| 393 | .chip_writen = fallback_chip_writen, |
| 394 | .delay = internal_delay, |
| 395 | }, |
| 396 | #endif |
| 397 | |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 398 | #if CONFIG_RAYER_SPI == 1 |
| 399 | { |
| 400 | .name = "rayer_spi", |
| 401 | .init = rayer_spi_init, |
| 402 | .shutdown = noop_shutdown, |
| 403 | .map_flash_region = fallback_map, |
| 404 | .unmap_flash_region = fallback_unmap, |
| 405 | .chip_readb = noop_chip_readb, |
| 406 | .chip_readw = fallback_chip_readw, |
| 407 | .chip_readl = fallback_chip_readl, |
| 408 | .chip_readn = fallback_chip_readn, |
| 409 | .chip_writeb = noop_chip_writeb, |
| 410 | .chip_writew = fallback_chip_writew, |
| 411 | .chip_writel = fallback_chip_writel, |
| 412 | .chip_writen = fallback_chip_writen, |
| 413 | .delay = internal_delay, |
| 414 | }, |
| 415 | #endif |
| 416 | |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 417 | {}, /* This entry corresponds to PROGRAMMER_INVALID. */ |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 418 | }; |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 419 | |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 420 | #define SHUTDOWN_MAXFN 4 |
| 421 | static int shutdown_fn_count = 0; |
| 422 | struct shutdown_func_data { |
| 423 | void (*func) (void *data); |
| 424 | void *data; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 425 | } static shutdown_fn[SHUTDOWN_MAXFN]; |
| 426 | /* Initialize to 0 to make sure nobody registers a shutdown function before |
| 427 | * programmer init. |
| 428 | */ |
| 429 | static int may_register_shutdown = 0; |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 430 | |
| 431 | /* Register a function to be executed on programmer shutdown. |
| 432 | * The advantage over atexit() is that you can supply a void pointer which will |
| 433 | * be used as parameter to the registered function upon programmer shutdown. |
| 434 | * This pointer can point to arbitrary data used by said function, e.g. undo |
| 435 | * information for GPIO settings etc. If unneeded, set data=NULL. |
| 436 | * Please note that the first (void *data) belongs to the function signature of |
| 437 | * the function passed as first parameter. |
| 438 | */ |
| 439 | int register_shutdown(void (*function) (void *data), void *data) |
| 440 | { |
| 441 | if (shutdown_fn_count >= SHUTDOWN_MAXFN) { |
Carl-Daniel Hailfinger | 9f5f215 | 2010-06-04 23:20:21 +0000 | [diff] [blame] | 442 | msg_perr("Tried to register more than %i shutdown functions.\n", |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 443 | SHUTDOWN_MAXFN); |
| 444 | return 1; |
| 445 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 446 | if (!may_register_shutdown) { |
| 447 | msg_perr("Tried to register a shutdown function before " |
| 448 | "programmer init.\n"); |
| 449 | return 1; |
| 450 | } |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 451 | shutdown_fn[shutdown_fn_count].func = function; |
| 452 | shutdown_fn[shutdown_fn_count].data = data; |
| 453 | shutdown_fn_count++; |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 458 | int programmer_init(char *param) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 459 | { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 460 | int ret; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 461 | /* Initialize all programmer specific data. */ |
| 462 | /* Default to unlimited decode sizes. */ |
| 463 | max_rom_decode = (const struct decode_sizes) { |
| 464 | .parallel = 0xffffffff, |
| 465 | .lpc = 0xffffffff, |
| 466 | .fwh = 0xffffffff, |
| 467 | .spi = 0xffffffff |
| 468 | }; |
| 469 | /* Default to Parallel/LPC/FWH flash devices. If a known host controller |
| 470 | * is found, the init routine sets the buses_supported bitfield. |
| 471 | */ |
| 472 | buses_supported = CHIP_BUSTYPE_NONSPI; |
| 473 | /* Default to top aligned flash at 4 GB. */ |
| 474 | flashbase = 0; |
| 475 | /* Registering shutdown functions is now allowed. */ |
| 476 | may_register_shutdown = 1; |
Carl-Daniel Hailfinger | d1be52d | 2010-07-03 12:14:25 +0000 | [diff] [blame] | 477 | /* Default to allowing writes. Broken programmers set this to 0. */ |
| 478 | programmer_may_write = 1; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 479 | |
| 480 | programmer_param = param; |
| 481 | msg_pdbg("Initializing %s programmer\n", |
| 482 | programmer_table[programmer].name); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 483 | ret = programmer_table[programmer].init(); |
| 484 | if (programmer_param && strlen(programmer_param)) { |
| 485 | msg_perr("Unhandled programmer parameters: %s\n", |
| 486 | programmer_param); |
| 487 | /* Do not error out here, the init itself was successful. */ |
| 488 | } |
| 489 | return ret; |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | int programmer_shutdown(void) |
| 493 | { |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 494 | /* Registering shutdown functions is no longer allowed. */ |
| 495 | may_register_shutdown = 0; |
| 496 | while (shutdown_fn_count > 0) { |
| 497 | int i = --shutdown_fn_count; |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 498 | shutdown_fn[i].func(shutdown_fn[i].data); |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 499 | } |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 500 | return programmer_table[programmer].shutdown(); |
| 501 | } |
| 502 | |
| 503 | void *programmer_map_flash_region(const char *descr, unsigned long phys_addr, |
| 504 | size_t len) |
| 505 | { |
| 506 | return programmer_table[programmer].map_flash_region(descr, |
| 507 | phys_addr, len); |
| 508 | } |
| 509 | |
| 510 | void programmer_unmap_flash_region(void *virt_addr, size_t len) |
| 511 | { |
| 512 | programmer_table[programmer].unmap_flash_region(virt_addr, len); |
| 513 | } |
| 514 | |
| 515 | void chip_writeb(uint8_t val, chipaddr addr) |
| 516 | { |
| 517 | programmer_table[programmer].chip_writeb(val, addr); |
| 518 | } |
| 519 | |
| 520 | void chip_writew(uint16_t val, chipaddr addr) |
| 521 | { |
| 522 | programmer_table[programmer].chip_writew(val, addr); |
| 523 | } |
| 524 | |
| 525 | void chip_writel(uint32_t val, chipaddr addr) |
| 526 | { |
| 527 | programmer_table[programmer].chip_writel(val, addr); |
| 528 | } |
| 529 | |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 530 | void chip_writen(uint8_t *buf, chipaddr addr, size_t len) |
| 531 | { |
| 532 | programmer_table[programmer].chip_writen(buf, addr, len); |
| 533 | } |
| 534 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 535 | uint8_t chip_readb(const chipaddr addr) |
| 536 | { |
| 537 | return programmer_table[programmer].chip_readb(addr); |
| 538 | } |
| 539 | |
| 540 | uint16_t chip_readw(const chipaddr addr) |
| 541 | { |
| 542 | return programmer_table[programmer].chip_readw(addr); |
| 543 | } |
| 544 | |
| 545 | uint32_t chip_readl(const chipaddr addr) |
| 546 | { |
| 547 | return programmer_table[programmer].chip_readl(addr); |
| 548 | } |
| 549 | |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 550 | void chip_readn(uint8_t *buf, chipaddr addr, size_t len) |
| 551 | { |
| 552 | programmer_table[programmer].chip_readn(buf, addr, len); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 555 | void programmer_delay(int usecs) |
| 556 | { |
| 557 | programmer_table[programmer].delay(usecs); |
| 558 | } |
| 559 | |
Peter Stuge | 776d202 | 2009-01-26 00:39:57 +0000 | [diff] [blame] | 560 | void map_flash_registers(struct flashchip *flash) |
Stefan Reinauer | ff4f197 | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 561 | { |
Stefan Reinauer | ff4f197 | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 562 | size_t size = flash->total_size * 1024; |
Carl-Daniel Hailfinger | d0fc946 | 2009-05-11 14:01:17 +0000 | [diff] [blame] | 563 | /* Flash registers live 4 MByte below the flash. */ |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 564 | /* FIXME: This is incorrect for nonstandard flashbase. */ |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 565 | flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size); |
Stefan Reinauer | ff4f197 | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 568 | int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len) |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 569 | { |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 570 | chip_readn(buf, flash->virtual_memory + start, len); |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
Carl-Daniel Hailfinger | 38a059d | 2009-06-13 12:04:03 +0000 | [diff] [blame] | 575 | int min(int a, int b) |
| 576 | { |
| 577 | return (a < b) ? a : b; |
| 578 | } |
| 579 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 580 | int max(int a, int b) |
| 581 | { |
| 582 | return (a > b) ? a : b; |
| 583 | } |
| 584 | |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 585 | int bitcount(unsigned long a) |
| 586 | { |
| 587 | int i = 0; |
| 588 | for (; a != 0; a >>= 1) |
| 589 | if (a & 1) |
| 590 | i++; |
| 591 | return i; |
| 592 | } |
| 593 | |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 594 | char *strcat_realloc(char *dest, const char *src) |
| 595 | { |
| 596 | dest = realloc(dest, strlen(dest) + strlen(src) + 1); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 597 | if (!dest) { |
| 598 | msg_gerr("Out of memory!\n"); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 599 | return NULL; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 600 | } |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 601 | strcat(dest, src); |
| 602 | return dest; |
| 603 | } |
| 604 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 605 | /* This is a somewhat hacked function similar in some ways to strtok(). |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 606 | * It will look for needle with a subsequent '=' in haystack, return a copy of |
| 607 | * needle and remove everything from the first occurrence of needle to the next |
| 608 | * delimiter from haystack. |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 609 | */ |
| 610 | char *extract_param(char **haystack, char *needle, char *delim) |
| 611 | { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 612 | char *param_pos, *opt_pos, *rest; |
| 613 | char *opt = NULL; |
| 614 | int optlen; |
Carl-Daniel Hailfinger | 2702376 | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 615 | int needlelen; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 616 | |
Carl-Daniel Hailfinger | 2702376 | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 617 | needlelen = strlen(needle); |
| 618 | if (!needlelen) { |
| 619 | msg_gerr("%s: empty needle! Please report a bug at " |
| 620 | "flashrom@flashrom.org\n", __func__); |
| 621 | return NULL; |
| 622 | } |
| 623 | /* No programmer parameters given. */ |
| 624 | if (*haystack == NULL) |
| 625 | return NULL; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 626 | param_pos = strstr(*haystack, needle); |
| 627 | do { |
| 628 | if (!param_pos) |
| 629 | return NULL; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 630 | /* Needle followed by '='? */ |
| 631 | if (param_pos[needlelen] == '=') { |
| 632 | |
| 633 | /* Beginning of the string? */ |
| 634 | if (param_pos == *haystack) |
| 635 | break; |
| 636 | /* After a delimiter? */ |
| 637 | if (strchr(delim, *(param_pos - 1))) |
| 638 | break; |
| 639 | } |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 640 | /* Continue searching. */ |
| 641 | param_pos++; |
| 642 | param_pos = strstr(param_pos, needle); |
| 643 | } while (1); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 644 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 645 | if (param_pos) { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 646 | /* Get the string after needle and '='. */ |
| 647 | opt_pos = param_pos + needlelen + 1; |
| 648 | optlen = strcspn(opt_pos, delim); |
| 649 | /* Return an empty string if the parameter was empty. */ |
| 650 | opt = malloc(optlen + 1); |
| 651 | if (!opt) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 652 | msg_gerr("Out of memory!\n"); |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 653 | exit(1); |
| 654 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 655 | strncpy(opt, opt_pos, optlen); |
| 656 | opt[optlen] = '\0'; |
| 657 | rest = opt_pos + optlen; |
| 658 | /* Skip all delimiters after the current parameter. */ |
| 659 | rest += strspn(rest, delim); |
| 660 | memmove(param_pos, rest, strlen(rest) + 1); |
| 661 | /* We could shrink haystack, but the effort is not worth it. */ |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 662 | } |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 663 | |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 664 | return opt; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 667 | char *extract_programmer_param(char *param_name) |
| 668 | { |
| 669 | return extract_param(&programmer_param, param_name, ","); |
| 670 | } |
| 671 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 672 | /* start is an offset to the base address of the flash chip */ |
| 673 | int check_erased_range(struct flashchip *flash, int start, int len) |
| 674 | { |
| 675 | int ret; |
| 676 | uint8_t *cmpbuf = malloc(len); |
| 677 | |
| 678 | if (!cmpbuf) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 679 | msg_gerr("Could not allocate memory!\n"); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 680 | exit(1); |
| 681 | } |
| 682 | memset(cmpbuf, 0xff, len); |
| 683 | ret = verify_range(flash, cmpbuf, start, len, "ERASE"); |
| 684 | free(cmpbuf); |
| 685 | return ret; |
| 686 | } |
| 687 | |
| 688 | /** |
Carl-Daniel Hailfinger | d0250a3 | 2009-11-25 17:05:52 +0000 | [diff] [blame] | 689 | * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the |
| 690 | flash content at location start |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 691 | * @start offset to the base address of the flash chip |
| 692 | * @len length of the verified area |
| 693 | * @message string to print in the "FAILED" message |
| 694 | * @return 0 for success, -1 for failure |
| 695 | */ |
| 696 | int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message) |
| 697 | { |
| 698 | int i, j, starthere, lenhere, ret = 0; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 699 | int page_size = flash->page_size; |
| 700 | uint8_t *readbuf = malloc(page_size); |
Carl-Daniel Hailfinger | 49b9cab | 2009-07-23 01:42:56 +0000 | [diff] [blame] | 701 | int failcount = 0; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 702 | |
| 703 | if (!len) |
| 704 | goto out_free; |
| 705 | |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 706 | if (!flash->read) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 707 | msg_cerr("ERROR: flashrom has no read function for this flash chip.\n"); |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 708 | return 1; |
| 709 | } |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 710 | if (!readbuf) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 711 | msg_gerr("Could not allocate memory!\n"); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 712 | exit(1); |
| 713 | } |
| 714 | |
| 715 | if (start + len > flash->total_size * 1024) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 716 | msg_gerr("Error: %s called with start 0x%x + len 0x%x >" |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 717 | " total_size 0x%x\n", __func__, start, len, |
| 718 | flash->total_size * 1024); |
| 719 | ret = -1; |
| 720 | goto out_free; |
| 721 | } |
| 722 | if (!message) |
| 723 | message = "VERIFY"; |
| 724 | |
| 725 | /* Warning: This loop has a very unusual condition and body. |
| 726 | * The loop needs to go through each page with at least one affected |
| 727 | * byte. The lowest page number is (start / page_size) since that |
| 728 | * division rounds down. The highest page number we want is the page |
| 729 | * where the last byte of the range lives. That last byte has the |
| 730 | * address (start + len - 1), thus the highest page number is |
| 731 | * (start + len - 1) / page_size. Since we want to include that last |
| 732 | * page as well, the loop condition uses <=. |
| 733 | */ |
| 734 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 735 | /* Byte position of the first byte in the range in this page. */ |
| 736 | starthere = max(start, i * page_size); |
| 737 | /* Length of bytes in the range in this page. */ |
| 738 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 739 | ret = flash->read(flash, readbuf, starthere, lenhere); |
| 740 | if (ret) { |
| 741 | msg_gerr("Verification impossible because read failed " |
| 742 | "at 0x%x (len 0x%x)\n", starthere, lenhere); |
| 743 | break; |
| 744 | } |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 745 | for (j = 0; j < lenhere; j++) { |
| 746 | if (cmpbuf[starthere - start + j] != readbuf[j]) { |
Carl-Daniel Hailfinger | 49b9cab | 2009-07-23 01:42:56 +0000 | [diff] [blame] | 747 | /* Only print the first failure. */ |
| 748 | if (!failcount++) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 749 | msg_cerr("%s FAILED at 0x%08x! " |
Carl-Daniel Hailfinger | 49b9cab | 2009-07-23 01:42:56 +0000 | [diff] [blame] | 750 | "Expected=0x%02x, Read=0x%02x,", |
| 751 | message, starthere + j, |
| 752 | cmpbuf[starthere - start + j], |
| 753 | readbuf[j]); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | } |
Carl-Daniel Hailfinger | 49b9cab | 2009-07-23 01:42:56 +0000 | [diff] [blame] | 757 | if (failcount) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 758 | msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n", |
Carl-Daniel Hailfinger | 49b9cab | 2009-07-23 01:42:56 +0000 | [diff] [blame] | 759 | start, start + len - 1, failcount); |
| 760 | ret = -1; |
| 761 | } |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 762 | |
| 763 | out_free: |
| 764 | free(readbuf); |
| 765 | return ret; |
| 766 | } |
| 767 | |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 768 | /** |
| 769 | * Check if the buffer @have can be programmed to the content of @want without |
| 770 | * erasing. This is only possible if all chunks of size @gran are either kept |
| 771 | * as-is or changed from an all-ones state to any other state. |
| 772 | * The following write granularities (enum @gran) are known: |
| 773 | * - 1 bit. Each bit can be cleared individually. |
| 774 | * - 1 byte. A byte can be written once. Further writes to an already written |
| 775 | * byte cause the contents to be either undefined or to stay unchanged. |
| 776 | * - 128 bytes. If less than 128 bytes are written, the rest will be |
| 777 | * erased. Each write to a 128-byte region will trigger an automatic erase |
| 778 | * before anything is written. Very uncommon behaviour and unsupported by |
| 779 | * this function. |
| 780 | * - 256 bytes. If less than 256 bytes are written, the contents of the |
| 781 | * unwritten bytes are undefined. |
| 782 | * |
| 783 | * @have buffer with current content |
| 784 | * @want buffer with desired content |
| 785 | * @len length of the verified area |
| 786 | * @gran write granularity (enum, not count) |
| 787 | * @return 0 if no erase is needed, 1 otherwise |
| 788 | */ |
| 789 | int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran) |
| 790 | { |
| 791 | int result = 0; |
| 792 | int i, j, limit; |
| 793 | |
| 794 | switch (gran) { |
| 795 | case write_gran_1bit: |
| 796 | for (i = 0; i < len; i++) |
| 797 | if ((have[i] & want[i]) != want[i]) { |
| 798 | result = 1; |
| 799 | break; |
| 800 | } |
| 801 | break; |
| 802 | case write_gran_1byte: |
| 803 | for (i = 0; i < len; i++) |
| 804 | if ((have[i] != want[i]) && (have[i] != 0xff)) { |
| 805 | result = 1; |
| 806 | break; |
| 807 | } |
| 808 | break; |
| 809 | case write_gran_256bytes: |
| 810 | for (j = 0; j < len / 256; j++) { |
| 811 | limit = min (256, len - j * 256); |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 812 | /* Are 'have' and 'want' identical? */ |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 813 | if (!memcmp(have + j * 256, want + j * 256, limit)) |
| 814 | continue; |
| 815 | /* have needs to be in erased state. */ |
| 816 | for (i = 0; i < limit; i++) |
| 817 | if (have[i] != 0xff) { |
| 818 | result = 1; |
| 819 | break; |
| 820 | } |
| 821 | if (result) |
| 822 | break; |
| 823 | } |
| 824 | break; |
| 825 | } |
| 826 | return result; |
| 827 | } |
| 828 | |
Carl-Daniel Hailfinger | eaac68b | 2009-11-23 12:55:31 +0000 | [diff] [blame] | 829 | /* This function generates various test patterns useful for testing controller |
| 830 | * and chip communication as well as chip behaviour. |
| 831 | * |
| 832 | * If a byte can be written multiple times, each time keeping 0-bits at 0 |
| 833 | * and changing 1-bits to 0 if the new value for that bit is 0, the effect |
| 834 | * is essentially an AND operation. That's also the reason why this function |
| 835 | * provides the result of AND between various patterns. |
| 836 | * |
| 837 | * Below is a list of patterns (and their block length). |
| 838 | * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes) |
| 839 | * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes) |
| 840 | * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes) |
| 841 | * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes) |
| 842 | * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes) |
| 843 | * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes) |
| 844 | * Pattern 6 is 00 (1 Byte) |
| 845 | * Pattern 7 is ff (1 Byte) |
| 846 | * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256 |
| 847 | * byte block. |
| 848 | * |
| 849 | * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B) |
| 850 | * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B) |
| 851 | * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter) |
| 852 | * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards) |
| 853 | * Pattern 12 is 00 (1 Byte) |
| 854 | * Pattern 13 is ff (1 Byte) |
| 855 | * Patterns 8-13 have no block number. |
| 856 | * |
| 857 | * Patterns 0-3 are created to detect and efficiently diagnose communication |
| 858 | * slips like missed bits or bytes and their repetitive nature gives good visual |
| 859 | * cues to the person inspecting the results. In addition, the following holds: |
| 860 | * AND Pattern 0/1 == Pattern 4 |
| 861 | * AND Pattern 2/3 == Pattern 5 |
| 862 | * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6 |
| 863 | * A weakness of pattern 0-5 is the inability to detect swaps/copies between |
| 864 | * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc. |
| 865 | * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes. |
| 866 | * 0x5 and 0xa were picked because they are 0101 and 1010 binary. |
| 867 | * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes. |
| 868 | * Besides that, they provide for bit testing of the last two bytes of every |
| 869 | * 256 byte block which contains the block number for patterns 0-6. |
| 870 | * Patterns 10-11 are special purpose for detecting subblock aliasing with |
| 871 | * block sizes >256 bytes (some Dataflash chips etc.) |
| 872 | * AND Pattern 8/9 == Pattern 12 |
| 873 | * AND Pattern 10/11 == Pattern 12 |
| 874 | * Pattern 13 is the completely erased state. |
| 875 | * None of the patterns can detect aliasing at boundaries which are a multiple |
| 876 | * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI). |
| 877 | */ |
| 878 | int generate_testpattern(uint8_t *buf, uint32_t size, int variant) |
| 879 | { |
| 880 | int i; |
| 881 | |
| 882 | if (!buf) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 883 | msg_gerr("Invalid buffer!\n"); |
Carl-Daniel Hailfinger | eaac68b | 2009-11-23 12:55:31 +0000 | [diff] [blame] | 884 | return 1; |
| 885 | } |
| 886 | |
| 887 | switch (variant) { |
| 888 | case 0: |
| 889 | for (i = 0; i < size; i++) |
| 890 | buf[i] = (i & 0xf) << 4 | 0x5; |
| 891 | break; |
| 892 | case 1: |
| 893 | for (i = 0; i < size; i++) |
| 894 | buf[i] = (i & 0xf) << 4 | 0xa; |
| 895 | break; |
| 896 | case 2: |
| 897 | for (i = 0; i < size; i++) |
| 898 | buf[i] = 0x50 | (i & 0xf); |
| 899 | break; |
| 900 | case 3: |
| 901 | for (i = 0; i < size; i++) |
| 902 | buf[i] = 0xa0 | (i & 0xf); |
| 903 | break; |
| 904 | case 4: |
| 905 | for (i = 0; i < size; i++) |
| 906 | buf[i] = (i & 0xf) << 4; |
| 907 | break; |
| 908 | case 5: |
| 909 | for (i = 0; i < size; i++) |
| 910 | buf[i] = i & 0xf; |
| 911 | break; |
| 912 | case 6: |
| 913 | memset(buf, 0x00, size); |
| 914 | break; |
| 915 | case 7: |
| 916 | memset(buf, 0xff, size); |
| 917 | break; |
| 918 | case 8: |
| 919 | for (i = 0; i < size; i++) |
| 920 | buf[i] = i & 0xff; |
| 921 | break; |
| 922 | case 9: |
| 923 | for (i = 0; i < size; i++) |
| 924 | buf[i] = ~(i & 0xff); |
| 925 | break; |
| 926 | case 10: |
| 927 | for (i = 0; i < size % 2; i++) { |
| 928 | buf[i * 2] = (i >> 8) & 0xff; |
| 929 | buf[i * 2 + 1] = i & 0xff; |
| 930 | } |
| 931 | if (size & 0x1) |
| 932 | buf[i * 2] = (i >> 8) & 0xff; |
| 933 | break; |
| 934 | case 11: |
| 935 | for (i = 0; i < size % 2; i++) { |
| 936 | buf[i * 2] = ~((i >> 8) & 0xff); |
| 937 | buf[i * 2 + 1] = ~(i & 0xff); |
| 938 | } |
| 939 | if (size & 0x1) |
| 940 | buf[i * 2] = ~((i >> 8) & 0xff); |
| 941 | break; |
| 942 | case 12: |
| 943 | memset(buf, 0x00, size); |
| 944 | break; |
| 945 | case 13: |
| 946 | memset(buf, 0xff, size); |
| 947 | break; |
| 948 | } |
| 949 | |
| 950 | if ((variant >= 0) && (variant <= 7)) { |
| 951 | /* Write block number in the last two bytes of each 256-byte |
| 952 | * block, big endian for easier reading of the hexdump. |
| 953 | * Note that this wraps around for chips larger than 2^24 bytes |
| 954 | * (16 MB). |
| 955 | */ |
| 956 | for (i = 0; i < size / 256; i++) { |
| 957 | buf[i * 256 + 254] = (i >> 8) & 0xff; |
| 958 | buf[i * 256 + 255] = i & 0xff; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | return 0; |
| 963 | } |
| 964 | |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 965 | int check_max_decode(enum chipbustype buses, uint32_t size) |
| 966 | { |
| 967 | int limitexceeded = 0; |
| 968 | if ((buses & CHIP_BUSTYPE_PARALLEL) && |
| 969 | (max_rom_decode.parallel < size)) { |
| 970 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 971 | msg_pdbg("Chip size %u kB is bigger than supported " |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 972 | "size %u kB of chipset/board/programmer " |
| 973 | "for %s interface, " |
| 974 | "probe/read/erase/write may fail. ", size / 1024, |
| 975 | max_rom_decode.parallel / 1024, "Parallel"); |
| 976 | } |
| 977 | if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) { |
| 978 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 979 | msg_pdbg("Chip size %u kB is bigger than supported " |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 980 | "size %u kB of chipset/board/programmer " |
| 981 | "for %s interface, " |
| 982 | "probe/read/erase/write may fail. ", size / 1024, |
| 983 | max_rom_decode.lpc / 1024, "LPC"); |
| 984 | } |
| 985 | if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) { |
| 986 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 987 | msg_pdbg("Chip size %u kB is bigger than supported " |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 988 | "size %u kB of chipset/board/programmer " |
| 989 | "for %s interface, " |
| 990 | "probe/read/erase/write may fail. ", size / 1024, |
| 991 | max_rom_decode.fwh / 1024, "FWH"); |
| 992 | } |
| 993 | if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) { |
| 994 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 995 | msg_pdbg("Chip size %u kB is bigger than supported " |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 996 | "size %u kB of chipset/board/programmer " |
| 997 | "for %s interface, " |
| 998 | "probe/read/erase/write may fail. ", size / 1024, |
| 999 | max_rom_decode.spi / 1024, "SPI"); |
| 1000 | } |
| 1001 | if (!limitexceeded) |
| 1002 | return 0; |
| 1003 | /* Sometimes chip and programmer have more than one bus in common, |
| 1004 | * and the limit is not exceeded on all buses. Tell the user. |
| 1005 | */ |
| 1006 | if (bitcount(buses) > limitexceeded) |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 1007 | /* FIXME: This message is designed towards CLI users. */ |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1008 | msg_pdbg("There is at least one common chip/programmer " |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1009 | "interface which can support a chip of this size. " |
| 1010 | "You can try --force at your own risk.\n"); |
| 1011 | return 1; |
| 1012 | } |
| 1013 | |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 1014 | struct flashchip *probe_flash(struct flashchip *first_flash, int force) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1015 | { |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 1016 | struct flashchip *flash; |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1017 | unsigned long base = 0; |
| 1018 | uint32_t size; |
| 1019 | enum chipbustype buses_common; |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1020 | char *tmp; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1021 | |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 1022 | for (flash = first_flash; flash && flash->name; flash++) { |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1023 | if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1024 | continue; |
Carl-Daniel Hailfinger | 12575e5 | 2010-03-22 23:43:51 +0000 | [diff] [blame] | 1025 | msg_gdbg("Probing for %s %s, %d KB: ", |
Stefan Reinauer | ac37897 | 2008-03-17 22:59:40 +0000 | [diff] [blame] | 1026 | flash->vendor, flash->name, flash->total_size); |
Peter Stuge | 7ffbc6f | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 1027 | if (!flash->probe && !force) { |
Carl-Daniel Hailfinger | 12575e5 | 2010-03-22 23:43:51 +0000 | [diff] [blame] | 1028 | msg_gdbg("failed! flashrom has no probe function for " |
| 1029 | "this flash chip.\n"); |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1030 | continue; |
| 1031 | } |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1032 | buses_common = buses_supported & flash->bustype; |
| 1033 | if (!buses_common) { |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1034 | tmp = flashbuses_to_text(buses_supported); |
Carl-Daniel Hailfinger | 12575e5 | 2010-03-22 23:43:51 +0000 | [diff] [blame] | 1035 | msg_gdbg("skipped."); |
| 1036 | msg_gspew(" Host bus type %s ", tmp); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1037 | free(tmp); |
| 1038 | tmp = flashbuses_to_text(flash->bustype); |
Carl-Daniel Hailfinger | 12575e5 | 2010-03-22 23:43:51 +0000 | [diff] [blame] | 1039 | msg_gspew("and chip bus type %s are incompatible.", |
| 1040 | tmp); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1041 | free(tmp); |
Carl-Daniel Hailfinger | 12575e5 | 2010-03-22 23:43:51 +0000 | [diff] [blame] | 1042 | msg_gdbg("\n"); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1043 | continue; |
| 1044 | } |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 1045 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1046 | size = flash->total_size * 1024; |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1047 | check_max_decode(buses_common, size); |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 1048 | |
Carl-Daniel Hailfinger | 97d6b09 | 2009-05-09 07:27:23 +0000 | [diff] [blame] | 1049 | base = flashbase ? flashbase : (0xffffffff - size + 1); |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 1050 | flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1051 | |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1052 | if (force) |
| 1053 | break; |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 1054 | |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 1055 | if (flash->probe(flash) != 1) |
| 1056 | goto notfound; |
| 1057 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 1058 | if (first_flash == flashchips |
| 1059 | || flash->model_id != GENERIC_DEVICE_ID) |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1060 | break; |
| 1061 | |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 1062 | notfound: |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 1063 | programmer_unmap_flash_region((void *)flash->virtual_memory, size); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1064 | } |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 1065 | |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1066 | if (!flash || !flash->name) |
| 1067 | return NULL; |
| 1068 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1069 | msg_cinfo("%s chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n", |
Carl-Daniel Hailfinger | 2702376 | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 1070 | force ? "Assuming" : "Found", |
Uwe Hermann | 9899cad | 2009-06-28 21:47:57 +0000 | [diff] [blame] | 1071 | flash->vendor, flash->name, flash->total_size, |
| 1072 | flashbuses_to_text(flash->bustype), base); |
| 1073 | |
Sean Nelson | 6e0b912 | 2010-02-19 00:52:10 +0000 | [diff] [blame] | 1074 | if (flash->printlock) |
| 1075 | flash->printlock(flash); |
| 1076 | |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1077 | return flash; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Stefan Reinauer | e370528 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 1080 | int verify_flash(struct flashchip *flash, uint8_t *buf) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1081 | { |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 1082 | int ret; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 1083 | int total_size = flash->total_size * 1024; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1084 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1085 | msg_cinfo("Verifying flash... "); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1086 | |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 1087 | ret = verify_range(flash, buf, 0, total_size, NULL); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1088 | |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 1089 | if (!ret) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1090 | msg_cinfo("VERIFIED. \n"); |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 1091 | |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 1092 | return ret; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1095 | int write_buf_to_file(unsigned char *buf, unsigned long size, char *filename) |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1096 | { |
| 1097 | unsigned long numbytes; |
| 1098 | FILE *image; |
Stephan Guilloux | 21dd55b | 2009-06-01 22:07:52 +0000 | [diff] [blame] | 1099 | |
| 1100 | if (!filename) { |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1101 | msg_gerr("No filename specified.\n"); |
Stephan Guilloux | 21dd55b | 2009-06-01 22:07:52 +0000 | [diff] [blame] | 1102 | return 1; |
| 1103 | } |
Patrick Georgi | 0bf842d | 2010-01-25 22:55:33 +0000 | [diff] [blame] | 1104 | if ((image = fopen(filename, "wb")) == NULL) { |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1105 | perror(filename); |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 1106 | return 1; |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1107 | } |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1108 | |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1109 | numbytes = fwrite(buf, 1, size, image); |
| 1110 | fclose(image); |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1111 | if (numbytes != size) { |
| 1112 | msg_gerr("File %s could not be written completely.\n", |
| 1113 | filename); |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1114 | return 1; |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1115 | } |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1116 | return 0; |
| 1117 | } |
| 1118 | |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1119 | int read_flash_to_file(struct flashchip *flash, char *filename) |
| 1120 | { |
| 1121 | unsigned long size = flash->total_size * 1024; |
| 1122 | unsigned char *buf = calloc(size, sizeof(char)); |
| 1123 | int ret = 0; |
| 1124 | |
| 1125 | msg_cinfo("Reading flash... "); |
| 1126 | if (!buf) { |
| 1127 | msg_gerr("Memory allocation failed!\n"); |
| 1128 | msg_cinfo("FAILED.\n"); |
| 1129 | return 1; |
| 1130 | } |
| 1131 | if (!flash->read) { |
| 1132 | msg_cerr("No read function available for this flash chip.\n"); |
| 1133 | ret = 1; |
| 1134 | goto out_free; |
| 1135 | } |
| 1136 | if (flash->read(flash, buf, 0, size)) { |
| 1137 | msg_cerr("Read operation failed!\n"); |
| 1138 | ret = 1; |
| 1139 | goto out_free; |
| 1140 | } |
| 1141 | |
| 1142 | ret = write_buf_to_file(buf, flash->total_size * 1024, filename); |
| 1143 | out_free: |
| 1144 | free(buf); |
| 1145 | msg_cinfo("%s.\n", ret ? "FAILED" : "done"); |
| 1146 | return ret; |
| 1147 | } |
| 1148 | |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1149 | /* This function shares a lot of its structure with erase_flash(). |
| 1150 | * Even if an error is found, the function will keep going and check the rest. |
| 1151 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 1152 | static int selfcheck_eraseblocks(struct flashchip *flash) |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1153 | { |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1154 | int i, j, k; |
| 1155 | int ret = 0; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1156 | |
| 1157 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
| 1158 | unsigned int done = 0; |
| 1159 | struct block_eraser eraser = flash->block_erasers[k]; |
| 1160 | |
| 1161 | for (i = 0; i < NUM_ERASEREGIONS; i++) { |
| 1162 | /* Blocks with zero size are bugs in flashchips.c. */ |
| 1163 | if (eraser.eraseblocks[i].count && |
| 1164 | !eraser.eraseblocks[i].size) { |
| 1165 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1166 | "%i region %i has size 0. Please report" |
| 1167 | " a bug at flashrom@flashrom.org\n", |
| 1168 | flash->name, k, i); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1169 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1170 | } |
| 1171 | /* Blocks with zero count are bugs in flashchips.c. */ |
| 1172 | if (!eraser.eraseblocks[i].count && |
| 1173 | eraser.eraseblocks[i].size) { |
| 1174 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1175 | "%i region %i has count 0. Please report" |
| 1176 | " a bug at flashrom@flashrom.org\n", |
| 1177 | flash->name, k, i); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1178 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1179 | } |
| 1180 | done += eraser.eraseblocks[i].count * |
| 1181 | eraser.eraseblocks[i].size; |
| 1182 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1183 | /* Empty eraseblock definition with erase function. */ |
| 1184 | if (!done && eraser.block_erase) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1185 | msg_gspew("Strange: Empty eraseblock definition with " |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1186 | "non-empty erase function. Not an error.\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1187 | if (!done) |
| 1188 | continue; |
| 1189 | if (done != flash->total_size * 1024) { |
| 1190 | msg_gerr("ERROR: Flash chip %s erase function %i " |
| 1191 | "region walking resulted in 0x%06x bytes total," |
| 1192 | " expected 0x%06x bytes. Please report a bug at" |
| 1193 | " flashrom@flashrom.org\n", flash->name, k, |
| 1194 | done, flash->total_size * 1024); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1195 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1196 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1197 | if (!eraser.block_erase) |
| 1198 | continue; |
| 1199 | /* Check if there are identical erase functions for different |
| 1200 | * layouts. That would imply "magic" erase functions. The |
| 1201 | * easiest way to check this is with function pointers. |
| 1202 | */ |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 1203 | for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) { |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1204 | if (eraser.block_erase == |
| 1205 | flash->block_erasers[j].block_erase) { |
| 1206 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1207 | "%i and %i are identical. Please report" |
| 1208 | " a bug at flashrom@flashrom.org\n", |
| 1209 | flash->name, k, j); |
| 1210 | ret = 1; |
| 1211 | } |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 1212 | } |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1213 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1214 | return ret; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Carl-Daniel Hailfinger | 4d3e9ca | 2010-07-13 00:42:00 +0000 | [diff] [blame] | 1217 | static int walk_eraseregions(struct flashchip *flash, int erasefunction, |
| 1218 | int (*do_something) (struct flashchip *flash, |
| 1219 | unsigned int addr, |
| 1220 | unsigned int len)) |
Carl-Daniel Hailfinger | a122341 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1221 | { |
| 1222 | int i, j; |
| 1223 | unsigned int start = 0; |
| 1224 | unsigned int len; |
| 1225 | struct block_eraser eraser = flash->block_erasers[erasefunction]; |
| 1226 | for (i = 0; i < NUM_ERASEREGIONS; i++) { |
| 1227 | /* count==0 for all automatically initialized array |
| 1228 | * members so the loop below won't be executed for them. |
| 1229 | */ |
| 1230 | len = eraser.eraseblocks[i].size; |
| 1231 | for (j = 0; j < eraser.eraseblocks[i].count; j++) { |
| 1232 | msg_cdbg("0x%06x-0x%06x, ", start, |
| 1233 | start + len - 1); |
| 1234 | if (do_something(flash, start, len)) |
| 1235 | return 1; |
| 1236 | start += len; |
| 1237 | } |
| 1238 | } |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1242 | int erase_flash(struct flashchip *flash) |
| 1243 | { |
Carl-Daniel Hailfinger | a122341 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1244 | int k, ret = 0, found = 0; |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1245 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1246 | msg_cinfo("Erasing flash chip... "); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1247 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1248 | struct block_eraser eraser = flash->block_erasers[k]; |
| 1249 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1250 | msg_cdbg("Looking at blockwise erase function %i... ", k); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1251 | if (!eraser.block_erase && !eraser.eraseblocks[0].count) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1252 | msg_cdbg("not defined. " |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1253 | "Looking for another erase function.\n"); |
| 1254 | continue; |
| 1255 | } |
| 1256 | if (!eraser.block_erase && eraser.eraseblocks[0].count) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1257 | msg_cdbg("eraseblock layout is known, but no " |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1258 | "matching block erase function found. " |
| 1259 | "Looking for another erase function.\n"); |
| 1260 | continue; |
| 1261 | } |
| 1262 | if (eraser.block_erase && !eraser.eraseblocks[0].count) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1263 | msg_cdbg("block erase function found, but " |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1264 | "eraseblock layout is unknown. " |
| 1265 | "Looking for another erase function.\n"); |
| 1266 | continue; |
| 1267 | } |
| 1268 | found = 1; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1269 | msg_cdbg("trying... "); |
Carl-Daniel Hailfinger | a122341 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1270 | ret = walk_eraseregions(flash, k, eraser.block_erase); |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1271 | msg_cdbg("\n"); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1272 | /* If everything is OK, don't try another erase function. */ |
| 1273 | if (!ret) |
| 1274 | break; |
| 1275 | } |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1276 | if (!found) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1277 | msg_cerr("ERROR: flashrom has no erase function for this flash chip.\n"); |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1278 | return 1; |
| 1279 | } |
Carl-Daniel Hailfinger | f160a12 | 2009-05-08 17:15:15 +0000 | [diff] [blame] | 1280 | |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1281 | if (ret) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1282 | msg_cerr("FAILED!\n"); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1283 | } else { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1284 | msg_cinfo("SUCCESS.\n"); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1285 | } |
| 1286 | return ret; |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 1289 | void emergency_help_message(void) |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1290 | { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1291 | msg_gerr("Your flash chip is in an unknown state.\n" |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 1292 | "Get help on IRC at irc.freenode.net (channel #flashrom) or\n" |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1293 | "mail flashrom@flashrom.org!\n" |
| 1294 | "-------------------------------------------------------------" |
| 1295 | "------------------\n" |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1296 | "DO NOT REBOOT OR POWEROFF!\n"); |
| 1297 | } |
| 1298 | |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1299 | /* The way to go if you want a delimited list of programmers*/ |
| 1300 | void list_programmers(char *delim) |
| 1301 | { |
| 1302 | enum programmer p; |
| 1303 | for (p = 0; p < PROGRAMMER_INVALID; p++) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1304 | msg_ginfo("%s", programmer_table[p].name); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1305 | if (p < PROGRAMMER_INVALID - 1) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1306 | msg_ginfo("%s", delim); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1307 | } |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1308 | msg_ginfo("\n"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1311 | void print_sysinfo(void) |
| 1312 | { |
| 1313 | #if HAVE_UTSNAME == 1 |
| 1314 | struct utsname osinfo; |
| 1315 | uname(&osinfo); |
| 1316 | |
| 1317 | msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release, |
| 1318 | osinfo.machine); |
| 1319 | #else |
| 1320 | msg_ginfo(" on unknown machine"); |
| 1321 | #endif |
| 1322 | msg_ginfo(", built with"); |
| 1323 | #if NEED_PCI == 1 |
| 1324 | #ifdef PCILIB_VERSION |
| 1325 | msg_ginfo(" libpci %s,", PCILIB_VERSION); |
| 1326 | #else |
| 1327 | msg_ginfo(" unknown PCI library,"); |
| 1328 | #endif |
| 1329 | #endif |
| 1330 | #ifdef __clang__ |
Carl-Daniel Hailfinger | b51e58e | 2010-07-17 14:49:30 +0000 | [diff] [blame] | 1331 | msg_ginfo(" LLVM Clang"); |
| 1332 | #ifdef __clang_version__ |
| 1333 | msg_ginfo(" %s,", __clang_version__); |
| 1334 | #else |
| 1335 | msg_ginfo(" unknown version (before r102686),"); |
| 1336 | #endif |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1337 | #elif defined(__GNUC__) |
| 1338 | msg_ginfo(" GCC"); |
| 1339 | #ifdef __VERSION__ |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1340 | msg_ginfo(" %s,", __VERSION__); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1341 | #else |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1342 | msg_ginfo(" unknown version,"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1343 | #endif |
| 1344 | #else |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1345 | msg_ginfo(" unknown compiler,"); |
| 1346 | #endif |
| 1347 | #if defined (__FLASHROM_LITTLE_ENDIAN__) |
| 1348 | msg_ginfo(" little endian"); |
| 1349 | #else |
| 1350 | msg_ginfo(" big endian"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1351 | #endif |
| 1352 | msg_ginfo("\n"); |
| 1353 | } |
| 1354 | |
Bernhard Walle | 201bde3 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 1355 | void print_version(void) |
| 1356 | { |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1357 | msg_ginfo("flashrom v%s", flashrom_version); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1358 | print_sysinfo(); |
Bernhard Walle | 201bde3 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1361 | void print_banner(void) |
| 1362 | { |
| 1363 | msg_ginfo("flashrom is free software, get the source code at " |
| 1364 | "http://www.flashrom.org\n"); |
| 1365 | msg_ginfo("\n"); |
| 1366 | } |
| 1367 | |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1368 | int selfcheck(void) |
| 1369 | { |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1370 | int ret = 0; |
| 1371 | struct flashchip *flash; |
| 1372 | |
| 1373 | /* Safety check. Instead of aborting after the first error, check |
| 1374 | * if more errors exist. |
| 1375 | */ |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1376 | if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1377 | msg_gerr("Programmer table miscompilation!\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1378 | ret = 1; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1379 | } |
| 1380 | if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1381 | msg_gerr("SPI programmer table miscompilation!\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1382 | ret = 1; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1383 | } |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1384 | for (flash = flashchips; flash && flash->name; flash++) |
| 1385 | if (selfcheck_eraseblocks(flash)) |
| 1386 | ret = 1; |
| 1387 | return ret; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | void check_chip_supported(struct flashchip *flash) |
| 1391 | { |
| 1392 | if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1393 | msg_cinfo("===\n"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1394 | if (flash->tested & TEST_BAD_MASK) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1395 | msg_cinfo("This flash part has status NOT WORKING for operations:"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1396 | if (flash->tested & TEST_BAD_PROBE) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1397 | msg_cinfo(" PROBE"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1398 | if (flash->tested & TEST_BAD_READ) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1399 | msg_cinfo(" READ"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1400 | if (flash->tested & TEST_BAD_ERASE) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1401 | msg_cinfo(" ERASE"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1402 | if (flash->tested & TEST_BAD_WRITE) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1403 | msg_cinfo(" WRITE"); |
| 1404 | msg_cinfo("\n"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1405 | } |
| 1406 | if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) || |
| 1407 | (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) || |
| 1408 | (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) || |
| 1409 | (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1410 | msg_cinfo("This flash part has status UNTESTED for operations:"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1411 | if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1412 | msg_cinfo(" PROBE"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1413 | if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1414 | msg_cinfo(" READ"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1415 | if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1416 | msg_cinfo(" ERASE"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1417 | if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE)) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1418 | msg_cinfo(" WRITE"); |
| 1419 | msg_cinfo("\n"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1420 | } |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 1421 | /* FIXME: This message is designed towards CLI users. */ |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1422 | msg_cinfo("The test status of this chip may have been updated " |
| 1423 | "in the latest development\n" |
| 1424 | "version of flashrom. If you are running the latest " |
| 1425 | "development version,\n" |
| 1426 | "please email a report to flashrom@flashrom.org if " |
| 1427 | "any of the above operations\n" |
| 1428 | "work correctly for you with this flash part. Please " |
| 1429 | "include the flashrom\n" |
| 1430 | "output with the additional -V option for all " |
| 1431 | "operations you tested (-V, -Vr,\n" |
| 1432 | "-Vw, -VE), and mention which mainboard or " |
| 1433 | "programmer you tested.\n" |
| 1434 | "Thanks for your help!\n" |
| 1435 | "===\n"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1436 | } |
| 1437 | } |
| 1438 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 1439 | int main(int argc, char *argv[]) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1440 | { |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 1441 | return cli_classic(argc, argv); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | /* This function signature is horrible. We need to design a better interface, |
| 1445 | * but right now it allows us to split off the CLI code. |
| 1446 | */ |
| 1447 | int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it) |
| 1448 | { |
| 1449 | uint8_t *buf; |
| 1450 | unsigned long numbytes; |
| 1451 | FILE *image; |
| 1452 | int ret = 0; |
| 1453 | unsigned long size; |
| 1454 | |
| 1455 | size = flash->total_size * 1024; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1456 | buf = (uint8_t *) calloc(size, sizeof(char)); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1457 | |
Carl-Daniel Hailfinger | d1be52d | 2010-07-03 12:14:25 +0000 | [diff] [blame] | 1458 | if (!programmer_may_write && (write_it || erase_it)) { |
| 1459 | msg_perr("Write/erase is not working yet on your programmer in " |
| 1460 | "its current configuration.\n"); |
| 1461 | /* --force is the wrong approach, but it's the best we can do |
| 1462 | * until the generic programmer parameter parser is merged. |
| 1463 | */ |
| 1464 | if (!force) { |
| 1465 | msg_perr("Aborting.\n"); |
| 1466 | programmer_shutdown(); |
| 1467 | return 1; |
| 1468 | } else { |
| 1469 | msg_cerr("Continuing anyway.\n"); |
| 1470 | } |
| 1471 | } |
| 1472 | |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 1473 | if (erase_it) { |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1474 | if (flash->tested & TEST_BAD_ERASE) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1475 | msg_cerr("Erase is not working on this chip. "); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1476 | if (!force) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1477 | msg_cerr("Aborting.\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1478 | programmer_shutdown(); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1479 | return 1; |
| 1480 | } else { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1481 | msg_cerr("Continuing anyway.\n"); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1482 | } |
| 1483 | } |
Sean Nelson | 6e0b912 | 2010-02-19 00:52:10 +0000 | [diff] [blame] | 1484 | if (flash->unlock) |
| 1485 | flash->unlock(flash); |
| 1486 | |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1487 | if (erase_flash(flash)) { |
| 1488 | emergency_help_message(); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1489 | programmer_shutdown(); |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1490 | return 1; |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1491 | } |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 1492 | } else if (read_it) { |
Sean Nelson | 6e0b912 | 2010-02-19 00:52:10 +0000 | [diff] [blame] | 1493 | if (flash->unlock) |
| 1494 | flash->unlock(flash); |
| 1495 | |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1496 | if (read_flash_to_file(flash, filename)) { |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1497 | programmer_shutdown(); |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 1498 | return 1; |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1499 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1500 | } else { |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 1501 | struct stat image_stat; |
| 1502 | |
Sean Nelson | 6e0b912 | 2010-02-19 00:52:10 +0000 | [diff] [blame] | 1503 | if (flash->unlock) |
| 1504 | flash->unlock(flash); |
| 1505 | |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1506 | if (flash->tested & TEST_BAD_ERASE) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1507 | msg_cerr("Erase is not working on this chip " |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1508 | "and erase is needed for write. "); |
| 1509 | if (!force) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1510 | msg_cerr("Aborting.\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1511 | programmer_shutdown(); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1512 | return 1; |
| 1513 | } else { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1514 | msg_cerr("Continuing anyway.\n"); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1515 | } |
| 1516 | } |
| 1517 | if (flash->tested & TEST_BAD_WRITE) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1518 | msg_cerr("Write is not working on this chip. "); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1519 | if (!force) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1520 | msg_cerr("Aborting.\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1521 | programmer_shutdown(); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1522 | return 1; |
| 1523 | } else { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1524 | msg_cerr("Continuing anyway.\n"); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1525 | } |
| 1526 | } |
Patrick Georgi | 0bf842d | 2010-01-25 22:55:33 +0000 | [diff] [blame] | 1527 | if ((image = fopen(filename, "rb")) == NULL) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1528 | perror(filename); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1529 | programmer_shutdown(); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1530 | exit(1); |
| 1531 | } |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 1532 | if (fstat(fileno(image), &image_stat) != 0) { |
| 1533 | perror(filename); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1534 | programmer_shutdown(); |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 1535 | exit(1); |
| 1536 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1537 | if (image_stat.st_size != flash->total_size * 1024) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1538 | msg_gerr("Error: Image size doesn't match\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1539 | programmer_shutdown(); |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 1540 | exit(1); |
| 1541 | } |
| 1542 | |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 1543 | numbytes = fread(buf, 1, size, image); |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 1544 | #if CONFIG_INTERNAL == 1 |
Peter Stuge | 7ffbc6f | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 1545 | show_id(buf, size, force); |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 1546 | #endif |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1547 | fclose(image); |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 1548 | if (numbytes != size) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1549 | msg_gerr("Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1550 | programmer_shutdown(); |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 1551 | return 1; |
| 1552 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1555 | // This should be moved into each flash part's code to do it |
| 1556 | // cleanly. This does the job. |
Carl-Daniel Hailfinger | f5fb51c | 2009-08-19 15:19:18 +0000 | [diff] [blame] | 1557 | handle_romentries(buf, flash); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1558 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1559 | // //////////////////////////////////////////////////////////// |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1560 | |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1561 | if (write_it) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1562 | msg_cinfo("Writing flash chip... "); |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1563 | if (!flash->write) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1564 | msg_cerr("Error: flashrom has no write function for this flash chip.\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1565 | programmer_shutdown(); |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1566 | return 1; |
| 1567 | } |
Carl-Daniel Hailfinger | 0a3e5ae | 2009-07-24 12:18:54 +0000 | [diff] [blame] | 1568 | ret = flash->write(flash, buf); |
| 1569 | if (ret) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1570 | msg_cerr("FAILED!\n"); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1571 | emergency_help_message(); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1572 | programmer_shutdown(); |
Carl-Daniel Hailfinger | 0a3e5ae | 2009-07-24 12:18:54 +0000 | [diff] [blame] | 1573 | return 1; |
| 1574 | } else { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1575 | msg_cinfo("COMPLETE.\n"); |
Carl-Daniel Hailfinger | 0a3e5ae | 2009-07-24 12:18:54 +0000 | [diff] [blame] | 1576 | } |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1577 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1578 | |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1579 | if (verify_it) { |
| 1580 | /* Work around chips which need some time to calm down. */ |
| 1581 | if (write_it) |
| 1582 | programmer_delay(1000*1000); |
Carl-Daniel Hailfinger | 0a3e5ae | 2009-07-24 12:18:54 +0000 | [diff] [blame] | 1583 | ret = verify_flash(flash, buf); |
Carl-Daniel Hailfinger | f529205 | 2009-11-17 09:57:34 +0000 | [diff] [blame] | 1584 | /* If we tried to write, and verification now fails, we |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1585 | * might have an emergency situation. |
| 1586 | */ |
| 1587 | if (ret && write_it) |
| 1588 | emergency_help_message(); |
| 1589 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1590 | |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 1591 | programmer_shutdown(); |
| 1592 | |
Stefan Reinauer | 143da0b | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 1593 | return ret; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1594 | } |