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> |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 26 | #ifndef __LIBPAYLOAD__ |
| 27 | #include <fcntl.h> |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 28 | #include <sys/stat.h> |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 29 | #endif |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 30 | #include <string.h> |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 32 | #include <ctype.h> |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 33 | #include <getopt.h> |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 34 | #if HAVE_UTSNAME == 1 |
| 35 | #include <sys/utsname.h> |
| 36 | #endif |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 37 | #include "flash.h" |
Carl-Daniel Hailfinger | 0845464 | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 38 | #include "flashchips.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 39 | #include "programmer.h" |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 40 | |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 41 | const char flashrom_version[] = FLASHROM_VERSION; |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 42 | char *chip_to_probe = NULL; |
Peter Stuge | 7ffbc6f | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 43 | int verbose = 0; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 44 | |
Carl-Daniel Hailfinger | 2e68160 | 2011-09-08 00:00:29 +0000 | [diff] [blame^] | 45 | static enum programmer programmer = PROGRAMMER_INVALID; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 46 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 47 | static char *programmer_param = NULL; |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 48 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 49 | /* Supported buses for the current programmer. */ |
| 50 | enum chipbustype buses_supported; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 51 | |
Uwe Hermann | 48ec1b1 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 52 | /* |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 53 | * Programmers supporting multiple buses can have differing size limits on |
| 54 | * each bus. Store the limits for each bus in a common struct. |
| 55 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 56 | struct decode_sizes max_rom_decode; |
| 57 | |
| 58 | /* If nonzero, used as the start address of bottom-aligned flash. */ |
| 59 | unsigned long flashbase; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 60 | |
Carl-Daniel Hailfinger | d1be52d | 2010-07-03 12:14:25 +0000 | [diff] [blame] | 61 | /* Is writing allowed with this programmer? */ |
| 62 | int programmer_may_write; |
| 63 | |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 64 | const struct programmer_entry programmer_table[] = { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 65 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 66 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 67 | .name = "internal", |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 68 | .init = internal_init, |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 69 | .map_flash_region = physmap, |
| 70 | .unmap_flash_region = physunmap, |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 71 | .chip_readb = internal_chip_readb, |
| 72 | .chip_readw = internal_chip_readw, |
| 73 | .chip_readl = internal_chip_readl, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 74 | .chip_readn = internal_chip_readn, |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 75 | .chip_writeb = internal_chip_writeb, |
| 76 | .chip_writew = internal_chip_writew, |
| 77 | .chip_writel = internal_chip_writel, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 78 | .chip_writen = fallback_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 79 | .delay = internal_delay, |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 80 | }, |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 81 | #endif |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 82 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 83 | #if CONFIG_DUMMY == 1 |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 84 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 85 | .name = "dummy", |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 86 | .init = dummy_init, |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 87 | .map_flash_region = dummy_map, |
| 88 | .unmap_flash_region = dummy_unmap, |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 89 | .chip_readb = dummy_chip_readb, |
| 90 | .chip_readw = dummy_chip_readw, |
| 91 | .chip_readl = dummy_chip_readl, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 92 | .chip_readn = dummy_chip_readn, |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 93 | .chip_writeb = dummy_chip_writeb, |
| 94 | .chip_writew = dummy_chip_writew, |
| 95 | .chip_writel = dummy_chip_writel, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 96 | .chip_writen = dummy_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 97 | .delay = internal_delay, |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 98 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 99 | #endif |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 100 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 101 | #if CONFIG_NIC3COM == 1 |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 102 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 103 | .name = "nic3com", |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 104 | .init = nic3com_init, |
Uwe Hermann | c691593 | 2009-05-17 23:12:17 +0000 | [diff] [blame] | 105 | .map_flash_region = fallback_map, |
| 106 | .unmap_flash_region = fallback_unmap, |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 107 | .chip_readb = nic3com_chip_readb, |
Carl-Daniel Hailfinger | 9ee1077 | 2009-05-16 01:23:55 +0000 | [diff] [blame] | 108 | .chip_readw = fallback_chip_readw, |
| 109 | .chip_readl = fallback_chip_readl, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 110 | .chip_readn = fallback_chip_readn, |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 111 | .chip_writeb = nic3com_chip_writeb, |
Carl-Daniel Hailfinger | 9ee1077 | 2009-05-16 01:23:55 +0000 | [diff] [blame] | 112 | .chip_writew = fallback_chip_writew, |
| 113 | .chip_writel = fallback_chip_writel, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 114 | .chip_writen = fallback_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 115 | .delay = internal_delay, |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 116 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 117 | #endif |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 118 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 119 | #if CONFIG_NICREALTEK == 1 |
Joerg Fischer | 5665ef3 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 120 | { |
Carl-Daniel Hailfinger | 40446ee | 2011-03-07 01:08:09 +0000 | [diff] [blame] | 121 | /* This programmer works for Realtek RTL8139 and SMC 1211. */ |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 122 | .name = "nicrealtek", |
| 123 | //.name = "nicsmc1211", |
| 124 | .init = nicrealtek_init, |
| 125 | .map_flash_region = fallback_map, |
| 126 | .unmap_flash_region = fallback_unmap, |
| 127 | .chip_readb = nicrealtek_chip_readb, |
| 128 | .chip_readw = fallback_chip_readw, |
| 129 | .chip_readl = fallback_chip_readl, |
| 130 | .chip_readn = fallback_chip_readn, |
| 131 | .chip_writeb = nicrealtek_chip_writeb, |
| 132 | .chip_writew = fallback_chip_writew, |
| 133 | .chip_writel = fallback_chip_writel, |
| 134 | .chip_writen = fallback_chip_writen, |
| 135 | .delay = internal_delay, |
Joerg Fischer | 5665ef3 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 136 | }, |
Joerg Fischer | 5665ef3 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 137 | #endif |
| 138 | |
Andrew Morgan | c29c2e7 | 2010-06-07 22:37:54 +0000 | [diff] [blame] | 139 | #if CONFIG_NICNATSEMI == 1 |
| 140 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 141 | .name = "nicnatsemi", |
| 142 | .init = nicnatsemi_init, |
| 143 | .map_flash_region = fallback_map, |
| 144 | .unmap_flash_region = fallback_unmap, |
| 145 | .chip_readb = nicnatsemi_chip_readb, |
| 146 | .chip_readw = fallback_chip_readw, |
| 147 | .chip_readl = fallback_chip_readl, |
| 148 | .chip_readn = fallback_chip_readn, |
| 149 | .chip_writeb = nicnatsemi_chip_writeb, |
| 150 | .chip_writew = fallback_chip_writew, |
| 151 | .chip_writel = fallback_chip_writel, |
| 152 | .chip_writen = fallback_chip_writen, |
| 153 | .delay = internal_delay, |
Andrew Morgan | c29c2e7 | 2010-06-07 22:37:54 +0000 | [diff] [blame] | 154 | }, |
| 155 | #endif |
Joerg Fischer | 5665ef3 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 156 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 157 | #if CONFIG_GFXNVIDIA == 1 |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 158 | { |
| 159 | .name = "gfxnvidia", |
| 160 | .init = gfxnvidia_init, |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 161 | .map_flash_region = fallback_map, |
| 162 | .unmap_flash_region = fallback_unmap, |
| 163 | .chip_readb = gfxnvidia_chip_readb, |
| 164 | .chip_readw = fallback_chip_readw, |
| 165 | .chip_readl = fallback_chip_readl, |
| 166 | .chip_readn = fallback_chip_readn, |
| 167 | .chip_writeb = gfxnvidia_chip_writeb, |
| 168 | .chip_writew = fallback_chip_writew, |
| 169 | .chip_writel = fallback_chip_writel, |
| 170 | .chip_writen = fallback_chip_writen, |
| 171 | .delay = internal_delay, |
| 172 | }, |
| 173 | #endif |
| 174 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 175 | #if CONFIG_DRKAISER == 1 |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 176 | { |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 177 | .name = "drkaiser", |
| 178 | .init = drkaiser_init, |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 179 | .map_flash_region = fallback_map, |
| 180 | .unmap_flash_region = fallback_unmap, |
| 181 | .chip_readb = drkaiser_chip_readb, |
| 182 | .chip_readw = fallback_chip_readw, |
| 183 | .chip_readl = fallback_chip_readl, |
| 184 | .chip_readn = fallback_chip_readn, |
| 185 | .chip_writeb = drkaiser_chip_writeb, |
| 186 | .chip_writew = fallback_chip_writew, |
| 187 | .chip_writel = fallback_chip_writel, |
| 188 | .chip_writen = fallback_chip_writen, |
| 189 | .delay = internal_delay, |
| 190 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 191 | #endif |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 192 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 193 | #if CONFIG_SATASII == 1 |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 194 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 195 | .name = "satasii", |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 196 | .init = satasii_init, |
Uwe Hermann | c691593 | 2009-05-17 23:12:17 +0000 | [diff] [blame] | 197 | .map_flash_region = fallback_map, |
| 198 | .unmap_flash_region = fallback_unmap, |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 199 | .chip_readb = satasii_chip_readb, |
| 200 | .chip_readw = fallback_chip_readw, |
| 201 | .chip_readl = fallback_chip_readl, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 202 | .chip_readn = fallback_chip_readn, |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 203 | .chip_writeb = satasii_chip_writeb, |
| 204 | .chip_writew = fallback_chip_writew, |
| 205 | .chip_writel = fallback_chip_writel, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 206 | .chip_writen = fallback_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 207 | .delay = internal_delay, |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 208 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 209 | #endif |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 210 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 211 | #if CONFIG_ATAHPT == 1 |
Uwe Hermann | ddd5c9e | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 212 | { |
| 213 | .name = "atahpt", |
| 214 | .init = atahpt_init, |
Uwe Hermann | ddd5c9e | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 215 | .map_flash_region = fallback_map, |
| 216 | .unmap_flash_region = fallback_unmap, |
| 217 | .chip_readb = atahpt_chip_readb, |
| 218 | .chip_readw = fallback_chip_readw, |
| 219 | .chip_readl = fallback_chip_readl, |
| 220 | .chip_readn = fallback_chip_readn, |
| 221 | .chip_writeb = atahpt_chip_writeb, |
| 222 | .chip_writew = fallback_chip_writew, |
| 223 | .chip_writel = fallback_chip_writel, |
| 224 | .chip_writen = fallback_chip_writen, |
| 225 | .delay = internal_delay, |
| 226 | }, |
| 227 | #endif |
| 228 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 229 | #if CONFIG_FT2232_SPI == 1 |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 230 | { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 231 | .name = "ft2232_spi", |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 232 | .init = ft2232_spi_init, |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 233 | .map_flash_region = fallback_map, |
| 234 | .unmap_flash_region = fallback_unmap, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 235 | .chip_readb = noop_chip_readb, |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 236 | .chip_readw = fallback_chip_readw, |
| 237 | .chip_readl = fallback_chip_readl, |
| 238 | .chip_readn = fallback_chip_readn, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 239 | .chip_writeb = noop_chip_writeb, |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 240 | .chip_writew = fallback_chip_writew, |
| 241 | .chip_writel = fallback_chip_writel, |
| 242 | .chip_writen = fallback_chip_writen, |
| 243 | .delay = internal_delay, |
| 244 | }, |
Carl-Daniel Hailfinger | 3426ef6 | 2009-08-19 13:27:58 +0000 | [diff] [blame] | 245 | #endif |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 246 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 247 | #if CONFIG_SERPROG == 1 |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 248 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 249 | .name = "serprog", |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 250 | .init = serprog_init, |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 251 | .map_flash_region = fallback_map, |
| 252 | .unmap_flash_region = fallback_unmap, |
| 253 | .chip_readb = serprog_chip_readb, |
| 254 | .chip_readw = fallback_chip_readw, |
| 255 | .chip_readl = fallback_chip_readl, |
| 256 | .chip_readn = serprog_chip_readn, |
| 257 | .chip_writeb = serprog_chip_writeb, |
| 258 | .chip_writew = fallback_chip_writew, |
| 259 | .chip_writel = fallback_chip_writel, |
| 260 | .chip_writen = fallback_chip_writen, |
| 261 | .delay = serprog_delay, |
| 262 | }, |
Carl-Daniel Hailfinger | 6be7411 | 2009-08-12 16:17:41 +0000 | [diff] [blame] | 263 | #endif |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 264 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 265 | #if CONFIG_BUSPIRATE_SPI == 1 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 266 | { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 267 | .name = "buspirate_spi", |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 268 | .init = buspirate_spi_init, |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 269 | .map_flash_region = fallback_map, |
| 270 | .unmap_flash_region = fallback_unmap, |
| 271 | .chip_readb = noop_chip_readb, |
| 272 | .chip_readw = fallback_chip_readw, |
| 273 | .chip_readl = fallback_chip_readl, |
| 274 | .chip_readn = fallback_chip_readn, |
| 275 | .chip_writeb = noop_chip_writeb, |
| 276 | .chip_writew = fallback_chip_writew, |
| 277 | .chip_writel = fallback_chip_writel, |
| 278 | .chip_writen = fallback_chip_writen, |
| 279 | .delay = internal_delay, |
| 280 | }, |
| 281 | #endif |
| 282 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 283 | #if CONFIG_DEDIPROG == 1 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 284 | { |
| 285 | .name = "dediprog", |
| 286 | .init = dediprog_init, |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 287 | .map_flash_region = fallback_map, |
| 288 | .unmap_flash_region = fallback_unmap, |
| 289 | .chip_readb = noop_chip_readb, |
| 290 | .chip_readw = fallback_chip_readw, |
| 291 | .chip_readl = fallback_chip_readl, |
| 292 | .chip_readn = fallback_chip_readn, |
| 293 | .chip_writeb = noop_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 | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 301 | #if CONFIG_RAYER_SPI == 1 |
| 302 | { |
| 303 | .name = "rayer_spi", |
| 304 | .init = rayer_spi_init, |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 305 | .map_flash_region = fallback_map, |
| 306 | .unmap_flash_region = fallback_unmap, |
| 307 | .chip_readb = noop_chip_readb, |
| 308 | .chip_readw = fallback_chip_readw, |
| 309 | .chip_readl = fallback_chip_readl, |
| 310 | .chip_readn = fallback_chip_readn, |
| 311 | .chip_writeb = noop_chip_writeb, |
| 312 | .chip_writew = fallback_chip_writew, |
| 313 | .chip_writel = fallback_chip_writel, |
| 314 | .chip_writen = fallback_chip_writen, |
| 315 | .delay = internal_delay, |
| 316 | }, |
| 317 | #endif |
| 318 | |
Carl-Daniel Hailfinger | b713d2e | 2011-05-08 00:24:18 +0000 | [diff] [blame] | 319 | #if CONFIG_NICINTEL == 1 |
| 320 | { |
| 321 | .name = "nicintel", |
| 322 | .init = nicintel_init, |
Carl-Daniel Hailfinger | b713d2e | 2011-05-08 00:24:18 +0000 | [diff] [blame] | 323 | .map_flash_region = fallback_map, |
| 324 | .unmap_flash_region = fallback_unmap, |
| 325 | .chip_readb = nicintel_chip_readb, |
| 326 | .chip_readw = fallback_chip_readw, |
| 327 | .chip_readl = fallback_chip_readl, |
| 328 | .chip_readn = fallback_chip_readn, |
| 329 | .chip_writeb = nicintel_chip_writeb, |
| 330 | .chip_writew = fallback_chip_writew, |
| 331 | .chip_writel = fallback_chip_writel, |
| 332 | .chip_writen = fallback_chip_writen, |
| 333 | .delay = internal_delay, |
| 334 | }, |
| 335 | #endif |
| 336 | |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 337 | #if CONFIG_NICINTEL_SPI == 1 |
| 338 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 339 | .name = "nicintel_spi", |
| 340 | .init = nicintel_spi_init, |
| 341 | .map_flash_region = fallback_map, |
| 342 | .unmap_flash_region = fallback_unmap, |
| 343 | .chip_readb = noop_chip_readb, |
| 344 | .chip_readw = fallback_chip_readw, |
| 345 | .chip_readl = fallback_chip_readl, |
| 346 | .chip_readn = fallback_chip_readn, |
| 347 | .chip_writeb = noop_chip_writeb, |
| 348 | .chip_writew = fallback_chip_writew, |
| 349 | .chip_writel = fallback_chip_writel, |
| 350 | .chip_writen = fallback_chip_writen, |
| 351 | .delay = internal_delay, |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 352 | }, |
| 353 | #endif |
| 354 | |
Mark Marshall | 90021f2 | 2010-12-03 14:48:11 +0000 | [diff] [blame] | 355 | #if CONFIG_OGP_SPI == 1 |
| 356 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 357 | .name = "ogp_spi", |
| 358 | .init = ogp_spi_init, |
| 359 | .map_flash_region = fallback_map, |
| 360 | .unmap_flash_region = fallback_unmap, |
| 361 | .chip_readb = noop_chip_readb, |
| 362 | .chip_readw = fallback_chip_readw, |
| 363 | .chip_readl = fallback_chip_readl, |
| 364 | .chip_readn = fallback_chip_readn, |
| 365 | .chip_writeb = noop_chip_writeb, |
| 366 | .chip_writew = fallback_chip_writew, |
| 367 | .chip_writel = fallback_chip_writel, |
| 368 | .chip_writen = fallback_chip_writen, |
| 369 | .delay = internal_delay, |
Mark Marshall | 90021f2 | 2010-12-03 14:48:11 +0000 | [diff] [blame] | 370 | }, |
| 371 | #endif |
| 372 | |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 373 | #if CONFIG_SATAMV == 1 |
| 374 | { |
| 375 | .name = "satamv", |
| 376 | .init = satamv_init, |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 377 | .map_flash_region = fallback_map, |
| 378 | .unmap_flash_region = fallback_unmap, |
| 379 | .chip_readb = satamv_chip_readb, |
| 380 | .chip_readw = fallback_chip_readw, |
| 381 | .chip_readl = fallback_chip_readl, |
| 382 | .chip_readn = fallback_chip_readn, |
| 383 | .chip_writeb = satamv_chip_writeb, |
| 384 | .chip_writew = fallback_chip_writew, |
| 385 | .chip_writel = fallback_chip_writel, |
| 386 | .chip_writen = fallback_chip_writen, |
| 387 | .delay = internal_delay, |
| 388 | }, |
| 389 | #endif |
| 390 | |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 391 | #if CONFIG_LINUX_SPI == 1 |
| 392 | { |
| 393 | .name = "linux_spi", |
| 394 | .init = linux_spi_init, |
| 395 | .map_flash_region = fallback_map, |
| 396 | .unmap_flash_region = fallback_unmap, |
| 397 | .chip_readb = noop_chip_readb, |
| 398 | .chip_readw = fallback_chip_readw, |
| 399 | .chip_readl = fallback_chip_readl, |
| 400 | .chip_readn = fallback_chip_readn, |
| 401 | .chip_writeb = noop_chip_writeb, |
| 402 | .chip_writew = fallback_chip_writew, |
| 403 | .chip_writel = fallback_chip_writel, |
| 404 | .chip_writen = fallback_chip_writen, |
| 405 | .delay = internal_delay, |
| 406 | }, |
| 407 | #endif |
| 408 | |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 409 | {}, /* This entry corresponds to PROGRAMMER_INVALID. */ |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 410 | }; |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 411 | |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 412 | #define SHUTDOWN_MAXFN 32 |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 413 | static int shutdown_fn_count = 0; |
| 414 | struct shutdown_func_data { |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 415 | int (*func) (void *data); |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 416 | void *data; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 417 | } static shutdown_fn[SHUTDOWN_MAXFN]; |
| 418 | /* Initialize to 0 to make sure nobody registers a shutdown function before |
| 419 | * programmer init. |
| 420 | */ |
| 421 | static int may_register_shutdown = 0; |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 422 | |
Stefan Tauner | 5368dca | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 423 | static int check_block_eraser(const struct flashchip *flash, int k, int log); |
| 424 | |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 425 | /* Register a function to be executed on programmer shutdown. |
| 426 | * The advantage over atexit() is that you can supply a void pointer which will |
| 427 | * be used as parameter to the registered function upon programmer shutdown. |
| 428 | * This pointer can point to arbitrary data used by said function, e.g. undo |
| 429 | * information for GPIO settings etc. If unneeded, set data=NULL. |
| 430 | * Please note that the first (void *data) belongs to the function signature of |
| 431 | * the function passed as first parameter. |
| 432 | */ |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 433 | int register_shutdown(int (*function) (void *data), void *data) |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 434 | { |
| 435 | if (shutdown_fn_count >= SHUTDOWN_MAXFN) { |
Carl-Daniel Hailfinger | 9f5f215 | 2010-06-04 23:20:21 +0000 | [diff] [blame] | 436 | msg_perr("Tried to register more than %i shutdown functions.\n", |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 437 | SHUTDOWN_MAXFN); |
| 438 | return 1; |
| 439 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 440 | if (!may_register_shutdown) { |
| 441 | msg_perr("Tried to register a shutdown function before " |
| 442 | "programmer init.\n"); |
| 443 | return 1; |
| 444 | } |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 445 | shutdown_fn[shutdown_fn_count].func = function; |
| 446 | shutdown_fn[shutdown_fn_count].data = data; |
| 447 | shutdown_fn_count++; |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
Carl-Daniel Hailfinger | 2e68160 | 2011-09-08 00:00:29 +0000 | [diff] [blame^] | 452 | int programmer_init(enum programmer prog, char *param) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 453 | { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 454 | int ret; |
Carl-Daniel Hailfinger | 2e68160 | 2011-09-08 00:00:29 +0000 | [diff] [blame^] | 455 | |
| 456 | if (prog >= PROGRAMMER_INVALID) { |
| 457 | msg_perr("Invalid programmer specified!\n"); |
| 458 | return -1; |
| 459 | } |
| 460 | programmer = prog; |
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, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 467 | .spi = 0xffffffff, |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 468 | }; |
Carl-Daniel Hailfinger | 1a22795 | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 469 | buses_supported = BUS_NONE; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 470 | /* Default to top aligned flash at 4 GB. */ |
| 471 | flashbase = 0; |
| 472 | /* Registering shutdown functions is now allowed. */ |
| 473 | may_register_shutdown = 1; |
Carl-Daniel Hailfinger | d1be52d | 2010-07-03 12:14:25 +0000 | [diff] [blame] | 474 | /* Default to allowing writes. Broken programmers set this to 0. */ |
| 475 | programmer_may_write = 1; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 476 | |
| 477 | programmer_param = param; |
| 478 | msg_pdbg("Initializing %s programmer\n", |
| 479 | programmer_table[programmer].name); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 480 | ret = programmer_table[programmer].init(); |
| 481 | if (programmer_param && strlen(programmer_param)) { |
| 482 | msg_perr("Unhandled programmer parameters: %s\n", |
| 483 | programmer_param); |
| 484 | /* Do not error out here, the init itself was successful. */ |
| 485 | } |
| 486 | return ret; |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | int programmer_shutdown(void) |
| 490 | { |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 491 | int ret = 0; |
| 492 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 493 | /* Registering shutdown functions is no longer allowed. */ |
| 494 | may_register_shutdown = 0; |
| 495 | while (shutdown_fn_count > 0) { |
| 496 | int i = --shutdown_fn_count; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 497 | ret |= shutdown_fn[i].func(shutdown_fn[i].data); |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 498 | } |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 499 | return ret; |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | void *programmer_map_flash_region(const char *descr, unsigned long phys_addr, |
| 503 | size_t len) |
| 504 | { |
| 505 | return programmer_table[programmer].map_flash_region(descr, |
| 506 | phys_addr, len); |
| 507 | } |
| 508 | |
| 509 | void programmer_unmap_flash_region(void *virt_addr, size_t len) |
| 510 | { |
| 511 | programmer_table[programmer].unmap_flash_region(virt_addr, len); |
| 512 | } |
| 513 | |
| 514 | void chip_writeb(uint8_t val, chipaddr addr) |
| 515 | { |
| 516 | programmer_table[programmer].chip_writeb(val, addr); |
| 517 | } |
| 518 | |
| 519 | void chip_writew(uint16_t val, chipaddr addr) |
| 520 | { |
| 521 | programmer_table[programmer].chip_writew(val, addr); |
| 522 | } |
| 523 | |
| 524 | void chip_writel(uint32_t val, chipaddr addr) |
| 525 | { |
| 526 | programmer_table[programmer].chip_writel(val, addr); |
| 527 | } |
| 528 | |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 529 | void chip_writen(uint8_t *buf, chipaddr addr, size_t len) |
| 530 | { |
| 531 | programmer_table[programmer].chip_writen(buf, addr, len); |
| 532 | } |
| 533 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 534 | uint8_t chip_readb(const chipaddr addr) |
| 535 | { |
| 536 | return programmer_table[programmer].chip_readb(addr); |
| 537 | } |
| 538 | |
| 539 | uint16_t chip_readw(const chipaddr addr) |
| 540 | { |
| 541 | return programmer_table[programmer].chip_readw(addr); |
| 542 | } |
| 543 | |
| 544 | uint32_t chip_readl(const chipaddr addr) |
| 545 | { |
| 546 | return programmer_table[programmer].chip_readl(addr); |
| 547 | } |
| 548 | |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 549 | void chip_readn(uint8_t *buf, chipaddr addr, size_t len) |
| 550 | { |
| 551 | programmer_table[programmer].chip_readn(buf, addr, len); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 554 | void programmer_delay(int usecs) |
| 555 | { |
| 556 | programmer_table[programmer].delay(usecs); |
| 557 | } |
| 558 | |
Peter Stuge | 776d202 | 2009-01-26 00:39:57 +0000 | [diff] [blame] | 559 | void map_flash_registers(struct flashchip *flash) |
Stefan Reinauer | ff4f197 | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 560 | { |
Stefan Reinauer | ff4f197 | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 561 | size_t size = flash->total_size * 1024; |
Carl-Daniel Hailfinger | d0fc946 | 2009-05-11 14:01:17 +0000 | [diff] [blame] | 562 | /* Flash registers live 4 MByte below the flash. */ |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 563 | /* FIXME: This is incorrect for nonstandard flashbase. */ |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 564 | 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] | 565 | } |
| 566 | |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 567 | 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] | 568 | { |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 569 | chip_readn(buf, flash->virtual_memory + start, len); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 570 | |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
Carl-Daniel Hailfinger | 38a059d | 2009-06-13 12:04:03 +0000 | [diff] [blame] | 574 | int min(int a, int b) |
| 575 | { |
| 576 | return (a < b) ? a : b; |
| 577 | } |
| 578 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 579 | int max(int a, int b) |
| 580 | { |
| 581 | return (a > b) ? a : b; |
| 582 | } |
| 583 | |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 584 | int bitcount(unsigned long a) |
| 585 | { |
| 586 | int i = 0; |
| 587 | for (; a != 0; a >>= 1) |
| 588 | if (a & 1) |
| 589 | i++; |
| 590 | return i; |
| 591 | } |
| 592 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 593 | void tolower_string(char *str) |
| 594 | { |
| 595 | for (; *str != '\0'; str++) |
| 596 | *str = (char)tolower((unsigned char)*str); |
| 597 | } |
| 598 | |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 599 | char *strcat_realloc(char *dest, const char *src) |
| 600 | { |
| 601 | dest = realloc(dest, strlen(dest) + strlen(src) + 1); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 602 | if (!dest) { |
| 603 | msg_gerr("Out of memory!\n"); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 604 | return NULL; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 605 | } |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 606 | strcat(dest, src); |
| 607 | return dest; |
| 608 | } |
| 609 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 610 | /* 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] | 611 | * It will look for needle with a subsequent '=' in haystack, return a copy of |
| 612 | * needle and remove everything from the first occurrence of needle to the next |
| 613 | * delimiter from haystack. |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 614 | */ |
Stefan Tauner | 6665244 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 615 | char *extract_param(char **haystack, const char *needle, const char *delim) |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 616 | { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 617 | char *param_pos, *opt_pos, *rest; |
| 618 | char *opt = NULL; |
| 619 | int optlen; |
Carl-Daniel Hailfinger | 2702376 | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 620 | int needlelen; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 621 | |
Carl-Daniel Hailfinger | 2702376 | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 622 | needlelen = strlen(needle); |
| 623 | if (!needlelen) { |
| 624 | msg_gerr("%s: empty needle! Please report a bug at " |
| 625 | "flashrom@flashrom.org\n", __func__); |
| 626 | return NULL; |
| 627 | } |
| 628 | /* No programmer parameters given. */ |
| 629 | if (*haystack == NULL) |
| 630 | return NULL; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 631 | param_pos = strstr(*haystack, needle); |
| 632 | do { |
| 633 | if (!param_pos) |
| 634 | return NULL; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 635 | /* Needle followed by '='? */ |
| 636 | if (param_pos[needlelen] == '=') { |
| 637 | |
| 638 | /* Beginning of the string? */ |
| 639 | if (param_pos == *haystack) |
| 640 | break; |
| 641 | /* After a delimiter? */ |
| 642 | if (strchr(delim, *(param_pos - 1))) |
| 643 | break; |
| 644 | } |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 645 | /* Continue searching. */ |
| 646 | param_pos++; |
| 647 | param_pos = strstr(param_pos, needle); |
| 648 | } while (1); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 649 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 650 | if (param_pos) { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 651 | /* Get the string after needle and '='. */ |
| 652 | opt_pos = param_pos + needlelen + 1; |
| 653 | optlen = strcspn(opt_pos, delim); |
| 654 | /* Return an empty string if the parameter was empty. */ |
| 655 | opt = malloc(optlen + 1); |
| 656 | if (!opt) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 657 | msg_gerr("Out of memory!\n"); |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 658 | exit(1); |
| 659 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 660 | strncpy(opt, opt_pos, optlen); |
| 661 | opt[optlen] = '\0'; |
| 662 | rest = opt_pos + optlen; |
| 663 | /* Skip all delimiters after the current parameter. */ |
| 664 | rest += strspn(rest, delim); |
| 665 | memmove(param_pos, rest, strlen(rest) + 1); |
| 666 | /* We could shrink haystack, but the effort is not worth it. */ |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 667 | } |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 668 | |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 669 | return opt; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Stefan Tauner | 6665244 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 672 | char *extract_programmer_param(const char *param_name) |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 673 | { |
| 674 | return extract_param(&programmer_param, param_name, ","); |
| 675 | } |
| 676 | |
Sylvain "ythier" Hitier | 9db4551 | 2011-07-04 07:27:17 +0000 | [diff] [blame] | 677 | /* Returns the number of well-defined erasers for a chip. */ |
| 678 | static unsigned int count_usable_erasers(const struct flashchip *flash) |
Stefan Tauner | 5368dca | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 679 | { |
| 680 | unsigned int usable_erasefunctions = 0; |
| 681 | int k; |
| 682 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
| 683 | if (!check_block_eraser(flash, k, 0)) |
| 684 | usable_erasefunctions++; |
| 685 | } |
| 686 | return usable_erasefunctions; |
| 687 | } |
| 688 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 689 | /* start is an offset to the base address of the flash chip */ |
| 690 | int check_erased_range(struct flashchip *flash, int start, int len) |
| 691 | { |
| 692 | int ret; |
| 693 | uint8_t *cmpbuf = malloc(len); |
| 694 | |
| 695 | if (!cmpbuf) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 696 | msg_gerr("Could not allocate memory!\n"); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 697 | exit(1); |
| 698 | } |
| 699 | memset(cmpbuf, 0xff, len); |
| 700 | ret = verify_range(flash, cmpbuf, start, len, "ERASE"); |
| 701 | free(cmpbuf); |
| 702 | return ret; |
| 703 | } |
| 704 | |
Uwe Hermann | 48ec1b1 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 705 | /* |
Carl-Daniel Hailfinger | d0250a3 | 2009-11-25 17:05:52 +0000 | [diff] [blame] | 706 | * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 707 | * flash content at location start |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 708 | * @start offset to the base address of the flash chip |
| 709 | * @len length of the verified area |
| 710 | * @message string to print in the "FAILED" message |
| 711 | * @return 0 for success, -1 for failure |
| 712 | */ |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 713 | int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, |
| 714 | const char *message) |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 715 | { |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 716 | int i; |
Carl-Daniel Hailfinger | d836941 | 2010-11-16 17:21:58 +0000 | [diff] [blame] | 717 | uint8_t *readbuf = malloc(len); |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 718 | int ret = 0, failcount = 0; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 719 | |
| 720 | if (!len) |
| 721 | goto out_free; |
| 722 | |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 723 | if (!flash->read) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 724 | 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] | 725 | return 1; |
| 726 | } |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 727 | if (!readbuf) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 728 | msg_gerr("Could not allocate memory!\n"); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 729 | exit(1); |
| 730 | } |
| 731 | |
| 732 | if (start + len > flash->total_size * 1024) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 733 | 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] | 734 | " total_size 0x%x\n", __func__, start, len, |
| 735 | flash->total_size * 1024); |
| 736 | ret = -1; |
| 737 | goto out_free; |
| 738 | } |
| 739 | if (!message) |
| 740 | message = "VERIFY"; |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 741 | |
Carl-Daniel Hailfinger | d836941 | 2010-11-16 17:21:58 +0000 | [diff] [blame] | 742 | ret = flash->read(flash, readbuf, start, len); |
| 743 | if (ret) { |
| 744 | msg_gerr("Verification impossible because read failed " |
| 745 | "at 0x%x (len 0x%x)\n", start, len); |
| 746 | return ret; |
| 747 | } |
| 748 | |
| 749 | for (i = 0; i < len; i++) { |
| 750 | if (cmpbuf[i] != readbuf[i]) { |
| 751 | /* Only print the first failure. */ |
| 752 | if (!failcount++) |
| 753 | msg_cerr("%s FAILED at 0x%08x! " |
| 754 | "Expected=0x%02x, Read=0x%02x,", |
| 755 | message, start + i, cmpbuf[i], |
| 756 | readbuf[i]); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
Carl-Daniel Hailfinger | 49b9cab | 2009-07-23 01:42:56 +0000 | [diff] [blame] | 759 | if (failcount) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 760 | msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n", |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 761 | start, start + len - 1, failcount); |
Carl-Daniel Hailfinger | 49b9cab | 2009-07-23 01:42:56 +0000 | [diff] [blame] | 762 | ret = -1; |
| 763 | } |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 764 | |
| 765 | out_free: |
| 766 | free(readbuf); |
| 767 | return ret; |
| 768 | } |
| 769 | |
Uwe Hermann | 48ec1b1 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 770 | /* |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 771 | * Check if the buffer @have can be programmed to the content of @want without |
| 772 | * erasing. This is only possible if all chunks of size @gran are either kept |
| 773 | * as-is or changed from an all-ones state to any other state. |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 774 | * |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 775 | * The following write granularities (enum @gran) are known: |
| 776 | * - 1 bit. Each bit can be cleared individually. |
| 777 | * - 1 byte. A byte can be written once. Further writes to an already written |
| 778 | * byte cause the contents to be either undefined or to stay unchanged. |
| 779 | * - 128 bytes. If less than 128 bytes are written, the rest will be |
| 780 | * erased. Each write to a 128-byte region will trigger an automatic erase |
| 781 | * before anything is written. Very uncommon behaviour and unsupported by |
| 782 | * this function. |
| 783 | * - 256 bytes. If less than 256 bytes are written, the contents of the |
| 784 | * unwritten bytes are undefined. |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 785 | * Warning: This function assumes that @have and @want point to naturally |
| 786 | * aligned regions. |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 787 | * |
| 788 | * @have buffer with current content |
| 789 | * @want buffer with desired content |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 790 | * @len length of the checked area |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 791 | * @gran write granularity (enum, not count) |
| 792 | * @return 0 if no erase is needed, 1 otherwise |
| 793 | */ |
| 794 | int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran) |
| 795 | { |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 796 | int result = 0; |
| 797 | int i, j, limit; |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 798 | |
| 799 | switch (gran) { |
| 800 | case write_gran_1bit: |
| 801 | for (i = 0; i < len; i++) |
| 802 | if ((have[i] & want[i]) != want[i]) { |
| 803 | result = 1; |
| 804 | break; |
| 805 | } |
| 806 | break; |
| 807 | case write_gran_1byte: |
| 808 | for (i = 0; i < len; i++) |
| 809 | if ((have[i] != want[i]) && (have[i] != 0xff)) { |
| 810 | result = 1; |
| 811 | break; |
| 812 | } |
| 813 | break; |
| 814 | case write_gran_256bytes: |
| 815 | for (j = 0; j < len / 256; j++) { |
| 816 | limit = min (256, len - j * 256); |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 817 | /* Are 'have' and 'want' identical? */ |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 818 | if (!memcmp(have + j * 256, want + j * 256, limit)) |
| 819 | continue; |
| 820 | /* have needs to be in erased state. */ |
| 821 | for (i = 0; i < limit; i++) |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 822 | if (have[j * 256 + i] != 0xff) { |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 823 | result = 1; |
| 824 | break; |
| 825 | } |
| 826 | if (result) |
| 827 | break; |
| 828 | } |
| 829 | break; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 830 | default: |
| 831 | msg_cerr("%s: Unsupported granularity! Please report a bug at " |
| 832 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 833 | } |
| 834 | return result; |
| 835 | } |
| 836 | |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 837 | /** |
| 838 | * Check if the buffer @have needs to be programmed to get the content of @want. |
| 839 | * If yes, return 1 and fill in first_start with the start address of the |
| 840 | * write operation and first_len with the length of the first to-be-written |
| 841 | * chunk. If not, return 0 and leave first_start and first_len undefined. |
| 842 | * |
| 843 | * Warning: This function assumes that @have and @want point to naturally |
| 844 | * aligned regions. |
| 845 | * |
| 846 | * @have buffer with current content |
| 847 | * @want buffer with desired content |
| 848 | * @len length of the checked area |
| 849 | * @gran write granularity (enum, not count) |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 850 | * @first_start offset of the first byte which needs to be written (passed in |
| 851 | * value is increased by the offset of the first needed write |
| 852 | * relative to have/want or unchanged if no write is needed) |
| 853 | * @return length of the first contiguous area which needs to be written |
| 854 | * 0 if no write is needed |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 855 | * |
| 856 | * FIXME: This function needs a parameter which tells it about coalescing |
| 857 | * in relation to the max write length of the programmer and the max write |
| 858 | * length of the chip. |
| 859 | */ |
| 860 | static int get_next_write(uint8_t *have, uint8_t *want, int len, |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 861 | int *first_start, enum write_granularity gran) |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 862 | { |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 863 | int need_write = 0, rel_start = 0, first_len = 0; |
| 864 | int i, limit, stride; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 865 | |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 866 | switch (gran) { |
| 867 | case write_gran_1bit: |
| 868 | case write_gran_1byte: |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 869 | stride = 1; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 870 | break; |
| 871 | case write_gran_256bytes: |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 872 | stride = 256; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 873 | break; |
| 874 | default: |
| 875 | msg_cerr("%s: Unsupported granularity! Please report a bug at " |
| 876 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 877 | /* Claim that no write was needed. A write with unknown |
| 878 | * granularity is too dangerous to try. |
| 879 | */ |
| 880 | return 0; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 881 | } |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 882 | for (i = 0; i < len / stride; i++) { |
| 883 | limit = min(stride, len - i * stride); |
| 884 | /* Are 'have' and 'want' identical? */ |
| 885 | if (memcmp(have + i * stride, want + i * stride, limit)) { |
| 886 | if (!need_write) { |
| 887 | /* First location where have and want differ. */ |
| 888 | need_write = 1; |
| 889 | rel_start = i * stride; |
| 890 | } |
| 891 | } else { |
| 892 | if (need_write) { |
| 893 | /* First location where have and want |
| 894 | * do not differ anymore. |
| 895 | */ |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 896 | break; |
| 897 | } |
| 898 | } |
| 899 | } |
Carl-Daniel Hailfinger | 202bf53 | 2010-12-06 13:05:44 +0000 | [diff] [blame] | 900 | if (need_write) |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 901 | first_len = min(i * stride - rel_start, len); |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 902 | *first_start += rel_start; |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 903 | return first_len; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Carl-Daniel Hailfinger | eaac68b | 2009-11-23 12:55:31 +0000 | [diff] [blame] | 906 | /* This function generates various test patterns useful for testing controller |
| 907 | * and chip communication as well as chip behaviour. |
| 908 | * |
| 909 | * If a byte can be written multiple times, each time keeping 0-bits at 0 |
| 910 | * and changing 1-bits to 0 if the new value for that bit is 0, the effect |
| 911 | * is essentially an AND operation. That's also the reason why this function |
| 912 | * provides the result of AND between various patterns. |
| 913 | * |
| 914 | * Below is a list of patterns (and their block length). |
| 915 | * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes) |
| 916 | * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes) |
| 917 | * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes) |
| 918 | * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes) |
| 919 | * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes) |
| 920 | * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes) |
| 921 | * Pattern 6 is 00 (1 Byte) |
| 922 | * Pattern 7 is ff (1 Byte) |
| 923 | * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256 |
| 924 | * byte block. |
| 925 | * |
| 926 | * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B) |
| 927 | * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B) |
| 928 | * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter) |
| 929 | * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards) |
| 930 | * Pattern 12 is 00 (1 Byte) |
| 931 | * Pattern 13 is ff (1 Byte) |
| 932 | * Patterns 8-13 have no block number. |
| 933 | * |
| 934 | * Patterns 0-3 are created to detect and efficiently diagnose communication |
| 935 | * slips like missed bits or bytes and their repetitive nature gives good visual |
| 936 | * cues to the person inspecting the results. In addition, the following holds: |
| 937 | * AND Pattern 0/1 == Pattern 4 |
| 938 | * AND Pattern 2/3 == Pattern 5 |
| 939 | * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6 |
| 940 | * A weakness of pattern 0-5 is the inability to detect swaps/copies between |
| 941 | * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc. |
| 942 | * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes. |
| 943 | * 0x5 and 0xa were picked because they are 0101 and 1010 binary. |
| 944 | * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes. |
| 945 | * Besides that, they provide for bit testing of the last two bytes of every |
| 946 | * 256 byte block which contains the block number for patterns 0-6. |
| 947 | * Patterns 10-11 are special purpose for detecting subblock aliasing with |
| 948 | * block sizes >256 bytes (some Dataflash chips etc.) |
| 949 | * AND Pattern 8/9 == Pattern 12 |
| 950 | * AND Pattern 10/11 == Pattern 12 |
| 951 | * Pattern 13 is the completely erased state. |
| 952 | * None of the patterns can detect aliasing at boundaries which are a multiple |
| 953 | * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI). |
| 954 | */ |
| 955 | int generate_testpattern(uint8_t *buf, uint32_t size, int variant) |
| 956 | { |
| 957 | int i; |
| 958 | |
| 959 | if (!buf) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 960 | msg_gerr("Invalid buffer!\n"); |
Carl-Daniel Hailfinger | eaac68b | 2009-11-23 12:55:31 +0000 | [diff] [blame] | 961 | return 1; |
| 962 | } |
| 963 | |
| 964 | switch (variant) { |
| 965 | case 0: |
| 966 | for (i = 0; i < size; i++) |
| 967 | buf[i] = (i & 0xf) << 4 | 0x5; |
| 968 | break; |
| 969 | case 1: |
| 970 | for (i = 0; i < size; i++) |
| 971 | buf[i] = (i & 0xf) << 4 | 0xa; |
| 972 | break; |
| 973 | case 2: |
| 974 | for (i = 0; i < size; i++) |
| 975 | buf[i] = 0x50 | (i & 0xf); |
| 976 | break; |
| 977 | case 3: |
| 978 | for (i = 0; i < size; i++) |
| 979 | buf[i] = 0xa0 | (i & 0xf); |
| 980 | break; |
| 981 | case 4: |
| 982 | for (i = 0; i < size; i++) |
| 983 | buf[i] = (i & 0xf) << 4; |
| 984 | break; |
| 985 | case 5: |
| 986 | for (i = 0; i < size; i++) |
| 987 | buf[i] = i & 0xf; |
| 988 | break; |
| 989 | case 6: |
| 990 | memset(buf, 0x00, size); |
| 991 | break; |
| 992 | case 7: |
| 993 | memset(buf, 0xff, size); |
| 994 | break; |
| 995 | case 8: |
| 996 | for (i = 0; i < size; i++) |
| 997 | buf[i] = i & 0xff; |
| 998 | break; |
| 999 | case 9: |
| 1000 | for (i = 0; i < size; i++) |
| 1001 | buf[i] = ~(i & 0xff); |
| 1002 | break; |
| 1003 | case 10: |
| 1004 | for (i = 0; i < size % 2; i++) { |
| 1005 | buf[i * 2] = (i >> 8) & 0xff; |
| 1006 | buf[i * 2 + 1] = i & 0xff; |
| 1007 | } |
| 1008 | if (size & 0x1) |
| 1009 | buf[i * 2] = (i >> 8) & 0xff; |
| 1010 | break; |
| 1011 | case 11: |
| 1012 | for (i = 0; i < size % 2; i++) { |
| 1013 | buf[i * 2] = ~((i >> 8) & 0xff); |
| 1014 | buf[i * 2 + 1] = ~(i & 0xff); |
| 1015 | } |
| 1016 | if (size & 0x1) |
| 1017 | buf[i * 2] = ~((i >> 8) & 0xff); |
| 1018 | break; |
| 1019 | case 12: |
| 1020 | memset(buf, 0x00, size); |
| 1021 | break; |
| 1022 | case 13: |
| 1023 | memset(buf, 0xff, size); |
| 1024 | break; |
| 1025 | } |
| 1026 | |
| 1027 | if ((variant >= 0) && (variant <= 7)) { |
| 1028 | /* Write block number in the last two bytes of each 256-byte |
| 1029 | * block, big endian for easier reading of the hexdump. |
| 1030 | * Note that this wraps around for chips larger than 2^24 bytes |
| 1031 | * (16 MB). |
| 1032 | */ |
| 1033 | for (i = 0; i < size / 256; i++) { |
| 1034 | buf[i * 256 + 254] = (i >> 8) & 0xff; |
| 1035 | buf[i * 256 + 255] = i & 0xff; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1042 | int check_max_decode(enum chipbustype buses, uint32_t size) |
| 1043 | { |
| 1044 | int limitexceeded = 0; |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1045 | |
| 1046 | if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) { |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1047 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1048 | msg_pdbg("Chip size %u kB is bigger than supported " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1049 | "size %u kB of chipset/board/programmer " |
| 1050 | "for %s interface, " |
| 1051 | "probe/read/erase/write may fail. ", size / 1024, |
| 1052 | max_rom_decode.parallel / 1024, "Parallel"); |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1053 | } |
Carl-Daniel Hailfinger | 1a22795 | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 1054 | if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) { |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1055 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1056 | msg_pdbg("Chip size %u kB is bigger than supported " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1057 | "size %u kB of chipset/board/programmer " |
| 1058 | "for %s interface, " |
| 1059 | "probe/read/erase/write may fail. ", size / 1024, |
| 1060 | max_rom_decode.lpc / 1024, "LPC"); |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1061 | } |
Carl-Daniel Hailfinger | 1a22795 | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 1062 | if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) { |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1063 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1064 | msg_pdbg("Chip size %u kB is bigger than supported " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1065 | "size %u kB of chipset/board/programmer " |
| 1066 | "for %s interface, " |
| 1067 | "probe/read/erase/write may fail. ", size / 1024, |
| 1068 | max_rom_decode.fwh / 1024, "FWH"); |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1069 | } |
Carl-Daniel Hailfinger | 1a22795 | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 1070 | if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) { |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1071 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1072 | msg_pdbg("Chip size %u kB is bigger than supported " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1073 | "size %u kB of chipset/board/programmer " |
| 1074 | "for %s interface, " |
| 1075 | "probe/read/erase/write may fail. ", size / 1024, |
| 1076 | max_rom_decode.spi / 1024, "SPI"); |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1077 | } |
| 1078 | if (!limitexceeded) |
| 1079 | return 0; |
| 1080 | /* Sometimes chip and programmer have more than one bus in common, |
| 1081 | * and the limit is not exceeded on all buses. Tell the user. |
| 1082 | */ |
| 1083 | if (bitcount(buses) > limitexceeded) |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 1084 | /* FIXME: This message is designed towards CLI users. */ |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1085 | msg_pdbg("There is at least one common chip/programmer " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1086 | "interface which can support a chip of this size. " |
| 1087 | "You can try --force at your own risk.\n"); |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1088 | return 1; |
| 1089 | } |
| 1090 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1091 | int probe_flash(int startchip, struct flashchip *fill_flash, int force) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1092 | { |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1093 | const struct flashchip *flash; |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1094 | unsigned long base = 0; |
Stefan Reinauer | 051e236 | 2011-01-19 06:21:54 +0000 | [diff] [blame] | 1095 | char location[64]; |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1096 | uint32_t size; |
| 1097 | enum chipbustype buses_common; |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1098 | char *tmp; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1099 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1100 | for (flash = flashchips + startchip; flash && flash->name; flash++) { |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1101 | if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1102 | continue; |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1103 | buses_common = buses_supported & flash->bustype; |
| 1104 | if (!buses_common) { |
Carl-Daniel Hailfinger | 6573b74 | 2011-06-17 22:38:53 +0000 | [diff] [blame] | 1105 | msg_gspew("Probing for %s %s, %d kB: skipped. ", |
| 1106 | flash->vendor, flash->name, flash->total_size); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1107 | tmp = flashbuses_to_text(buses_supported); |
Carl-Daniel Hailfinger | 6573b74 | 2011-06-17 22:38:53 +0000 | [diff] [blame] | 1108 | msg_gspew("Host bus type %s ", tmp); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1109 | free(tmp); |
| 1110 | tmp = flashbuses_to_text(flash->bustype); |
Carl-Daniel Hailfinger | 12575e5 | 2010-03-22 23:43:51 +0000 | [diff] [blame] | 1111 | msg_gspew("and chip bus type %s are incompatible.", |
| 1112 | tmp); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1113 | free(tmp); |
Carl-Daniel Hailfinger | 6573b74 | 2011-06-17 22:38:53 +0000 | [diff] [blame] | 1114 | msg_gspew("\n"); |
| 1115 | continue; |
| 1116 | } |
| 1117 | msg_gdbg("Probing for %s %s, %d kB: ", |
| 1118 | flash->vendor, flash->name, flash->total_size); |
| 1119 | if (!flash->probe && !force) { |
| 1120 | msg_gdbg("failed! flashrom has no probe function for " |
| 1121 | "this flash chip.\n"); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1122 | continue; |
| 1123 | } |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 1124 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1125 | size = flash->total_size * 1024; |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1126 | check_max_decode(buses_common, size); |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 1127 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1128 | /* Start filling in the dynamic data. */ |
| 1129 | *fill_flash = *flash; |
| 1130 | |
Carl-Daniel Hailfinger | 97d6b09 | 2009-05-09 07:27:23 +0000 | [diff] [blame] | 1131 | base = flashbase ? flashbase : (0xffffffff - size + 1); |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1132 | fill_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] | 1133 | |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1134 | if (force) |
| 1135 | break; |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 1136 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1137 | if (fill_flash->probe(fill_flash) != 1) |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 1138 | goto notfound; |
| 1139 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1140 | /* If this is the first chip found, accept it. |
| 1141 | * If this is not the first chip found, accept it only if it is |
| 1142 | * a non-generic match. |
| 1143 | * We could either make chipcount global or provide it as |
| 1144 | * parameter, or we assume that startchip==0 means this call to |
| 1145 | * probe_flash() is the first one and thus no chip has been |
| 1146 | * found before. |
| 1147 | */ |
| 1148 | if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID) |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 1151 | notfound: |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1152 | programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1153 | } |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 1154 | |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1155 | if (!flash || !flash->name) |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1156 | return -1; |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1157 | |
Carl-Daniel Hailfinger | d953558 | 2011-03-08 00:09:11 +0000 | [diff] [blame] | 1158 | #if CONFIG_INTERNAL == 1 |
| 1159 | if (programmer_table[programmer].map_flash_region == physmap) |
Stefan Reinauer | 051e236 | 2011-01-19 06:21:54 +0000 | [diff] [blame] | 1160 | snprintf(location, sizeof(location), "at physical address 0x%lx", base); |
Carl-Daniel Hailfinger | d953558 | 2011-03-08 00:09:11 +0000 | [diff] [blame] | 1161 | else |
| 1162 | #endif |
Stefan Reinauer | 051e236 | 2011-01-19 06:21:54 +0000 | [diff] [blame] | 1163 | snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name); |
Stefan Reinauer | 051e236 | 2011-01-19 06:21:54 +0000 | [diff] [blame] | 1164 | |
Stefan Tauner | 0015549 | 2011-06-26 20:45:35 +0000 | [diff] [blame] | 1165 | tmp = flashbuses_to_text(flash->bustype); |
Stefan Tauner | 716e098 | 2011-07-25 20:38:52 +0000 | [diff] [blame] | 1166 | msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) %s.\n", |
Stefan Tauner | 0015549 | 2011-06-26 20:45:35 +0000 | [diff] [blame] | 1167 | force ? "Assuming" : "Found", fill_flash->vendor, |
| 1168 | fill_flash->name, fill_flash->total_size, tmp, location); |
| 1169 | free(tmp); |
Uwe Hermann | 9899cad | 2009-06-28 21:47:57 +0000 | [diff] [blame] | 1170 | |
Carl-Daniel Hailfinger | 859f3f0 | 2010-12-02 21:59:42 +0000 | [diff] [blame] | 1171 | /* Flash registers will not be mapped if the chip was forced. Lock info |
| 1172 | * may be stored in registers, so avoid lock info printing. |
| 1173 | */ |
| 1174 | if (!force) |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1175 | if (fill_flash->printlock) |
| 1176 | fill_flash->printlock(fill_flash); |
Sean Nelson | 6e0b912 | 2010-02-19 00:52:10 +0000 | [diff] [blame] | 1177 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1178 | /* Return position of matching chip. */ |
| 1179 | return flash - flashchips; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Stefan Reinauer | e370528 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 1182 | int verify_flash(struct flashchip *flash, uint8_t *buf) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1183 | { |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 1184 | int ret; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 1185 | int total_size = flash->total_size * 1024; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1186 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1187 | msg_cinfo("Verifying flash... "); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1188 | |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 1189 | ret = verify_range(flash, buf, 0, total_size, NULL); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1190 | |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 1191 | if (!ret) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1192 | msg_cinfo("VERIFIED. \n"); |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 1193 | |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 1194 | return ret; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1197 | int read_buf_from_file(unsigned char *buf, unsigned long size, |
| 1198 | const char *filename) |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1199 | { |
| 1200 | unsigned long numbytes; |
| 1201 | FILE *image; |
| 1202 | struct stat image_stat; |
| 1203 | |
| 1204 | if ((image = fopen(filename, "rb")) == NULL) { |
| 1205 | perror(filename); |
| 1206 | return 1; |
| 1207 | } |
| 1208 | if (fstat(fileno(image), &image_stat) != 0) { |
| 1209 | perror(filename); |
| 1210 | fclose(image); |
| 1211 | return 1; |
| 1212 | } |
| 1213 | if (image_stat.st_size != size) { |
| 1214 | msg_gerr("Error: Image size doesn't match\n"); |
| 1215 | fclose(image); |
| 1216 | return 1; |
| 1217 | } |
| 1218 | numbytes = fread(buf, 1, size, image); |
| 1219 | if (fclose(image)) { |
| 1220 | perror(filename); |
| 1221 | return 1; |
| 1222 | } |
| 1223 | if (numbytes != size) { |
| 1224 | msg_gerr("Error: Failed to read complete file. Got %ld bytes, " |
| 1225 | "wanted %ld!\n", numbytes, size); |
| 1226 | return 1; |
| 1227 | } |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1231 | int write_buf_to_file(unsigned char *buf, unsigned long size, |
| 1232 | const char *filename) |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1233 | { |
| 1234 | unsigned long numbytes; |
| 1235 | FILE *image; |
Stephan Guilloux | 21dd55b | 2009-06-01 22:07:52 +0000 | [diff] [blame] | 1236 | |
| 1237 | if (!filename) { |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1238 | msg_gerr("No filename specified.\n"); |
Stephan Guilloux | 21dd55b | 2009-06-01 22:07:52 +0000 | [diff] [blame] | 1239 | return 1; |
| 1240 | } |
Patrick Georgi | 0bf842d | 2010-01-25 22:55:33 +0000 | [diff] [blame] | 1241 | if ((image = fopen(filename, "wb")) == NULL) { |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1242 | perror(filename); |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 1243 | return 1; |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1244 | } |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1245 | |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1246 | numbytes = fwrite(buf, 1, size, image); |
| 1247 | fclose(image); |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1248 | if (numbytes != size) { |
| 1249 | msg_gerr("File %s could not be written completely.\n", |
| 1250 | filename); |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1251 | return 1; |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1252 | } |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1253 | return 0; |
| 1254 | } |
| 1255 | |
Stefan Tauner | 6665244 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 1256 | int read_flash_to_file(struct flashchip *flash, const char *filename) |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1257 | { |
| 1258 | unsigned long size = flash->total_size * 1024; |
| 1259 | unsigned char *buf = calloc(size, sizeof(char)); |
| 1260 | int ret = 0; |
| 1261 | |
| 1262 | msg_cinfo("Reading flash... "); |
| 1263 | if (!buf) { |
| 1264 | msg_gerr("Memory allocation failed!\n"); |
| 1265 | msg_cinfo("FAILED.\n"); |
| 1266 | return 1; |
| 1267 | } |
| 1268 | if (!flash->read) { |
| 1269 | msg_cerr("No read function available for this flash chip.\n"); |
| 1270 | ret = 1; |
| 1271 | goto out_free; |
| 1272 | } |
| 1273 | if (flash->read(flash, buf, 0, size)) { |
| 1274 | msg_cerr("Read operation failed!\n"); |
| 1275 | ret = 1; |
| 1276 | goto out_free; |
| 1277 | } |
| 1278 | |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 1279 | ret = write_buf_to_file(buf, size, filename); |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1280 | out_free: |
| 1281 | free(buf); |
| 1282 | msg_cinfo("%s.\n", ret ? "FAILED" : "done"); |
| 1283 | return ret; |
| 1284 | } |
| 1285 | |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1286 | /* This function shares a lot of its structure with erase_and_write_flash() and |
| 1287 | * walk_eraseregions(). |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1288 | * Even if an error is found, the function will keep going and check the rest. |
| 1289 | */ |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1290 | static int selfcheck_eraseblocks(const struct flashchip *flash) |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1291 | { |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 1292 | int i, j, k; |
| 1293 | int ret = 0; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1294 | |
| 1295 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
| 1296 | unsigned int done = 0; |
| 1297 | struct block_eraser eraser = flash->block_erasers[k]; |
| 1298 | |
| 1299 | for (i = 0; i < NUM_ERASEREGIONS; i++) { |
| 1300 | /* Blocks with zero size are bugs in flashchips.c. */ |
| 1301 | if (eraser.eraseblocks[i].count && |
| 1302 | !eraser.eraseblocks[i].size) { |
| 1303 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1304 | "%i region %i has size 0. Please report" |
| 1305 | " a bug at flashrom@flashrom.org\n", |
| 1306 | flash->name, k, i); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1307 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1308 | } |
| 1309 | /* Blocks with zero count are bugs in flashchips.c. */ |
| 1310 | if (!eraser.eraseblocks[i].count && |
| 1311 | eraser.eraseblocks[i].size) { |
| 1312 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1313 | "%i region %i has count 0. Please report" |
| 1314 | " a bug at flashrom@flashrom.org\n", |
| 1315 | flash->name, k, i); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1316 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1317 | } |
| 1318 | done += eraser.eraseblocks[i].count * |
| 1319 | eraser.eraseblocks[i].size; |
| 1320 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1321 | /* Empty eraseblock definition with erase function. */ |
| 1322 | if (!done && eraser.block_erase) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1323 | msg_gspew("Strange: Empty eraseblock definition with " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1324 | "non-empty erase function. Not an error.\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1325 | if (!done) |
| 1326 | continue; |
| 1327 | if (done != flash->total_size * 1024) { |
| 1328 | msg_gerr("ERROR: Flash chip %s erase function %i " |
| 1329 | "region walking resulted in 0x%06x bytes total," |
| 1330 | " expected 0x%06x bytes. Please report a bug at" |
| 1331 | " flashrom@flashrom.org\n", flash->name, k, |
| 1332 | done, flash->total_size * 1024); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1333 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1334 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1335 | if (!eraser.block_erase) |
| 1336 | continue; |
| 1337 | /* Check if there are identical erase functions for different |
| 1338 | * layouts. That would imply "magic" erase functions. The |
| 1339 | * easiest way to check this is with function pointers. |
| 1340 | */ |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 1341 | for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) { |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1342 | if (eraser.block_erase == |
| 1343 | flash->block_erasers[j].block_erase) { |
| 1344 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1345 | "%i and %i are identical. Please report" |
| 1346 | " a bug at flashrom@flashrom.org\n", |
| 1347 | flash->name, k, j); |
| 1348 | ret = 1; |
| 1349 | } |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 1350 | } |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1351 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1352 | return ret; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1355 | static int erase_and_write_block_helper(struct flashchip *flash, |
| 1356 | unsigned int start, unsigned int len, |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1357 | uint8_t *curcontents, |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1358 | uint8_t *newcontents, |
| 1359 | int (*erasefn) (struct flashchip *flash, |
| 1360 | unsigned int addr, |
| 1361 | unsigned int len)) |
| 1362 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1363 | int starthere = 0, lenhere = 0, ret = 0, skip = 1, writecount = 0; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1364 | enum write_granularity gran = write_gran_256bytes; /* FIXME */ |
| 1365 | |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1366 | /* curcontents and newcontents are opaque to walk_eraseregions, and |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1367 | * need to be adjusted here to keep the impression of proper abstraction |
| 1368 | */ |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1369 | curcontents += start; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1370 | newcontents += start; |
| 1371 | msg_cdbg(":"); |
| 1372 | /* FIXME: Assume 256 byte granularity for now to play it safe. */ |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1373 | if (need_erase(curcontents, newcontents, len, gran)) { |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1374 | msg_cdbg("E"); |
| 1375 | ret = erasefn(flash, start, len); |
| 1376 | if (ret) |
| 1377 | return ret; |
Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 1378 | if (check_erased_range(flash, start, len)) { |
| 1379 | msg_cerr("ERASE FAILED!\n"); |
| 1380 | return -1; |
| 1381 | } |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1382 | /* Erase was successful. Adjust curcontents. */ |
| 1383 | memset(curcontents, 0xff, len); |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1384 | skip = 0; |
| 1385 | } |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1386 | /* get_next_write() sets starthere to a new value after the call. */ |
| 1387 | while ((lenhere = get_next_write(curcontents + starthere, |
| 1388 | newcontents + starthere, |
| 1389 | len - starthere, &starthere, gran))) { |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1390 | if (!writecount++) |
| 1391 | msg_cdbg("W"); |
| 1392 | /* Needs the partial write function signature. */ |
| 1393 | ret = flash->write(flash, newcontents + starthere, |
| 1394 | start + starthere, lenhere); |
| 1395 | if (ret) |
| 1396 | return ret; |
| 1397 | starthere += lenhere; |
| 1398 | skip = 0; |
| 1399 | } |
| 1400 | if (skip) |
| 1401 | msg_cdbg("S"); |
| 1402 | return ret; |
| 1403 | } |
| 1404 | |
Carl-Daniel Hailfinger | 4d3e9ca | 2010-07-13 00:42:00 +0000 | [diff] [blame] | 1405 | static int walk_eraseregions(struct flashchip *flash, int erasefunction, |
| 1406 | int (*do_something) (struct flashchip *flash, |
| 1407 | unsigned int addr, |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1408 | unsigned int len, |
| 1409 | uint8_t *param1, |
| 1410 | uint8_t *param2, |
| 1411 | int (*erasefn) ( |
| 1412 | struct flashchip *flash, |
| 1413 | unsigned int addr, |
| 1414 | unsigned int len)), |
| 1415 | void *param1, void *param2) |
Carl-Daniel Hailfinger | a122341 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1416 | { |
| 1417 | int i, j; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 1418 | unsigned int start = 0; |
| 1419 | unsigned int len; |
Carl-Daniel Hailfinger | a122341 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1420 | struct block_eraser eraser = flash->block_erasers[erasefunction]; |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1421 | |
Carl-Daniel Hailfinger | a122341 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1422 | for (i = 0; i < NUM_ERASEREGIONS; i++) { |
| 1423 | /* count==0 for all automatically initialized array |
| 1424 | * members so the loop below won't be executed for them. |
| 1425 | */ |
| 1426 | len = eraser.eraseblocks[i].size; |
| 1427 | for (j = 0; j < eraser.eraseblocks[i].count; j++) { |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1428 | /* Print this for every block except the first one. */ |
| 1429 | if (i || j) |
| 1430 | msg_cdbg(", "); |
| 1431 | msg_cdbg("0x%06x-0x%06x", start, |
Carl-Daniel Hailfinger | a122341 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1432 | start + len - 1); |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1433 | if (do_something(flash, start, len, param1, param2, |
| 1434 | eraser.block_erase)) { |
Carl-Daniel Hailfinger | a122341 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1435 | return 1; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1436 | } |
Carl-Daniel Hailfinger | a122341 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1437 | start += len; |
| 1438 | } |
| 1439 | } |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1440 | msg_cdbg("\n"); |
Carl-Daniel Hailfinger | a122341 | 2010-07-13 00:37:19 +0000 | [diff] [blame] | 1441 | return 0; |
| 1442 | } |
| 1443 | |
Stefan Tauner | 5368dca | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 1444 | static int check_block_eraser(const struct flashchip *flash, int k, int log) |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1445 | { |
| 1446 | struct block_eraser eraser = flash->block_erasers[k]; |
| 1447 | |
| 1448 | if (!eraser.block_erase && !eraser.eraseblocks[0].count) { |
| 1449 | if (log) |
| 1450 | msg_cdbg("not defined. "); |
| 1451 | return 1; |
| 1452 | } |
| 1453 | if (!eraser.block_erase && eraser.eraseblocks[0].count) { |
| 1454 | if (log) |
| 1455 | msg_cdbg("eraseblock layout is known, but matching " |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 1456 | "block erase function is not implemented. "); |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1457 | return 1; |
| 1458 | } |
| 1459 | if (eraser.block_erase && !eraser.eraseblocks[0].count) { |
| 1460 | if (log) |
| 1461 | msg_cdbg("block erase function found, but " |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 1462 | "eraseblock layout is not defined. "); |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1463 | return 1; |
| 1464 | } |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1468 | int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents, |
| 1469 | uint8_t *newcontents) |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1470 | { |
Carl-Daniel Hailfinger | d8334db | 2011-07-21 21:21:04 +0000 | [diff] [blame] | 1471 | int k, ret = 1; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1472 | uint8_t *curcontents; |
| 1473 | unsigned long size = flash->total_size * 1024; |
Sylvain "ythier" Hitier | 9db4551 | 2011-07-04 07:27:17 +0000 | [diff] [blame] | 1474 | unsigned int usable_erasefunctions = count_usable_erasers(flash); |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1475 | |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1476 | msg_cinfo("Erasing and writing flash chip... "); |
Stefan Tauner | 269de35 | 2011-07-12 22:35:21 +0000 | [diff] [blame] | 1477 | curcontents = malloc(size); |
| 1478 | if (!curcontents) { |
| 1479 | msg_gerr("Out of memory!\n"); |
| 1480 | exit(1); |
| 1481 | } |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1482 | /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */ |
| 1483 | memcpy(curcontents, oldcontents, size); |
| 1484 | |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1485 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
Carl-Daniel Hailfinger | 84d1968 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1486 | if (k != 0) |
| 1487 | msg_cdbg("Looking for another erase function.\n"); |
Carl-Daniel Hailfinger | d8334db | 2011-07-21 21:21:04 +0000 | [diff] [blame] | 1488 | if (!usable_erasefunctions) { |
| 1489 | msg_cdbg("No usable erase functions left.\n"); |
| 1490 | break; |
| 1491 | } |
Carl-Daniel Hailfinger | 84d1968 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1492 | msg_cdbg("Trying erase function %i... ", k); |
| 1493 | if (check_block_eraser(flash, k, 1)) |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1494 | continue; |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1495 | usable_erasefunctions--; |
Carl-Daniel Hailfinger | 84d1968 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1496 | ret = walk_eraseregions(flash, k, &erase_and_write_block_helper, |
| 1497 | curcontents, newcontents); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1498 | /* If everything is OK, don't try another erase function. */ |
| 1499 | if (!ret) |
| 1500 | break; |
Carl-Daniel Hailfinger | b23b1eb | 2010-12-02 02:41:55 +0000 | [diff] [blame] | 1501 | /* Write/erase failed, so try to find out what the current chip |
Carl-Daniel Hailfinger | d8334db | 2011-07-21 21:21:04 +0000 | [diff] [blame] | 1502 | * contents are. If no usable erase functions remain, we can |
| 1503 | * skip this: the next iteration will break immediately anyway. |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1504 | */ |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1505 | if (!usable_erasefunctions) |
| 1506 | continue; |
Carl-Daniel Hailfinger | 84d1968 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1507 | /* Reading the whole chip may take a while, inform the user even |
| 1508 | * in non-verbose mode. |
| 1509 | */ |
| 1510 | msg_cinfo("Reading current flash chip contents... "); |
Carl-Daniel Hailfinger | b23b1eb | 2010-12-02 02:41:55 +0000 | [diff] [blame] | 1511 | if (flash->read(flash, curcontents, 0, size)) { |
| 1512 | /* Now we are truly screwed. Read failed as well. */ |
Carl-Daniel Hailfinger | 84d1968 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1513 | msg_cerr("Can't read anymore! Aborting.\n"); |
Carl-Daniel Hailfinger | b23b1eb | 2010-12-02 02:41:55 +0000 | [diff] [blame] | 1514 | /* We have no idea about the flash chip contents, so |
| 1515 | * retrying with another erase function is pointless. |
| 1516 | */ |
| 1517 | break; |
| 1518 | } |
Carl-Daniel Hailfinger | 84d1968 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1519 | msg_cinfo("done. "); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1520 | } |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1521 | /* Free the scratchpad. */ |
| 1522 | free(curcontents); |
Carl-Daniel Hailfinger | f160a12 | 2009-05-08 17:15:15 +0000 | [diff] [blame] | 1523 | |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1524 | if (ret) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1525 | msg_cerr("FAILED!\n"); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1526 | } else { |
Carl-Daniel Hailfinger | 84d1968 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1527 | msg_cinfo("Erase/write done.\n"); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1528 | } |
| 1529 | return ret; |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1532 | void nonfatal_help_message(void) |
| 1533 | { |
| 1534 | msg_gerr("Writing to the flash chip apparently didn't do anything.\n" |
| 1535 | "This means we have to add special support for your board, " |
| 1536 | "programmer or flash chip.\n" |
| 1537 | "Please report this on IRC at irc.freenode.net (channel " |
| 1538 | "#flashrom) or\n" |
| 1539 | "mail flashrom@flashrom.org!\n" |
| 1540 | "-------------------------------------------------------------" |
| 1541 | "------------------\n" |
| 1542 | "You may now reboot or simply leave the machine running.\n"); |
| 1543 | } |
| 1544 | |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 1545 | void emergency_help_message(void) |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1546 | { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1547 | msg_gerr("Your flash chip is in an unknown state.\n" |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 1548 | "Get help on IRC at irc.freenode.net (channel #flashrom) or\n" |
Paul Menzel | ab6328f | 2010-10-08 11:03:02 +0000 | [diff] [blame] | 1549 | "mail flashrom@flashrom.org with FAILED: your board name in " |
| 1550 | "the subject line!\n" |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1551 | "-------------------------------------------------------------" |
| 1552 | "------------------\n" |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1553 | "DO NOT REBOOT OR POWEROFF!\n"); |
| 1554 | } |
| 1555 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1556 | /* The way to go if you want a delimited list of programmers */ |
Stefan Tauner | 6665244 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 1557 | void list_programmers(const char *delim) |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1558 | { |
| 1559 | enum programmer p; |
| 1560 | for (p = 0; p < PROGRAMMER_INVALID; p++) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1561 | msg_ginfo("%s", programmer_table[p].name); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1562 | if (p < PROGRAMMER_INVALID - 1) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1563 | msg_ginfo("%s", delim); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1564 | } |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1565 | msg_ginfo("\n"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1568 | void list_programmers_linebreak(int startcol, int cols, int paren) |
| 1569 | { |
| 1570 | const char *pname; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 1571 | int pnamelen; |
| 1572 | int remaining = 0, firstline = 1; |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1573 | enum programmer p; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 1574 | int i; |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1575 | |
| 1576 | for (p = 0; p < PROGRAMMER_INVALID; p++) { |
| 1577 | pname = programmer_table[p].name; |
| 1578 | pnamelen = strlen(pname); |
| 1579 | if (remaining - pnamelen - 2 < 0) { |
| 1580 | if (firstline) |
| 1581 | firstline = 0; |
| 1582 | else |
| 1583 | printf("\n"); |
| 1584 | for (i = 0; i < startcol; i++) |
| 1585 | printf(" "); |
| 1586 | remaining = cols - startcol; |
| 1587 | } else { |
| 1588 | printf(" "); |
| 1589 | remaining--; |
| 1590 | } |
| 1591 | if (paren && (p == 0)) { |
| 1592 | printf("("); |
| 1593 | remaining--; |
| 1594 | } |
| 1595 | printf("%s", pname); |
| 1596 | remaining -= pnamelen; |
| 1597 | if (p < PROGRAMMER_INVALID - 1) { |
| 1598 | printf(","); |
| 1599 | remaining--; |
| 1600 | } else { |
| 1601 | if (paren) |
| 1602 | printf(")"); |
| 1603 | printf("\n"); |
| 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1608 | void print_sysinfo(void) |
| 1609 | { |
| 1610 | #if HAVE_UTSNAME == 1 |
| 1611 | struct utsname osinfo; |
| 1612 | uname(&osinfo); |
| 1613 | |
| 1614 | msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release, |
| 1615 | osinfo.machine); |
| 1616 | #else |
| 1617 | msg_ginfo(" on unknown machine"); |
| 1618 | #endif |
| 1619 | msg_ginfo(", built with"); |
| 1620 | #if NEED_PCI == 1 |
| 1621 | #ifdef PCILIB_VERSION |
| 1622 | msg_ginfo(" libpci %s,", PCILIB_VERSION); |
| 1623 | #else |
| 1624 | msg_ginfo(" unknown PCI library,"); |
| 1625 | #endif |
| 1626 | #endif |
| 1627 | #ifdef __clang__ |
Carl-Daniel Hailfinger | b51e58e | 2010-07-17 14:49:30 +0000 | [diff] [blame] | 1628 | msg_ginfo(" LLVM Clang"); |
| 1629 | #ifdef __clang_version__ |
| 1630 | msg_ginfo(" %s,", __clang_version__); |
| 1631 | #else |
| 1632 | msg_ginfo(" unknown version (before r102686),"); |
| 1633 | #endif |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1634 | #elif defined(__GNUC__) |
| 1635 | msg_ginfo(" GCC"); |
| 1636 | #ifdef __VERSION__ |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1637 | msg_ginfo(" %s,", __VERSION__); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1638 | #else |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1639 | msg_ginfo(" unknown version,"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1640 | #endif |
| 1641 | #else |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1642 | msg_ginfo(" unknown compiler,"); |
| 1643 | #endif |
| 1644 | #if defined (__FLASHROM_LITTLE_ENDIAN__) |
| 1645 | msg_ginfo(" little endian"); |
| 1646 | #else |
| 1647 | msg_ginfo(" big endian"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1648 | #endif |
| 1649 | msg_ginfo("\n"); |
| 1650 | } |
| 1651 | |
Bernhard Walle | 201bde3 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 1652 | void print_version(void) |
| 1653 | { |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1654 | msg_ginfo("flashrom v%s", flashrom_version); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1655 | print_sysinfo(); |
Bernhard Walle | 201bde3 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1658 | void print_banner(void) |
| 1659 | { |
| 1660 | msg_ginfo("flashrom is free software, get the source code at " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1661 | "http://www.flashrom.org\n"); |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1662 | msg_ginfo("\n"); |
| 1663 | } |
| 1664 | |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1665 | int selfcheck(void) |
| 1666 | { |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1667 | int ret = 0; |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1668 | const struct flashchip *flash; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1669 | |
| 1670 | /* Safety check. Instead of aborting after the first error, check |
| 1671 | * if more errors exist. |
| 1672 | */ |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1673 | if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1674 | msg_gerr("Programmer table miscompilation!\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1675 | ret = 1; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1676 | } |
Stefan Tauner | 93f7023 | 2011-07-26 14:33:46 +0000 | [diff] [blame] | 1677 | /* It would be favorable if we could also check for correct termination |
Stefan Tauner | 716e098 | 2011-07-25 20:38:52 +0000 | [diff] [blame] | 1678 | * of the following arrays, but we don't know their sizes in here... |
Stefan Tauner | 7bcacb1 | 2011-05-26 01:35:19 +0000 | [diff] [blame] | 1679 | * For 'flashchips' we check the first element to be non-null. In the |
| 1680 | * other cases there exist use cases where the first element can be |
| 1681 | * null. */ |
| 1682 | if (flashchips == NULL || flashchips[0].vendor == NULL) { |
| 1683 | msg_gerr("Flashchips table miscompilation!\n"); |
| 1684 | ret = 1; |
| 1685 | } |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1686 | for (flash = flashchips; flash && flash->name; flash++) |
| 1687 | if (selfcheck_eraseblocks(flash)) |
| 1688 | ret = 1; |
Stefan Tauner | 7bcacb1 | 2011-05-26 01:35:19 +0000 | [diff] [blame] | 1689 | |
| 1690 | #if CONFIG_INTERNAL == 1 |
| 1691 | if (chipset_enables == NULL) { |
| 1692 | msg_gerr("Chipset enables table does not exist!\n"); |
| 1693 | ret = 1; |
| 1694 | } |
Carl-Daniel Hailfinger | 97d5b12 | 2011-08-31 16:19:50 +0000 | [diff] [blame] | 1695 | if (board_matches == NULL) { |
Stefan Tauner | 7bcacb1 | 2011-05-26 01:35:19 +0000 | [diff] [blame] | 1696 | msg_gerr("Board enables table does not exist!\n"); |
| 1697 | ret = 1; |
| 1698 | } |
| 1699 | if (boards_known == NULL) { |
| 1700 | msg_gerr("Known boards table does not exist!\n"); |
| 1701 | ret = 1; |
| 1702 | } |
| 1703 | if (laptops_known == NULL) { |
| 1704 | msg_gerr("Known laptops table does not exist!\n"); |
| 1705 | ret = 1; |
| 1706 | } |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1707 | #endif |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1708 | return ret; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1711 | void check_chip_supported(const struct flashchip *flash) |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1712 | { |
| 1713 | if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1714 | msg_cinfo("===\n"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1715 | if (flash->tested & TEST_BAD_MASK) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1716 | msg_cinfo("This flash part has status NOT WORKING for operations:"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1717 | if (flash->tested & TEST_BAD_PROBE) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1718 | msg_cinfo(" PROBE"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1719 | if (flash->tested & TEST_BAD_READ) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1720 | msg_cinfo(" READ"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1721 | if (flash->tested & TEST_BAD_ERASE) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1722 | msg_cinfo(" ERASE"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1723 | if (flash->tested & TEST_BAD_WRITE) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1724 | msg_cinfo(" WRITE"); |
| 1725 | msg_cinfo("\n"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1726 | } |
| 1727 | if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) || |
| 1728 | (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) || |
| 1729 | (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) || |
| 1730 | (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1731 | msg_cinfo("This flash part has status UNTESTED for operations:"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1732 | if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1733 | msg_cinfo(" PROBE"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1734 | if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1735 | msg_cinfo(" READ"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1736 | if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1737 | msg_cinfo(" ERASE"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1738 | if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE)) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1739 | msg_cinfo(" WRITE"); |
| 1740 | msg_cinfo("\n"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1741 | } |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 1742 | /* FIXME: This message is designed towards CLI users. */ |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1743 | msg_cinfo("The test status of this chip may have been updated " |
| 1744 | "in the latest development\n" |
| 1745 | "version of flashrom. If you are running the latest " |
| 1746 | "development version,\n" |
| 1747 | "please email a report to flashrom@flashrom.org if " |
| 1748 | "any of the above operations\n" |
| 1749 | "work correctly for you with this flash part. Please " |
| 1750 | "include the flashrom\n" |
| 1751 | "output with the additional -V option for all " |
| 1752 | "operations you tested (-V, -Vr,\n" |
| 1753 | "-Vw, -VE), and mention which mainboard or " |
| 1754 | "programmer you tested.\n" |
Paul Menzel | ab6328f | 2010-10-08 11:03:02 +0000 | [diff] [blame] | 1755 | "Please mention your board in the subject line. " |
| 1756 | "Thanks for your help!\n"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1757 | } |
| 1758 | } |
| 1759 | |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1760 | /* FIXME: This function signature needs to be improved once doit() has a better |
| 1761 | * function signature. |
| 1762 | */ |
David Hendricks | 77f931a | 2011-05-18 01:30:56 +0000 | [diff] [blame] | 1763 | int chip_safety_check(struct flashchip *flash, int force, int read_it, int write_it, int erase_it, int verify_it) |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1764 | { |
| 1765 | if (!programmer_may_write && (write_it || erase_it)) { |
| 1766 | msg_perr("Write/erase is not working yet on your programmer in " |
| 1767 | "its current configuration.\n"); |
| 1768 | /* --force is the wrong approach, but it's the best we can do |
| 1769 | * until the generic programmer parameter parser is merged. |
| 1770 | */ |
| 1771 | if (!force) |
| 1772 | return 1; |
| 1773 | msg_cerr("Continuing anyway.\n"); |
| 1774 | } |
| 1775 | |
| 1776 | if (read_it || erase_it || write_it || verify_it) { |
| 1777 | /* Everything needs read. */ |
| 1778 | if (flash->tested & TEST_BAD_READ) { |
| 1779 | msg_cerr("Read is not working on this chip. "); |
| 1780 | if (!force) |
| 1781 | return 1; |
| 1782 | msg_cerr("Continuing anyway.\n"); |
| 1783 | } |
| 1784 | if (!flash->read) { |
| 1785 | msg_cerr("flashrom has no read function for this " |
| 1786 | "flash chip.\n"); |
| 1787 | return 1; |
| 1788 | } |
| 1789 | } |
| 1790 | if (erase_it || write_it) { |
| 1791 | /* Write needs erase. */ |
| 1792 | if (flash->tested & TEST_BAD_ERASE) { |
| 1793 | msg_cerr("Erase is not working on this chip. "); |
| 1794 | if (!force) |
| 1795 | return 1; |
| 1796 | msg_cerr("Continuing anyway.\n"); |
| 1797 | } |
Sylvain "ythier" Hitier | 9db4551 | 2011-07-04 07:27:17 +0000 | [diff] [blame] | 1798 | if(count_usable_erasers(flash) == 0) { |
Stefan Tauner | 5368dca | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 1799 | msg_cerr("flashrom has no erase function for this " |
| 1800 | "flash chip.\n"); |
| 1801 | return 1; |
| 1802 | } |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1803 | } |
| 1804 | if (write_it) { |
| 1805 | if (flash->tested & TEST_BAD_WRITE) { |
| 1806 | msg_cerr("Write is not working on this chip. "); |
| 1807 | if (!force) |
| 1808 | return 1; |
| 1809 | msg_cerr("Continuing anyway.\n"); |
| 1810 | } |
| 1811 | if (!flash->write) { |
| 1812 | msg_cerr("flashrom has no write function for this " |
| 1813 | "flash chip.\n"); |
| 1814 | return 1; |
| 1815 | } |
| 1816 | } |
| 1817 | return 0; |
| 1818 | } |
| 1819 | |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1820 | /* This function signature is horrible. We need to design a better interface, |
| 1821 | * but right now it allows us to split off the CLI code. |
Carl-Daniel Hailfinger | f52f784 | 2010-10-08 18:52:29 +0000 | [diff] [blame] | 1822 | * Besides that, the function itself is a textbook example of abysmal code flow. |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1823 | */ |
Stefan Tauner | 6665244 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 1824 | int doit(struct flashchip *flash, int force, const char *filename, int read_it, int write_it, int erase_it, int verify_it) |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1825 | { |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1826 | uint8_t *oldcontents; |
| 1827 | uint8_t *newcontents; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1828 | int ret = 0; |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1829 | unsigned long size = flash->total_size * 1024; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1830 | |
David Hendricks | 77f931a | 2011-05-18 01:30:56 +0000 | [diff] [blame] | 1831 | if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) { |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1832 | msg_cerr("Aborting.\n"); |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1833 | ret = 1; |
| 1834 | goto out_nofree; |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1837 | /* Given the existence of read locks, we want to unlock for read, |
| 1838 | * erase and write. |
| 1839 | */ |
| 1840 | if (flash->unlock) |
| 1841 | flash->unlock(flash); |
| 1842 | |
| 1843 | if (read_it) { |
| 1844 | ret = read_flash_to_file(flash, filename); |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1845 | goto out_nofree; |
Carl-Daniel Hailfinger | d1be52d | 2010-07-03 12:14:25 +0000 | [diff] [blame] | 1846 | } |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1847 | |
Stefan Tauner | 269de35 | 2011-07-12 22:35:21 +0000 | [diff] [blame] | 1848 | oldcontents = malloc(size); |
| 1849 | if (!oldcontents) { |
| 1850 | msg_gerr("Out of memory!\n"); |
| 1851 | exit(1); |
| 1852 | } |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1853 | /* Assume worst case: All bits are 0. */ |
| 1854 | memset(oldcontents, 0x00, size); |
Stefan Tauner | 269de35 | 2011-07-12 22:35:21 +0000 | [diff] [blame] | 1855 | newcontents = malloc(size); |
| 1856 | if (!newcontents) { |
| 1857 | msg_gerr("Out of memory!\n"); |
| 1858 | exit(1); |
| 1859 | } |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1860 | /* Assume best case: All bits should be 1. */ |
| 1861 | memset(newcontents, 0xff, size); |
| 1862 | /* Side effect of the assumptions above: Default write action is erase |
| 1863 | * because newcontents looks like a completely erased chip, and |
| 1864 | * oldcontents being completely 0x00 means we have to erase everything |
| 1865 | * before we can write. |
| 1866 | */ |
| 1867 | |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 1868 | if (erase_it) { |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1869 | /* FIXME: Do we really want the scary warning if erase failed? |
| 1870 | * After all, after erase the chip is either blank or partially |
| 1871 | * blank or it has the old contents. A blank chip won't boot, |
| 1872 | * so if the user wanted erase and reboots afterwards, the user |
| 1873 | * knows very well that booting won't work. |
| 1874 | */ |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1875 | if (erase_and_write_flash(flash, oldcontents, newcontents)) { |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1876 | emergency_help_message(); |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1877 | ret = 1; |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1878 | } |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1879 | goto out; |
Carl-Daniel Hailfinger | f52f784 | 2010-10-08 18:52:29 +0000 | [diff] [blame] | 1880 | } |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1881 | |
Carl-Daniel Hailfinger | f52f784 | 2010-10-08 18:52:29 +0000 | [diff] [blame] | 1882 | if (write_it || verify_it) { |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1883 | if (read_buf_from_file(newcontents, size, filename)) { |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1884 | ret = 1; |
| 1885 | goto out; |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 1888 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1889 | if (programmer == PROGRAMMER_INTERNAL) |
| 1890 | show_id(newcontents, size, force); |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 1891 | #endif |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1894 | /* Read the whole chip to be able to check whether regions need to be |
| 1895 | * erased and to give better diagnostics in case write fails. |
| 1896 | * The alternative would be to read only the regions which are to be |
| 1897 | * preserved, but in that case we might perform unneeded erase which |
| 1898 | * takes time as well. |
| 1899 | */ |
Carl-Daniel Hailfinger | 84d1968 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1900 | msg_cinfo("Reading old flash chip contents... "); |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1901 | if (flash->read(flash, oldcontents, 0, size)) { |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1902 | ret = 1; |
Carl-Daniel Hailfinger | 84d1968 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1903 | msg_cinfo("FAILED.\n"); |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1904 | goto out; |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1905 | } |
Carl-Daniel Hailfinger | 84d1968 | 2011-07-26 14:28:35 +0000 | [diff] [blame] | 1906 | msg_cinfo("done.\n"); |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1907 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1908 | // This should be moved into each flash part's code to do it |
| 1909 | // cleanly. This does the job. |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1910 | handle_romentries(flash, oldcontents, newcontents); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1911 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1912 | // //////////////////////////////////////////////////////////// |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1913 | |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1914 | if (write_it) { |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 1915 | if (erase_and_write_flash(flash, oldcontents, newcontents)) { |
| 1916 | msg_cerr("Uh oh. Erase/write failed. Checking if " |
| 1917 | "anything changed.\n"); |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1918 | if (!flash->read(flash, newcontents, 0, size)) { |
| 1919 | if (!memcmp(oldcontents, newcontents, size)) { |
| 1920 | msg_cinfo("Good. It seems nothing was " |
| 1921 | "changed.\n"); |
| 1922 | nonfatal_help_message(); |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1923 | ret = 1; |
| 1924 | goto out; |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1925 | } |
| 1926 | } |
Carl-Daniel Hailfinger | f52f784 | 2010-10-08 18:52:29 +0000 | [diff] [blame] | 1927 | emergency_help_message(); |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1928 | ret = 1; |
| 1929 | goto out; |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1930 | } |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1931 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1932 | |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1933 | if (verify_it) { |
| 1934 | /* Work around chips which need some time to calm down. */ |
| 1935 | if (write_it) |
| 1936 | programmer_delay(1000*1000); |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1937 | ret = verify_flash(flash, newcontents); |
Carl-Daniel Hailfinger | f529205 | 2009-11-17 09:57:34 +0000 | [diff] [blame] | 1938 | /* If we tried to write, and verification now fails, we |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1939 | * might have an emergency situation. |
| 1940 | */ |
| 1941 | if (ret && write_it) |
| 1942 | emergency_help_message(); |
| 1943 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1944 | |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 1945 | out: |
| 1946 | free(oldcontents); |
| 1947 | free(newcontents); |
| 1948 | out_nofree: |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 1949 | programmer_shutdown(); |
Stefan Reinauer | 143da0b | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 1950 | return ret; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1951 | } |