Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1 | /* |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 2 | * This file is part of the flashrom project. |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 3 | * |
Uwe Hermann | d22a1d4 | 2007-09-09 20:21:05 +0000 | [diff] [blame] | 4 | * Copyright (C) 2000 Silicon Integrated System Corporation |
| 5 | * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com> |
Uwe Hermann | c7e8a0c | 2009-05-19 14:14:21 +0000 | [diff] [blame] | 6 | * Copyright (C) 2005-2008 coresystems GmbH |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 7 | * Copyright (C) 2008,2009 Carl-Daniel Hailfinger |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 8 | * Copyright (C) 2016 secunet Security Networks AG |
| 9 | * (Written by Nico Huber <nico.huber@secunet.com> for secunet) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 10 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 15 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 20 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * 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] | 24 | */ |
| 25 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 27 | #include <sys/types.h> |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 28 | #ifndef __LIBPAYLOAD__ |
| 29 | #include <fcntl.h> |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 30 | #include <sys/stat.h> |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 31 | #endif |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 32 | #include <string.h> |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 34 | #include <stdlib.h> |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 35 | #include <errno.h> |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 36 | #include <ctype.h> |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 37 | #include <getopt.h> |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 38 | #if HAVE_UTSNAME == 1 |
| 39 | #include <sys/utsname.h> |
| 40 | #endif |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 41 | #include "flash.h" |
Carl-Daniel Hailfinger | 0845464 | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 42 | #include "flashchips.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 43 | #include "programmer.h" |
Carl-Daniel Hailfinger | 06b9efa | 2012-08-07 11:59:59 +0000 | [diff] [blame] | 44 | #include "hwaccess.h" |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 45 | |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 46 | const char flashrom_version[] = FLASHROM_VERSION; |
Nico Huber | bcb2e5a | 2012-12-30 01:23:17 +0000 | [diff] [blame] | 47 | const char *chip_to_probe = NULL; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 48 | |
Carl-Daniel Hailfinger | 2e68160 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 49 | static enum programmer programmer = PROGRAMMER_INVALID; |
Nico Huber | bcb2e5a | 2012-12-30 01:23:17 +0000 | [diff] [blame] | 50 | static const char *programmer_param = NULL; |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +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", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 68 | .type = OTHER, |
| 69 | .devs.note = NULL, |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 70 | .init = internal_init, |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 71 | .map_flash_region = physmap, |
| 72 | .unmap_flash_region = physunmap, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 73 | .delay = internal_delay, |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 74 | }, |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 75 | #endif |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 76 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 77 | #if CONFIG_DUMMY == 1 |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 78 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 79 | .name = "dummy", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 80 | .type = OTHER, |
| 81 | /* FIXME */ |
| 82 | .devs.note = "Dummy device, does nothing and logs all accesses\n", |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 83 | .init = dummy_init, |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 84 | .map_flash_region = dummy_map, |
| 85 | .unmap_flash_region = dummy_unmap, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 86 | .delay = internal_delay, |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 87 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 88 | #endif |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 89 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 90 | #if CONFIG_NIC3COM == 1 |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 91 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 92 | .name = "nic3com", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 93 | .type = PCI, |
Stefan Tauner | 4b24a2d | 2012-12-27 18:40:36 +0000 | [diff] [blame] | 94 | .devs.dev = nics_3com, |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 95 | .init = nic3com_init, |
Uwe Hermann | c691593 | 2009-05-17 23:12:17 +0000 | [diff] [blame] | 96 | .map_flash_region = fallback_map, |
| 97 | .unmap_flash_region = fallback_unmap, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 98 | .delay = internal_delay, |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 99 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 100 | #endif |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 101 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 102 | #if CONFIG_NICREALTEK == 1 |
Joerg Fischer | 5665ef3 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 103 | { |
Carl-Daniel Hailfinger | 40446ee | 2011-03-07 01:08:09 +0000 | [diff] [blame] | 104 | /* This programmer works for Realtek RTL8139 and SMC 1211. */ |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 105 | .name = "nicrealtek", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 106 | .type = PCI, |
Stefan Tauner | 4b24a2d | 2012-12-27 18:40:36 +0000 | [diff] [blame] | 107 | .devs.dev = nics_realtek, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 108 | .init = nicrealtek_init, |
| 109 | .map_flash_region = fallback_map, |
| 110 | .unmap_flash_region = fallback_unmap, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 111 | .delay = internal_delay, |
Joerg Fischer | 5665ef3 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 112 | }, |
Joerg Fischer | 5665ef3 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 113 | #endif |
| 114 | |
Andrew Morgan | c29c2e7 | 2010-06-07 22:37:54 +0000 | [diff] [blame] | 115 | #if CONFIG_NICNATSEMI == 1 |
| 116 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 117 | .name = "nicnatsemi", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 118 | .type = PCI, |
Stefan Tauner | 4b24a2d | 2012-12-27 18:40:36 +0000 | [diff] [blame] | 119 | .devs.dev = nics_natsemi, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 120 | .init = nicnatsemi_init, |
| 121 | .map_flash_region = fallback_map, |
| 122 | .unmap_flash_region = fallback_unmap, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 123 | .delay = internal_delay, |
Andrew Morgan | c29c2e7 | 2010-06-07 22:37:54 +0000 | [diff] [blame] | 124 | }, |
| 125 | #endif |
Joerg Fischer | 5665ef3 | 2010-05-21 21:54:07 +0000 | [diff] [blame] | 126 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 127 | #if CONFIG_GFXNVIDIA == 1 |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 128 | { |
| 129 | .name = "gfxnvidia", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 130 | .type = PCI, |
Stefan Tauner | 4b24a2d | 2012-12-27 18:40:36 +0000 | [diff] [blame] | 131 | .devs.dev = gfx_nvidia, |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 132 | .init = gfxnvidia_init, |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 133 | .map_flash_region = fallback_map, |
| 134 | .unmap_flash_region = fallback_unmap, |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 135 | .delay = internal_delay, |
| 136 | }, |
| 137 | #endif |
| 138 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 139 | #if CONFIG_DRKAISER == 1 |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 140 | { |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 141 | .name = "drkaiser", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 142 | .type = PCI, |
Stefan Tauner | 4b24a2d | 2012-12-27 18:40:36 +0000 | [diff] [blame] | 143 | .devs.dev = drkaiser_pcidev, |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 144 | .init = drkaiser_init, |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 145 | .map_flash_region = fallback_map, |
| 146 | .unmap_flash_region = fallback_unmap, |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 147 | .delay = internal_delay, |
| 148 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 149 | #endif |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 150 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 151 | #if CONFIG_SATASII == 1 |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 152 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 153 | .name = "satasii", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 154 | .type = PCI, |
Stefan Tauner | 4b24a2d | 2012-12-27 18:40:36 +0000 | [diff] [blame] | 155 | .devs.dev = satas_sii, |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 156 | .init = satasii_init, |
Uwe Hermann | c691593 | 2009-05-17 23:12:17 +0000 | [diff] [blame] | 157 | .map_flash_region = fallback_map, |
| 158 | .unmap_flash_region = fallback_unmap, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 159 | .delay = internal_delay, |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 160 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 161 | #endif |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 162 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 163 | #if CONFIG_ATAHPT == 1 |
Uwe Hermann | ddd5c9e | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 164 | { |
| 165 | .name = "atahpt", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 166 | .type = PCI, |
Stefan Tauner | 4b24a2d | 2012-12-27 18:40:36 +0000 | [diff] [blame] | 167 | .devs.dev = ata_hpt, |
Uwe Hermann | ddd5c9e | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 168 | .init = atahpt_init, |
Uwe Hermann | ddd5c9e | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 169 | .map_flash_region = fallback_map, |
| 170 | .unmap_flash_region = fallback_unmap, |
Uwe Hermann | ddd5c9e | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 171 | .delay = internal_delay, |
| 172 | }, |
| 173 | #endif |
| 174 | |
Jonathan Kollasch | 7f0f3fa | 2014-06-01 10:26:23 +0000 | [diff] [blame] | 175 | #if CONFIG_ATAVIA == 1 |
| 176 | { |
| 177 | .name = "atavia", |
| 178 | .type = PCI, |
| 179 | .devs.dev = ata_via, |
| 180 | .init = atavia_init, |
| 181 | .map_flash_region = atavia_map, |
| 182 | .unmap_flash_region = fallback_unmap, |
| 183 | .delay = internal_delay, |
| 184 | }, |
| 185 | #endif |
| 186 | |
Joseph C. Lehner | c2644a3 | 2016-01-16 23:45:25 +0000 | [diff] [blame] | 187 | #if CONFIG_ATAPROMISE == 1 |
| 188 | { |
| 189 | .name = "atapromise", |
| 190 | .type = PCI, |
| 191 | .devs.dev = ata_promise, |
| 192 | .init = atapromise_init, |
| 193 | .map_flash_region = atapromise_map, |
| 194 | .unmap_flash_region = fallback_unmap, |
| 195 | .delay = internal_delay, |
| 196 | }, |
| 197 | #endif |
| 198 | |
Kyösti Mälkki | 72d42f8 | 2014-06-01 23:48:31 +0000 | [diff] [blame] | 199 | #if CONFIG_IT8212 == 1 |
| 200 | { |
| 201 | .name = "it8212", |
| 202 | .type = PCI, |
| 203 | .devs.dev = devs_it8212, |
| 204 | .init = it8212_init, |
| 205 | .map_flash_region = fallback_map, |
| 206 | .unmap_flash_region = fallback_unmap, |
| 207 | .delay = internal_delay, |
| 208 | }, |
| 209 | #endif |
| 210 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 211 | #if CONFIG_FT2232_SPI == 1 |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 212 | { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 213 | .name = "ft2232_spi", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 214 | .type = USB, |
Stefan Tauner | 4b24a2d | 2012-12-27 18:40:36 +0000 | [diff] [blame] | 215 | .devs.dev = devs_ft2232spi, |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 216 | .init = ft2232_spi_init, |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 217 | .map_flash_region = fallback_map, |
| 218 | .unmap_flash_region = fallback_unmap, |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 219 | .delay = internal_delay, |
| 220 | }, |
Carl-Daniel Hailfinger | 3426ef6 | 2009-08-19 13:27:58 +0000 | [diff] [blame] | 221 | #endif |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 222 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 223 | #if CONFIG_SERPROG == 1 |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 224 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 225 | .name = "serprog", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 226 | .type = OTHER, |
| 227 | /* FIXME */ |
| 228 | .devs.note = "All programmer devices speaking the serprog protocol\n", |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 229 | .init = serprog_init, |
Urja Rannikko | 0b4ffd5 | 2015-06-29 23:24:23 +0000 | [diff] [blame] | 230 | .map_flash_region = serprog_map, |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 231 | .unmap_flash_region = fallback_unmap, |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 232 | .delay = serprog_delay, |
| 233 | }, |
Carl-Daniel Hailfinger | 6be7411 | 2009-08-12 16:17:41 +0000 | [diff] [blame] | 234 | #endif |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 235 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 236 | #if CONFIG_BUSPIRATE_SPI == 1 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 237 | { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 238 | .name = "buspirate_spi", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 239 | .type = OTHER, |
| 240 | /* FIXME */ |
| 241 | .devs.note = "Dangerous Prototypes Bus Pirate\n", |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 242 | .init = buspirate_spi_init, |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 243 | .map_flash_region = fallback_map, |
| 244 | .unmap_flash_region = fallback_unmap, |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 245 | .delay = internal_delay, |
| 246 | }, |
| 247 | #endif |
| 248 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 249 | #if CONFIG_DEDIPROG == 1 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 250 | { |
| 251 | .name = "dediprog", |
Stefan Tauner | fdec747 | 2016-02-22 08:59:27 +0000 | [diff] [blame] | 252 | .type = USB, |
| 253 | .devs.dev = devs_dediprog, |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 254 | .init = dediprog_init, |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 255 | .map_flash_region = fallback_map, |
| 256 | .unmap_flash_region = fallback_unmap, |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 257 | .delay = internal_delay, |
| 258 | }, |
| 259 | #endif |
| 260 | |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 261 | #if CONFIG_RAYER_SPI == 1 |
| 262 | { |
| 263 | .name = "rayer_spi", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 264 | .type = OTHER, |
| 265 | /* FIXME */ |
| 266 | .devs.note = "RayeR parallel port programmer\n", |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 267 | .init = rayer_spi_init, |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 268 | .map_flash_region = fallback_map, |
| 269 | .unmap_flash_region = fallback_unmap, |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 270 | .delay = internal_delay, |
| 271 | }, |
| 272 | #endif |
| 273 | |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 274 | #if CONFIG_PONY_SPI == 1 |
| 275 | { |
| 276 | .name = "pony_spi", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 277 | .type = OTHER, |
| 278 | /* FIXME */ |
| 279 | .devs.note = "Programmers compatible with SI-Prog, serbang or AJAWe\n", |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 280 | .init = pony_spi_init, |
| 281 | .map_flash_region = fallback_map, |
| 282 | .unmap_flash_region = fallback_unmap, |
| 283 | .delay = internal_delay, |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 284 | }, |
Virgil-Adrian Teaca | da7c545 | 2012-04-30 23:11:06 +0000 | [diff] [blame] | 285 | #endif |
| 286 | |
Carl-Daniel Hailfinger | b713d2e | 2011-05-08 00:24:18 +0000 | [diff] [blame] | 287 | #if CONFIG_NICINTEL == 1 |
| 288 | { |
| 289 | .name = "nicintel", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 290 | .type = PCI, |
Stefan Tauner | 4b24a2d | 2012-12-27 18:40:36 +0000 | [diff] [blame] | 291 | .devs.dev = nics_intel, |
Carl-Daniel Hailfinger | b713d2e | 2011-05-08 00:24:18 +0000 | [diff] [blame] | 292 | .init = nicintel_init, |
Carl-Daniel Hailfinger | b713d2e | 2011-05-08 00:24:18 +0000 | [diff] [blame] | 293 | .map_flash_region = fallback_map, |
| 294 | .unmap_flash_region = fallback_unmap, |
Carl-Daniel Hailfinger | b713d2e | 2011-05-08 00:24:18 +0000 | [diff] [blame] | 295 | .delay = internal_delay, |
| 296 | }, |
| 297 | #endif |
| 298 | |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 299 | #if CONFIG_NICINTEL_SPI == 1 |
| 300 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 301 | .name = "nicintel_spi", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 302 | .type = PCI, |
Stefan Tauner | 4b24a2d | 2012-12-27 18:40:36 +0000 | [diff] [blame] | 303 | .devs.dev = nics_intel_spi, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 304 | .init = nicintel_spi_init, |
| 305 | .map_flash_region = fallback_map, |
| 306 | .unmap_flash_region = fallback_unmap, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 307 | .delay = internal_delay, |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 308 | }, |
| 309 | #endif |
| 310 | |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 311 | #if CONFIG_NICINTEL_EEPROM == 1 |
| 312 | { |
| 313 | .name = "nicintel_eeprom", |
| 314 | .type = PCI, |
| 315 | .devs.dev = nics_intel_ee, |
| 316 | .init = nicintel_ee_init, |
| 317 | .map_flash_region = fallback_map, |
| 318 | .unmap_flash_region = fallback_unmap, |
| 319 | .delay = internal_delay, |
| 320 | }, |
| 321 | #endif |
| 322 | |
Mark Marshall | 90021f2 | 2010-12-03 14:48:11 +0000 | [diff] [blame] | 323 | #if CONFIG_OGP_SPI == 1 |
| 324 | { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 325 | .name = "ogp_spi", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 326 | .type = PCI, |
Stefan Tauner | 4b24a2d | 2012-12-27 18:40:36 +0000 | [diff] [blame] | 327 | .devs.dev = ogp_spi, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 328 | .init = ogp_spi_init, |
| 329 | .map_flash_region = fallback_map, |
| 330 | .unmap_flash_region = fallback_unmap, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 331 | .delay = internal_delay, |
Mark Marshall | 90021f2 | 2010-12-03 14:48:11 +0000 | [diff] [blame] | 332 | }, |
| 333 | #endif |
| 334 | |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 335 | #if CONFIG_SATAMV == 1 |
| 336 | { |
| 337 | .name = "satamv", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 338 | .type = PCI, |
Stefan Tauner | 4b24a2d | 2012-12-27 18:40:36 +0000 | [diff] [blame] | 339 | .devs.dev = satas_mv, |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 340 | .init = satamv_init, |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 341 | .map_flash_region = fallback_map, |
| 342 | .unmap_flash_region = fallback_unmap, |
Carl-Daniel Hailfinger | 9a1105c | 2011-02-04 21:37:59 +0000 | [diff] [blame] | 343 | .delay = internal_delay, |
| 344 | }, |
| 345 | #endif |
| 346 | |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 347 | #if CONFIG_LINUX_SPI == 1 |
| 348 | { |
| 349 | .name = "linux_spi", |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 350 | .type = OTHER, |
| 351 | .devs.note = "Device files /dev/spidev*.*\n", |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 352 | .init = linux_spi_init, |
| 353 | .map_flash_region = fallback_map, |
| 354 | .unmap_flash_region = fallback_unmap, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 355 | .delay = internal_delay, |
| 356 | }, |
| 357 | #endif |
| 358 | |
James Laird | c60de0e | 2013-03-27 13:00:23 +0000 | [diff] [blame] | 359 | #if CONFIG_USBBLASTER_SPI == 1 |
| 360 | { |
| 361 | .name = "usbblaster_spi", |
| 362 | .type = USB, |
| 363 | .devs.dev = devs_usbblasterspi, |
| 364 | .init = usbblaster_spi_init, |
| 365 | .map_flash_region = fallback_map, |
| 366 | .unmap_flash_region = fallback_unmap, |
| 367 | .delay = internal_delay, |
| 368 | }, |
| 369 | #endif |
| 370 | |
Alexandre Boeglin | 80e6471 | 2014-12-20 20:25:19 +0000 | [diff] [blame] | 371 | #if CONFIG_MSTARDDC_SPI == 1 |
| 372 | { |
| 373 | .name = "mstarddc_spi", |
| 374 | .type = OTHER, |
| 375 | .devs.note = "MSTAR DDC devices addressable via /dev/i2c-* on Linux.\n", |
| 376 | .init = mstarddc_spi_init, |
| 377 | .map_flash_region = fallback_map, |
| 378 | .unmap_flash_region = fallback_unmap, |
| 379 | .delay = internal_delay, |
| 380 | }, |
| 381 | #endif |
| 382 | |
Justin Chevrier | 66e554b | 2015-02-08 21:58:10 +0000 | [diff] [blame] | 383 | #if CONFIG_PICKIT2_SPI == 1 |
| 384 | { |
| 385 | .name = "pickit2_spi", |
Stefan Tauner | f31fe84 | 2016-02-22 08:59:15 +0000 | [diff] [blame] | 386 | .type = USB, |
| 387 | .devs.dev = devs_pickit2_spi, |
Justin Chevrier | 66e554b | 2015-02-08 21:58:10 +0000 | [diff] [blame] | 388 | .init = pickit2_spi_init, |
| 389 | .map_flash_region = fallback_map, |
| 390 | .unmap_flash_region = fallback_unmap, |
| 391 | .delay = internal_delay, |
| 392 | }, |
| 393 | #endif |
| 394 | |
Urja Rannikko | 0870b02 | 2016-01-31 22:10:29 +0000 | [diff] [blame] | 395 | #if CONFIG_CH341A_SPI == 1 |
| 396 | { |
| 397 | .name = "ch341a_spi", |
| 398 | .type = USB, |
| 399 | .devs.dev = devs_ch341a_spi, |
| 400 | .init = ch341a_spi_init, |
| 401 | .map_flash_region = fallback_map, |
| 402 | .unmap_flash_region = fallback_unmap, |
| 403 | .delay = ch341a_spi_delay, |
| 404 | }, |
| 405 | #endif |
| 406 | |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 407 | {0}, /* This entry corresponds to PROGRAMMER_INVALID. */ |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 408 | }; |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 409 | |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 410 | #define SHUTDOWN_MAXFN 32 |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 411 | static int shutdown_fn_count = 0; |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 412 | /** @private */ |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 413 | struct shutdown_func_data { |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 414 | int (*func) (void *data); |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 415 | void *data; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 416 | } static shutdown_fn[SHUTDOWN_MAXFN]; |
| 417 | /* Initialize to 0 to make sure nobody registers a shutdown function before |
| 418 | * programmer init. |
| 419 | */ |
| 420 | static int may_register_shutdown = 0; |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 421 | |
Stefan Tauner | c4f44df | 2013-08-12 22:58:43 +0000 | [diff] [blame] | 422 | /* Did we change something or was every erase/write skipped (if any)? */ |
| 423 | static bool all_skipped = true; |
| 424 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 425 | static int check_block_eraser(const struct flashctx *flash, int k, int log); |
Stefan Tauner | 5368dca | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 426 | |
Stefan Tauner | 2a1ed77 | 2014-08-31 00:09:21 +0000 | [diff] [blame] | 427 | int shutdown_free(void *data) |
| 428 | { |
| 429 | free(data); |
| 430 | return 0; |
| 431 | } |
| 432 | |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 433 | /* Register a function to be executed on programmer shutdown. |
| 434 | * The advantage over atexit() is that you can supply a void pointer which will |
| 435 | * be used as parameter to the registered function upon programmer shutdown. |
| 436 | * This pointer can point to arbitrary data used by said function, e.g. undo |
| 437 | * information for GPIO settings etc. If unneeded, set data=NULL. |
| 438 | * Please note that the first (void *data) belongs to the function signature of |
| 439 | * the function passed as first parameter. |
| 440 | */ |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 441 | int register_shutdown(int (*function) (void *data), void *data) |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 442 | { |
| 443 | if (shutdown_fn_count >= SHUTDOWN_MAXFN) { |
Carl-Daniel Hailfinger | 9f5f215 | 2010-06-04 23:20:21 +0000 | [diff] [blame] | 444 | msg_perr("Tried to register more than %i shutdown functions.\n", |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 445 | SHUTDOWN_MAXFN); |
| 446 | return 1; |
| 447 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 448 | if (!may_register_shutdown) { |
| 449 | msg_perr("Tried to register a shutdown function before " |
| 450 | "programmer init.\n"); |
| 451 | return 1; |
| 452 | } |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 453 | shutdown_fn[shutdown_fn_count].func = function; |
| 454 | shutdown_fn[shutdown_fn_count].data = data; |
| 455 | shutdown_fn_count++; |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
Nico Huber | bcb2e5a | 2012-12-30 01:23:17 +0000 | [diff] [blame] | 460 | int programmer_init(enum programmer prog, const char *param) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 461 | { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 462 | int ret; |
Carl-Daniel Hailfinger | 2e68160 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 463 | |
| 464 | if (prog >= PROGRAMMER_INVALID) { |
| 465 | msg_perr("Invalid programmer specified!\n"); |
| 466 | return -1; |
| 467 | } |
| 468 | programmer = prog; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 469 | /* Initialize all programmer specific data. */ |
| 470 | /* Default to unlimited decode sizes. */ |
| 471 | max_rom_decode = (const struct decode_sizes) { |
| 472 | .parallel = 0xffffffff, |
| 473 | .lpc = 0xffffffff, |
| 474 | .fwh = 0xffffffff, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 475 | .spi = 0xffffffff, |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 476 | }; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 477 | /* Default to top aligned flash at 4 GB. */ |
| 478 | flashbase = 0; |
| 479 | /* Registering shutdown functions is now allowed. */ |
| 480 | may_register_shutdown = 1; |
Carl-Daniel Hailfinger | d1be52d | 2010-07-03 12:14:25 +0000 | [diff] [blame] | 481 | /* Default to allowing writes. Broken programmers set this to 0. */ |
| 482 | programmer_may_write = 1; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 483 | |
| 484 | programmer_param = param; |
Carl-Daniel Hailfinger | 20a36ba | 2013-08-13 07:09:57 +0000 | [diff] [blame] | 485 | msg_pdbg("Initializing %s programmer\n", programmer_table[programmer].name); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 486 | ret = programmer_table[programmer].init(); |
| 487 | if (programmer_param && strlen(programmer_param)) { |
Carl-Daniel Hailfinger | 20a36ba | 2013-08-13 07:09:57 +0000 | [diff] [blame] | 488 | if (ret != 0) { |
| 489 | /* It is quite possible that any unhandled programmer parameter would have been valid, |
| 490 | * but an error in actual programmer init happened before the parameter was evaluated. |
| 491 | */ |
| 492 | msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n", |
| 493 | programmer_param); |
| 494 | } else { |
| 495 | /* Actual programmer init was successful, but the user specified an invalid or unusable |
| 496 | * (for the current programmer configuration) parameter. |
| 497 | */ |
| 498 | msg_perr("Unhandled programmer parameters: %s\n", programmer_param); |
| 499 | msg_perr("Aborting.\n"); |
| 500 | ret = ERROR_FATAL; |
| 501 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 502 | } |
| 503 | return ret; |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Stefan Tauner | 20da4aa | 2014-05-07 22:07:23 +0000 | [diff] [blame] | 506 | /** Calls registered shutdown functions and resets internal programmer-related variables. |
| 507 | * Calling it is safe even without previous initialization, but further interactions with programmer support |
| 508 | * require a call to programmer_init() (afterwards). |
| 509 | * |
| 510 | * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */ |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 511 | int programmer_shutdown(void) |
| 512 | { |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 513 | int ret = 0; |
| 514 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 515 | /* Registering shutdown functions is no longer allowed. */ |
| 516 | may_register_shutdown = 0; |
| 517 | while (shutdown_fn_count > 0) { |
| 518 | int i = --shutdown_fn_count; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 519 | ret |= shutdown_fn[i].func(shutdown_fn[i].data); |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 520 | } |
Stefan Tauner | e34e3e8 | 2013-01-01 00:06:51 +0000 | [diff] [blame] | 521 | |
Stefan Tauner | b8911d6 | 2012-12-26 07:55:00 +0000 | [diff] [blame] | 522 | programmer_param = NULL; |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 523 | registered_master_count = 0; |
Stefan Tauner | e34e3e8 | 2013-01-01 00:06:51 +0000 | [diff] [blame] | 524 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 525 | return ret; |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 528 | void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 529 | { |
Stefan Tauner | 26e7a15 | 2013-09-13 17:21:05 +0000 | [diff] [blame] | 530 | void *ret = programmer_table[programmer].map_flash_region(descr, phys_addr, len); |
| 531 | msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n", |
| 532 | __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret); |
| 533 | return ret; |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | void programmer_unmap_flash_region(void *virt_addr, size_t len) |
| 537 | { |
| 538 | programmer_table[programmer].unmap_flash_region(virt_addr, len); |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 539 | msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 542 | void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 543 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 544 | flash->mst->par.chip_writeb(flash, val, addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 547 | void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 548 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 549 | flash->mst->par.chip_writew(flash, val, addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 552 | void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 553 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 554 | flash->mst->par.chip_writel(flash, val, addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 557 | void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len) |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 558 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 559 | flash->mst->par.chip_writen(flash, buf, addr, len); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 562 | uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 563 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 564 | return flash->mst->par.chip_readb(flash, addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 567 | uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 568 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 569 | return flash->mst->par.chip_readw(flash, addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 572 | uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 573 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 574 | return flash->mst->par.chip_readl(flash, addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 577 | void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr, |
| 578 | size_t len) |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 579 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 580 | flash->mst->par.chip_readn(flash, buf, addr, len); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 583 | void programmer_delay(unsigned int usecs) |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 584 | { |
Urja Rannikko | 8d7ec2a | 2013-10-21 21:49:08 +0000 | [diff] [blame] | 585 | if (usecs > 0) |
| 586 | programmer_table[programmer].delay(usecs); |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 589 | int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 590 | int unsigned len) |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 591 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 592 | chip_readn(flash, buf, flash->virtual_memory + start, len); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 593 | |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 594 | return 0; |
| 595 | } |
| 596 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 597 | /* 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] | 598 | * It will look for needle with a subsequent '=' in haystack, return a copy of |
| 599 | * needle and remove everything from the first occurrence of needle to the next |
| 600 | * delimiter from haystack. |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 601 | */ |
Nico Huber | bcb2e5a | 2012-12-30 01:23:17 +0000 | [diff] [blame] | 602 | char *extract_param(const char *const *haystack, const char *needle, const char *delim) |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 603 | { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 604 | char *param_pos, *opt_pos, *rest; |
| 605 | char *opt = NULL; |
| 606 | int optlen; |
Carl-Daniel Hailfinger | 2702376 | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 607 | int needlelen; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 608 | |
Carl-Daniel Hailfinger | 2702376 | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 609 | needlelen = strlen(needle); |
| 610 | if (!needlelen) { |
| 611 | msg_gerr("%s: empty needle! Please report a bug at " |
| 612 | "flashrom@flashrom.org\n", __func__); |
| 613 | return NULL; |
| 614 | } |
| 615 | /* No programmer parameters given. */ |
| 616 | if (*haystack == NULL) |
| 617 | return NULL; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 618 | param_pos = strstr(*haystack, needle); |
| 619 | do { |
| 620 | if (!param_pos) |
| 621 | return NULL; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 622 | /* Needle followed by '='? */ |
| 623 | if (param_pos[needlelen] == '=') { |
| 624 | |
| 625 | /* Beginning of the string? */ |
| 626 | if (param_pos == *haystack) |
| 627 | break; |
| 628 | /* After a delimiter? */ |
| 629 | if (strchr(delim, *(param_pos - 1))) |
| 630 | break; |
| 631 | } |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 632 | /* Continue searching. */ |
| 633 | param_pos++; |
| 634 | param_pos = strstr(param_pos, needle); |
| 635 | } while (1); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 636 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 637 | if (param_pos) { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 638 | /* Get the string after needle and '='. */ |
| 639 | opt_pos = param_pos + needlelen + 1; |
| 640 | optlen = strcspn(opt_pos, delim); |
| 641 | /* Return an empty string if the parameter was empty. */ |
| 642 | opt = malloc(optlen + 1); |
| 643 | if (!opt) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 644 | msg_gerr("Out of memory!\n"); |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 645 | exit(1); |
| 646 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 647 | strncpy(opt, opt_pos, optlen); |
| 648 | opt[optlen] = '\0'; |
| 649 | rest = opt_pos + optlen; |
| 650 | /* Skip all delimiters after the current parameter. */ |
| 651 | rest += strspn(rest, delim); |
| 652 | memmove(param_pos, rest, strlen(rest) + 1); |
| 653 | /* We could shrink haystack, but the effort is not worth it. */ |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 654 | } |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 655 | |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 656 | return opt; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Stefan Tauner | 6665244 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 659 | char *extract_programmer_param(const char *param_name) |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 660 | { |
| 661 | return extract_param(&programmer_param, param_name, ","); |
| 662 | } |
| 663 | |
Sylvain "ythier" Hitier | 9db4551 | 2011-07-04 07:27:17 +0000 | [diff] [blame] | 664 | /* Returns the number of well-defined erasers for a chip. */ |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 665 | static unsigned int count_usable_erasers(const struct flashctx *flash) |
Stefan Tauner | 5368dca | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 666 | { |
| 667 | unsigned int usable_erasefunctions = 0; |
| 668 | int k; |
| 669 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
| 670 | if (!check_block_eraser(flash, k, 0)) |
| 671 | usable_erasefunctions++; |
| 672 | } |
| 673 | return usable_erasefunctions; |
| 674 | } |
| 675 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 676 | static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len) |
Stefan Tauner | 78ffbea | 2012-10-27 15:36:56 +0000 | [diff] [blame] | 677 | { |
| 678 | int ret = 0, failcount = 0; |
| 679 | unsigned int i; |
| 680 | for (i = 0; i < len; i++) { |
| 681 | if (wantbuf[i] != havebuf[i]) { |
| 682 | /* Only print the first failure. */ |
| 683 | if (!failcount++) |
| 684 | msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,", |
| 685 | start + i, wantbuf[i], havebuf[i]); |
| 686 | } |
| 687 | } |
| 688 | if (failcount) { |
| 689 | msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n", |
| 690 | start, start + len - 1, failcount); |
| 691 | ret = -1; |
| 692 | } |
| 693 | return ret; |
| 694 | } |
| 695 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 696 | /* start is an offset to the base address of the flash chip */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 697 | int check_erased_range(struct flashctx *flash, unsigned int start, |
| 698 | unsigned int len) |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 699 | { |
| 700 | int ret; |
| 701 | uint8_t *cmpbuf = malloc(len); |
| 702 | |
| 703 | if (!cmpbuf) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 704 | msg_gerr("Could not allocate memory!\n"); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 705 | exit(1); |
| 706 | } |
| 707 | memset(cmpbuf, 0xff, len); |
Stefan Tauner | 78ffbea | 2012-10-27 15:36:56 +0000 | [diff] [blame] | 708 | ret = verify_range(flash, cmpbuf, start, len); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 709 | free(cmpbuf); |
| 710 | return ret; |
| 711 | } |
| 712 | |
Uwe Hermann | 48ec1b1 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 713 | /* |
Carl-Daniel Hailfinger | d0250a3 | 2009-11-25 17:05:52 +0000 | [diff] [blame] | 714 | * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 715 | * flash content at location start |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 716 | * @start offset to the base address of the flash chip |
| 717 | * @len length of the verified area |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 718 | * @return 0 for success, -1 for failure |
| 719 | */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 720 | int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 721 | { |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 722 | if (!len) |
Stefan Tauner | df64a42 | 2014-05-27 00:06:14 +0000 | [diff] [blame] | 723 | return -1; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 724 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 725 | if (!flash->chip->read) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 726 | msg_cerr("ERROR: flashrom has no read function for this flash chip.\n"); |
Stefan Tauner | df64a42 | 2014-05-27 00:06:14 +0000 | [diff] [blame] | 727 | return -1; |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 728 | } |
Stefan Tauner | df64a42 | 2014-05-27 00:06:14 +0000 | [diff] [blame] | 729 | |
| 730 | uint8_t *readbuf = malloc(len); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 731 | if (!readbuf) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 732 | msg_gerr("Could not allocate memory!\n"); |
Stefan Tauner | df64a42 | 2014-05-27 00:06:14 +0000 | [diff] [blame] | 733 | return -1; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 734 | } |
Stefan Tauner | df64a42 | 2014-05-27 00:06:14 +0000 | [diff] [blame] | 735 | int ret = 0; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 736 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 737 | if (start + len > flash->chip->total_size * 1024) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 738 | 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] | 739 | " total_size 0x%x\n", __func__, start, len, |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 740 | flash->chip->total_size * 1024); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 741 | ret = -1; |
| 742 | goto out_free; |
| 743 | } |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 744 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 745 | ret = flash->chip->read(flash, readbuf, start, len); |
Carl-Daniel Hailfinger | d836941 | 2010-11-16 17:21:58 +0000 | [diff] [blame] | 746 | if (ret) { |
| 747 | msg_gerr("Verification impossible because read failed " |
| 748 | "at 0x%x (len 0x%x)\n", start, len); |
Stefan Tauner | df64a42 | 2014-05-27 00:06:14 +0000 | [diff] [blame] | 749 | ret = -1; |
| 750 | goto out_free; |
Carl-Daniel Hailfinger | d836941 | 2010-11-16 17:21:58 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Stefan Tauner | 78ffbea | 2012-10-27 15:36:56 +0000 | [diff] [blame] | 753 | ret = compare_range(cmpbuf, readbuf, start, len); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 754 | out_free: |
| 755 | free(readbuf); |
| 756 | return ret; |
| 757 | } |
| 758 | |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 759 | /* Helper function for need_erase() that focuses on granularities of gran bytes. */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 760 | static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len, unsigned int gran) |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 761 | { |
| 762 | unsigned int i, j, limit; |
| 763 | for (j = 0; j < len / gran; j++) { |
| 764 | limit = min (gran, len - j * gran); |
| 765 | /* Are 'have' and 'want' identical? */ |
| 766 | if (!memcmp(have + j * gran, want + j * gran, limit)) |
| 767 | continue; |
| 768 | /* have needs to be in erased state. */ |
| 769 | for (i = 0; i < limit; i++) |
| 770 | if (have[j * gran + i] != 0xff) |
| 771 | return 1; |
| 772 | } |
| 773 | return 0; |
| 774 | } |
| 775 | |
Uwe Hermann | 48ec1b1 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 776 | /* |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 777 | * Check if the buffer @have can be programmed to the content of @want without |
| 778 | * erasing. This is only possible if all chunks of size @gran are either kept |
| 779 | * 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] | 780 | * |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 781 | * Warning: This function assumes that @have and @want point to naturally |
| 782 | * aligned regions. |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 783 | * |
| 784 | * @have buffer with current content |
| 785 | * @want buffer with desired content |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 786 | * @len length of the checked area |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 787 | * @gran write granularity (enum, not count) |
| 788 | * @return 0 if no erase is needed, 1 otherwise |
| 789 | */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 790 | int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len, enum write_granularity gran) |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 791 | { |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 792 | int result = 0; |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 793 | unsigned int i; |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 794 | |
| 795 | switch (gran) { |
| 796 | case write_gran_1bit: |
| 797 | for (i = 0; i < len; i++) |
| 798 | if ((have[i] & want[i]) != want[i]) { |
| 799 | result = 1; |
| 800 | break; |
| 801 | } |
| 802 | break; |
| 803 | case write_gran_1byte: |
| 804 | for (i = 0; i < len; i++) |
| 805 | if ((have[i] != want[i]) && (have[i] != 0xff)) { |
| 806 | result = 1; |
| 807 | break; |
| 808 | } |
| 809 | break; |
Paul Kocialkowski | c8305e1 | 2015-10-16 02:16:20 +0000 | [diff] [blame] | 810 | case write_gran_128bytes: |
| 811 | result = need_erase_gran_bytes(have, want, len, 128); |
| 812 | break; |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 813 | case write_gran_256bytes: |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 814 | result = need_erase_gran_bytes(have, want, len, 256); |
| 815 | break; |
| 816 | case write_gran_264bytes: |
| 817 | result = need_erase_gran_bytes(have, want, len, 264); |
| 818 | break; |
| 819 | case write_gran_512bytes: |
| 820 | result = need_erase_gran_bytes(have, want, len, 512); |
| 821 | break; |
| 822 | case write_gran_528bytes: |
| 823 | result = need_erase_gran_bytes(have, want, len, 528); |
| 824 | break; |
| 825 | case write_gran_1024bytes: |
| 826 | result = need_erase_gran_bytes(have, want, len, 1024); |
| 827 | break; |
| 828 | case write_gran_1056bytes: |
| 829 | result = need_erase_gran_bytes(have, want, len, 1056); |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 830 | break; |
Carl-Daniel Hailfinger | 1b0e9fc | 2014-06-16 22:36:17 +0000 | [diff] [blame] | 831 | case write_gran_1byte_implicit_erase: |
| 832 | /* Do not erase, handle content changes from anything->0xff by writing 0xff. */ |
| 833 | result = 0; |
| 834 | break; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 835 | default: |
| 836 | msg_cerr("%s: Unsupported granularity! Please report a bug at " |
| 837 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 838 | } |
| 839 | return result; |
| 840 | } |
| 841 | |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 842 | /** |
| 843 | * Check if the buffer @have needs to be programmed to get the content of @want. |
| 844 | * If yes, return 1 and fill in first_start with the start address of the |
| 845 | * write operation and first_len with the length of the first to-be-written |
| 846 | * chunk. If not, return 0 and leave first_start and first_len undefined. |
| 847 | * |
| 848 | * Warning: This function assumes that @have and @want point to naturally |
| 849 | * aligned regions. |
| 850 | * |
| 851 | * @have buffer with current content |
| 852 | * @want buffer with desired content |
| 853 | * @len length of the checked area |
| 854 | * @gran write granularity (enum, not count) |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 855 | * @first_start offset of the first byte which needs to be written (passed in |
| 856 | * value is increased by the offset of the first needed write |
| 857 | * relative to have/want or unchanged if no write is needed) |
| 858 | * @return length of the first contiguous area which needs to be written |
| 859 | * 0 if no write is needed |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 860 | * |
| 861 | * FIXME: This function needs a parameter which tells it about coalescing |
| 862 | * in relation to the max write length of the programmer and the max write |
| 863 | * length of the chip. |
| 864 | */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 865 | static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 866 | unsigned int *first_start, |
| 867 | enum write_granularity gran) |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 868 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 869 | int need_write = 0; |
| 870 | unsigned int rel_start = 0, first_len = 0; |
| 871 | unsigned int i, limit, stride; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 872 | |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 873 | switch (gran) { |
| 874 | case write_gran_1bit: |
| 875 | case write_gran_1byte: |
Carl-Daniel Hailfinger | 1b0e9fc | 2014-06-16 22:36:17 +0000 | [diff] [blame] | 876 | case write_gran_1byte_implicit_erase: |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 877 | stride = 1; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 878 | break; |
Paul Kocialkowski | c8305e1 | 2015-10-16 02:16:20 +0000 | [diff] [blame] | 879 | case write_gran_128bytes: |
| 880 | stride = 128; |
| 881 | break; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 882 | case write_gran_256bytes: |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 883 | stride = 256; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 884 | break; |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 885 | case write_gran_264bytes: |
| 886 | stride = 264; |
| 887 | break; |
| 888 | case write_gran_512bytes: |
| 889 | stride = 512; |
| 890 | break; |
| 891 | case write_gran_528bytes: |
| 892 | stride = 528; |
| 893 | break; |
| 894 | case write_gran_1024bytes: |
| 895 | stride = 1024; |
| 896 | break; |
| 897 | case write_gran_1056bytes: |
| 898 | stride = 1056; |
| 899 | break; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 900 | default: |
| 901 | msg_cerr("%s: Unsupported granularity! Please report a bug at " |
| 902 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 903 | /* Claim that no write was needed. A write with unknown |
| 904 | * granularity is too dangerous to try. |
| 905 | */ |
| 906 | return 0; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 907 | } |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 908 | for (i = 0; i < len / stride; i++) { |
| 909 | limit = min(stride, len - i * stride); |
| 910 | /* Are 'have' and 'want' identical? */ |
| 911 | if (memcmp(have + i * stride, want + i * stride, limit)) { |
| 912 | if (!need_write) { |
| 913 | /* First location where have and want differ. */ |
| 914 | need_write = 1; |
| 915 | rel_start = i * stride; |
| 916 | } |
| 917 | } else { |
| 918 | if (need_write) { |
| 919 | /* First location where have and want |
| 920 | * do not differ anymore. |
| 921 | */ |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 922 | break; |
| 923 | } |
| 924 | } |
| 925 | } |
Carl-Daniel Hailfinger | 202bf53 | 2010-12-06 13:05:44 +0000 | [diff] [blame] | 926 | if (need_write) |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 927 | first_len = min(i * stride - rel_start, len); |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 928 | *first_start += rel_start; |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 929 | return first_len; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Carl-Daniel Hailfinger | eaac68b | 2009-11-23 12:55:31 +0000 | [diff] [blame] | 932 | /* This function generates various test patterns useful for testing controller |
| 933 | * and chip communication as well as chip behaviour. |
| 934 | * |
| 935 | * If a byte can be written multiple times, each time keeping 0-bits at 0 |
| 936 | * and changing 1-bits to 0 if the new value for that bit is 0, the effect |
| 937 | * is essentially an AND operation. That's also the reason why this function |
| 938 | * provides the result of AND between various patterns. |
| 939 | * |
| 940 | * Below is a list of patterns (and their block length). |
| 941 | * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes) |
| 942 | * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes) |
| 943 | * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes) |
| 944 | * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes) |
| 945 | * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes) |
| 946 | * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes) |
| 947 | * Pattern 6 is 00 (1 Byte) |
| 948 | * Pattern 7 is ff (1 Byte) |
| 949 | * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256 |
| 950 | * byte block. |
| 951 | * |
| 952 | * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B) |
| 953 | * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B) |
| 954 | * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter) |
| 955 | * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards) |
| 956 | * Pattern 12 is 00 (1 Byte) |
| 957 | * Pattern 13 is ff (1 Byte) |
| 958 | * Patterns 8-13 have no block number. |
| 959 | * |
| 960 | * Patterns 0-3 are created to detect and efficiently diagnose communication |
| 961 | * slips like missed bits or bytes and their repetitive nature gives good visual |
| 962 | * cues to the person inspecting the results. In addition, the following holds: |
| 963 | * AND Pattern 0/1 == Pattern 4 |
| 964 | * AND Pattern 2/3 == Pattern 5 |
| 965 | * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6 |
| 966 | * A weakness of pattern 0-5 is the inability to detect swaps/copies between |
| 967 | * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc. |
| 968 | * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes. |
| 969 | * 0x5 and 0xa were picked because they are 0101 and 1010 binary. |
| 970 | * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes. |
| 971 | * Besides that, they provide for bit testing of the last two bytes of every |
| 972 | * 256 byte block which contains the block number for patterns 0-6. |
| 973 | * Patterns 10-11 are special purpose for detecting subblock aliasing with |
| 974 | * block sizes >256 bytes (some Dataflash chips etc.) |
| 975 | * AND Pattern 8/9 == Pattern 12 |
| 976 | * AND Pattern 10/11 == Pattern 12 |
| 977 | * Pattern 13 is the completely erased state. |
| 978 | * None of the patterns can detect aliasing at boundaries which are a multiple |
| 979 | * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI). |
| 980 | */ |
| 981 | int generate_testpattern(uint8_t *buf, uint32_t size, int variant) |
| 982 | { |
| 983 | int i; |
| 984 | |
| 985 | if (!buf) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 986 | msg_gerr("Invalid buffer!\n"); |
Carl-Daniel Hailfinger | eaac68b | 2009-11-23 12:55:31 +0000 | [diff] [blame] | 987 | return 1; |
| 988 | } |
| 989 | |
| 990 | switch (variant) { |
| 991 | case 0: |
| 992 | for (i = 0; i < size; i++) |
| 993 | buf[i] = (i & 0xf) << 4 | 0x5; |
| 994 | break; |
| 995 | case 1: |
| 996 | for (i = 0; i < size; i++) |
| 997 | buf[i] = (i & 0xf) << 4 | 0xa; |
| 998 | break; |
| 999 | case 2: |
| 1000 | for (i = 0; i < size; i++) |
| 1001 | buf[i] = 0x50 | (i & 0xf); |
| 1002 | break; |
| 1003 | case 3: |
| 1004 | for (i = 0; i < size; i++) |
| 1005 | buf[i] = 0xa0 | (i & 0xf); |
| 1006 | break; |
| 1007 | case 4: |
| 1008 | for (i = 0; i < size; i++) |
| 1009 | buf[i] = (i & 0xf) << 4; |
| 1010 | break; |
| 1011 | case 5: |
| 1012 | for (i = 0; i < size; i++) |
| 1013 | buf[i] = i & 0xf; |
| 1014 | break; |
| 1015 | case 6: |
| 1016 | memset(buf, 0x00, size); |
| 1017 | break; |
| 1018 | case 7: |
| 1019 | memset(buf, 0xff, size); |
| 1020 | break; |
| 1021 | case 8: |
| 1022 | for (i = 0; i < size; i++) |
| 1023 | buf[i] = i & 0xff; |
| 1024 | break; |
| 1025 | case 9: |
| 1026 | for (i = 0; i < size; i++) |
| 1027 | buf[i] = ~(i & 0xff); |
| 1028 | break; |
| 1029 | case 10: |
| 1030 | for (i = 0; i < size % 2; i++) { |
| 1031 | buf[i * 2] = (i >> 8) & 0xff; |
| 1032 | buf[i * 2 + 1] = i & 0xff; |
| 1033 | } |
| 1034 | if (size & 0x1) |
| 1035 | buf[i * 2] = (i >> 8) & 0xff; |
| 1036 | break; |
| 1037 | case 11: |
| 1038 | for (i = 0; i < size % 2; i++) { |
| 1039 | buf[i * 2] = ~((i >> 8) & 0xff); |
| 1040 | buf[i * 2 + 1] = ~(i & 0xff); |
| 1041 | } |
| 1042 | if (size & 0x1) |
| 1043 | buf[i * 2] = ~((i >> 8) & 0xff); |
| 1044 | break; |
| 1045 | case 12: |
| 1046 | memset(buf, 0x00, size); |
| 1047 | break; |
| 1048 | case 13: |
| 1049 | memset(buf, 0xff, size); |
| 1050 | break; |
| 1051 | } |
| 1052 | |
| 1053 | if ((variant >= 0) && (variant <= 7)) { |
| 1054 | /* Write block number in the last two bytes of each 256-byte |
| 1055 | * block, big endian for easier reading of the hexdump. |
| 1056 | * Note that this wraps around for chips larger than 2^24 bytes |
| 1057 | * (16 MB). |
| 1058 | */ |
| 1059 | for (i = 0; i < size / 256; i++) { |
| 1060 | buf[i * 256 + 254] = (i >> 8) & 0xff; |
| 1061 | buf[i * 256 + 255] = i & 0xff; |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
Stefan Tauner | 9e3a698 | 2014-08-15 17:17:59 +0000 | [diff] [blame] | 1068 | /* Returns the number of busses commonly supported by the current programmer and flash chip where the latter |
| 1069 | * can not be completely accessed due to size/address limits of the programmer. */ |
| 1070 | unsigned int count_max_decode_exceedings(const struct flashctx *flash) |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1071 | { |
Stefan Tauner | 9e3a698 | 2014-08-15 17:17:59 +0000 | [diff] [blame] | 1072 | unsigned int limitexceeded = 0; |
| 1073 | uint32_t size = flash->chip->total_size * 1024; |
| 1074 | enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype; |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1075 | |
| 1076 | if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) { |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1077 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1078 | msg_pdbg("Chip size %u kB is bigger than supported " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1079 | "size %u kB of chipset/board/programmer " |
| 1080 | "for %s interface, " |
| 1081 | "probe/read/erase/write may fail. ", size / 1024, |
| 1082 | max_rom_decode.parallel / 1024, "Parallel"); |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1083 | } |
Carl-Daniel Hailfinger | 1a22795 | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 1084 | if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) { |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1085 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1086 | msg_pdbg("Chip size %u kB is bigger than supported " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1087 | "size %u kB of chipset/board/programmer " |
| 1088 | "for %s interface, " |
| 1089 | "probe/read/erase/write may fail. ", size / 1024, |
| 1090 | max_rom_decode.lpc / 1024, "LPC"); |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1091 | } |
Carl-Daniel Hailfinger | 1a22795 | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 1092 | if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) { |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1093 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1094 | msg_pdbg("Chip size %u kB is bigger than supported " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1095 | "size %u kB of chipset/board/programmer " |
| 1096 | "for %s interface, " |
| 1097 | "probe/read/erase/write may fail. ", size / 1024, |
| 1098 | max_rom_decode.fwh / 1024, "FWH"); |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1099 | } |
Carl-Daniel Hailfinger | 1a22795 | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 1100 | if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) { |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1101 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1102 | msg_pdbg("Chip size %u kB is bigger than supported " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1103 | "size %u kB of chipset/board/programmer " |
| 1104 | "for %s interface, " |
| 1105 | "probe/read/erase/write may fail. ", size / 1024, |
| 1106 | max_rom_decode.spi / 1024, "SPI"); |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1107 | } |
Stefan Tauner | 9e3a698 | 2014-08-15 17:17:59 +0000 | [diff] [blame] | 1108 | return limitexceeded; |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 1111 | void unmap_flash(struct flashctx *flash) |
| 1112 | { |
| 1113 | if (flash->virtual_registers != (chipaddr)ERROR_PTR) { |
| 1114 | programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024); |
| 1115 | flash->physical_registers = 0; |
| 1116 | flash->virtual_registers = (chipaddr)ERROR_PTR; |
| 1117 | } |
| 1118 | |
| 1119 | if (flash->virtual_memory != (chipaddr)ERROR_PTR) { |
| 1120 | programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024); |
| 1121 | flash->physical_memory = 0; |
| 1122 | flash->virtual_memory = (chipaddr)ERROR_PTR; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | int map_flash(struct flashctx *flash) |
| 1127 | { |
| 1128 | /* Init pointers to the fail-safe state to distinguish them later from legit values. */ |
| 1129 | flash->virtual_memory = (chipaddr)ERROR_PTR; |
| 1130 | flash->virtual_registers = (chipaddr)ERROR_PTR; |
| 1131 | |
| 1132 | /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0. |
| 1133 | * These are used for various probing-related hacks that would not map successfully anyway and should be |
| 1134 | * removed ASAP. */ |
| 1135 | if (flash->chip->total_size == 0) |
| 1136 | return 0; |
| 1137 | |
| 1138 | const chipsize_t size = flash->chip->total_size * 1024; |
| 1139 | uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1); |
| 1140 | void *addr = programmer_map_flash_region(flash->chip->name, base, size); |
| 1141 | if (addr == ERROR_PTR) { |
| 1142 | msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n", |
| 1143 | flash->chip->name, PRIxPTR_WIDTH, base); |
| 1144 | return 1; |
| 1145 | } |
| 1146 | flash->physical_memory = base; |
| 1147 | flash->virtual_memory = (chipaddr)addr; |
| 1148 | |
| 1149 | /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere |
| 1150 | * completely different on some chips and programmers, or not mappable at all. |
| 1151 | * Ignore these problems for now and always report success. */ |
| 1152 | if (flash->chip->feature_bits & FEATURE_REGISTERMAP) { |
| 1153 | base = 0xffffffff - size - 0x400000 + 1; |
| 1154 | addr = programmer_map_flash_region("flash chip registers", base, size); |
| 1155 | if (addr == ERROR_PTR) { |
| 1156 | msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n", |
| 1157 | flash->chip->name, PRIxPTR_WIDTH, base); |
| 1158 | return 0; |
| 1159 | } |
| 1160 | flash->physical_registers = base; |
| 1161 | flash->virtual_registers = (chipaddr)addr; |
| 1162 | } |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
Nico Huber | 2d62572 | 2016-05-03 10:48:02 +0200 | [diff] [blame] | 1166 | /* |
| 1167 | * Return a string corresponding to the bustype parameter. |
| 1168 | * Memory is obtained with malloc() and must be freed with free() by the caller. |
| 1169 | */ |
| 1170 | char *flashbuses_to_text(enum chipbustype bustype) |
| 1171 | { |
| 1172 | char *ret = calloc(1, 1); |
| 1173 | /* |
| 1174 | * FIXME: Once all chipsets and flash chips have been updated, NONSPI |
| 1175 | * will cease to exist and should be eliminated here as well. |
| 1176 | */ |
| 1177 | if (bustype == BUS_NONSPI) { |
| 1178 | ret = strcat_realloc(ret, "Non-SPI, "); |
| 1179 | } else { |
| 1180 | if (bustype & BUS_PARALLEL) |
| 1181 | ret = strcat_realloc(ret, "Parallel, "); |
| 1182 | if (bustype & BUS_LPC) |
| 1183 | ret = strcat_realloc(ret, "LPC, "); |
| 1184 | if (bustype & BUS_FWH) |
| 1185 | ret = strcat_realloc(ret, "FWH, "); |
| 1186 | if (bustype & BUS_SPI) |
| 1187 | ret = strcat_realloc(ret, "SPI, "); |
| 1188 | if (bustype & BUS_PROG) |
| 1189 | ret = strcat_realloc(ret, "Programmer-specific, "); |
| 1190 | if (bustype == BUS_NONE) |
| 1191 | ret = strcat_realloc(ret, "None, "); |
| 1192 | } |
| 1193 | /* Kill last comma. */ |
| 1194 | ret[strlen(ret) - 2] = '\0'; |
| 1195 | ret = realloc(ret, strlen(ret) + 1); |
| 1196 | return ret; |
| 1197 | } |
| 1198 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 1199 | int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1200 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1201 | const struct flashchip *chip; |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 1202 | enum chipbustype buses_common; |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1203 | char *tmp; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1204 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1205 | for (chip = flashchips + startchip; chip && chip->name; chip++) { |
| 1206 | if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1207 | continue; |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 1208 | buses_common = mst->buses_supported & chip->bustype; |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 1209 | if (!buses_common) |
Carl-Daniel Hailfinger | 6573b74 | 2011-06-17 22:38:53 +0000 | [diff] [blame] | 1210 | continue; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1211 | msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size); |
| 1212 | if (!chip->probe && !force) { |
| 1213 | msg_gdbg("failed! flashrom has no probe function for this flash chip.\n"); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 1214 | continue; |
| 1215 | } |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 1216 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1217 | /* Start filling in the dynamic data. */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1218 | flash->chip = calloc(1, sizeof(struct flashchip)); |
| 1219 | if (!flash->chip) { |
| 1220 | msg_gerr("Out of memory!\n"); |
| 1221 | exit(1); |
| 1222 | } |
| 1223 | memcpy(flash->chip, chip, sizeof(struct flashchip)); |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 1224 | flash->mst = mst; |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1225 | |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 1226 | if (map_flash(flash) != 0) |
| 1227 | return -1; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1228 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1229 | /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash() |
| 1230 | * is only called with force=1 after normal probing failed. |
| 1231 | */ |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1232 | if (force) |
| 1233 | break; |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 1234 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1235 | if (flash->chip->probe(flash) != 1) |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 1236 | goto notfound; |
| 1237 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1238 | /* If this is the first chip found, accept it. |
| 1239 | * If this is not the first chip found, accept it only if it is |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 1240 | * a non-generic match. SFDP and CFI are generic matches. |
| 1241 | * startchip==0 means this call to probe_flash() is the first |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 1242 | * one for this programmer interface (master) and thus no other chip has |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 1243 | * been found on this interface. |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1244 | */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1245 | if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) { |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 1246 | msg_cinfo("===\n" |
| 1247 | "SFDP has autodetected a flash chip which is " |
| 1248 | "not natively supported by flashrom yet.\n"); |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1249 | if (count_usable_erasers(flash) == 0) |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 1250 | msg_cinfo("The standard operations read and " |
| 1251 | "verify should work, but to support " |
| 1252 | "erase, write and all other " |
| 1253 | "possible features"); |
| 1254 | else |
| 1255 | msg_cinfo("All standard operations (read, " |
| 1256 | "verify, erase and write) should " |
| 1257 | "work, but to support all possible " |
| 1258 | "features"); |
| 1259 | |
Stefan Tauner | b4e06bd | 2012-08-20 00:24:22 +0000 | [diff] [blame] | 1260 | msg_cinfo(" we need to add them manually.\n" |
| 1261 | "You can help us by mailing us the output of the following command to " |
| 1262 | "flashrom@flashrom.org:\n" |
| 1263 | "'flashrom -VV [plus the -p/--programmer parameter]'\n" |
| 1264 | "Thanks for your help!\n" |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 1265 | "===\n"); |
| 1266 | } |
| 1267 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1268 | /* First flash chip detected on this bus. */ |
| 1269 | if (startchip == 0) |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1270 | break; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1271 | /* Not the first flash chip detected on this bus, but not a generic match either. */ |
| 1272 | if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID)) |
| 1273 | break; |
| 1274 | /* Not the first flash chip detected on this bus, and it's just a generic match. Ignore it. */ |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 1275 | notfound: |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 1276 | unmap_flash(flash); |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1277 | free(flash->chip); |
| 1278 | flash->chip = NULL; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1279 | } |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 1280 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1281 | if (!flash->chip) |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1282 | return -1; |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 1283 | |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1284 | /* Fill fallback layout covering the whole chip. */ |
| 1285 | struct single_layout *const fallback = &flash->fallback_layout; |
| 1286 | fallback->base.entries = &fallback->entry; |
| 1287 | fallback->base.num_entries = 1; |
| 1288 | fallback->entry.start = 0; |
| 1289 | fallback->entry.end = flash->chip->total_size * 1024 - 1; |
| 1290 | fallback->entry.included = true; |
| 1291 | strcpy(fallback->entry.name, "complete flash"); |
Stefan Reinauer | 051e236 | 2011-01-19 06:21:54 +0000 | [diff] [blame] | 1292 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1293 | tmp = flashbuses_to_text(flash->chip->bustype); |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 1294 | msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found", |
| 1295 | flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp); |
Stefan Tauner | 0015549 | 2011-06-26 20:45:35 +0000 | [diff] [blame] | 1296 | free(tmp); |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 1297 | #if CONFIG_INTERNAL == 1 |
| 1298 | if (programmer_table[programmer].map_flash_region == physmap) |
| 1299 | msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n", |
| 1300 | PRIxPTR_WIDTH, flash->physical_memory); |
| 1301 | else |
| 1302 | #endif |
| 1303 | msg_cinfo("on %s.\n", programmer_table[programmer].name); |
Uwe Hermann | 9899cad | 2009-06-28 21:47:57 +0000 | [diff] [blame] | 1304 | |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 1305 | /* Flash registers may more likely not be mapped if the chip was forced. |
| 1306 | * Lock info may be stored in registers, so avoid lock info printing. */ |
Carl-Daniel Hailfinger | 859f3f0 | 2010-12-02 21:59:42 +0000 | [diff] [blame] | 1307 | if (!force) |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1308 | if (flash->chip->printlock) |
| 1309 | flash->chip->printlock(flash); |
Sean Nelson | 6e0b912 | 2010-02-19 00:52:10 +0000 | [diff] [blame] | 1310 | |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 1311 | /* Get out of the way for later runs. */ |
| 1312 | unmap_flash(flash); |
| 1313 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 1314 | /* Return position of matching chip. */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1315 | return chip - flashchips; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1318 | int read_buf_from_file(unsigned char *buf, unsigned long size, |
| 1319 | const char *filename) |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1320 | { |
Nico Huber | 7562f7d | 2013-08-30 21:29:45 +0000 | [diff] [blame] | 1321 | #ifdef __LIBPAYLOAD__ |
| 1322 | msg_gerr("Error: No file I/O support in libpayload\n"); |
| 1323 | return 1; |
| 1324 | #else |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 1325 | int ret = 0; |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1326 | |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 1327 | FILE *image; |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1328 | if ((image = fopen(filename, "rb")) == NULL) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 1329 | msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno)); |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1330 | return 1; |
| 1331 | } |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 1332 | |
| 1333 | struct stat image_stat; |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1334 | if (fstat(fileno(image), &image_stat) != 0) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 1335 | msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno)); |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 1336 | ret = 1; |
| 1337 | goto out; |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1338 | } |
| 1339 | if (image_stat.st_size != size) { |
Carl-Daniel Hailfinger | 11990da | 2013-07-13 23:21:05 +0000 | [diff] [blame] | 1340 | msg_gerr("Error: Image size (%jd B) doesn't match the flash chip's size (%lu B)!\n", |
Stefan Tauner | e038e90 | 2013-02-04 04:38:42 +0000 | [diff] [blame] | 1341 | (intmax_t)image_stat.st_size, size); |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 1342 | ret = 1; |
| 1343 | goto out; |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1344 | } |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 1345 | |
| 1346 | unsigned long numbytes = fread(buf, 1, size, image); |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1347 | if (numbytes != size) { |
| 1348 | msg_gerr("Error: Failed to read complete file. Got %ld bytes, " |
| 1349 | "wanted %ld!\n", numbytes, size); |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 1350 | ret = 1; |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1351 | } |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 1352 | out: |
| 1353 | (void)fclose(image); |
| 1354 | return ret; |
Nico Huber | 7562f7d | 2013-08-30 21:29:45 +0000 | [diff] [blame] | 1355 | #endif |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 1358 | int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename) |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1359 | { |
Nico Huber | 7562f7d | 2013-08-30 21:29:45 +0000 | [diff] [blame] | 1360 | #ifdef __LIBPAYLOAD__ |
| 1361 | msg_gerr("Error: No file I/O support in libpayload\n"); |
| 1362 | return 1; |
| 1363 | #else |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1364 | FILE *image; |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 1365 | int ret = 0; |
Stephan Guilloux | 21dd55b | 2009-06-01 22:07:52 +0000 | [diff] [blame] | 1366 | |
| 1367 | if (!filename) { |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1368 | msg_gerr("No filename specified.\n"); |
Stephan Guilloux | 21dd55b | 2009-06-01 22:07:52 +0000 | [diff] [blame] | 1369 | return 1; |
| 1370 | } |
Patrick Georgi | 0bf842d | 2010-01-25 22:55:33 +0000 | [diff] [blame] | 1371 | if ((image = fopen(filename, "wb")) == NULL) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 1372 | msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno)); |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 1373 | return 1; |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1374 | } |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1375 | |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 1376 | unsigned long numbytes = fwrite(buf, 1, size, image); |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1377 | if (numbytes != size) { |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 1378 | msg_gerr("Error: file %s could not be written completely.\n", filename); |
| 1379 | ret = 1; |
| 1380 | goto out; |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1381 | } |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 1382 | if (fflush(image)) { |
| 1383 | msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno)); |
| 1384 | ret = 1; |
| 1385 | } |
| 1386 | // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW). |
| 1387 | #if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1) |
| 1388 | struct stat image_stat; |
| 1389 | if (fstat(fileno(image), &image_stat) != 0) { |
| 1390 | msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno)); |
| 1391 | ret = 1; |
| 1392 | goto out; |
| 1393 | } |
| 1394 | if (S_ISREG(image_stat.st_mode)) { |
| 1395 | if (fsync(fileno(image))) { |
| 1396 | msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno)); |
| 1397 | ret = 1; |
| 1398 | } |
| 1399 | } |
| 1400 | #endif |
| 1401 | out: |
| 1402 | if (fclose(image)) { |
| 1403 | msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno)); |
| 1404 | ret = 1; |
| 1405 | } |
| 1406 | return ret; |
Nico Huber | 7562f7d | 2013-08-30 21:29:45 +0000 | [diff] [blame] | 1407 | #endif |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Nico Huber | 899e4ec | 2016-04-29 18:39:01 +0200 | [diff] [blame] | 1410 | static int read_by_layout(struct flashctx *, uint8_t *); |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 1411 | int read_flash_to_file(struct flashctx *flash, const char *filename) |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1412 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1413 | unsigned long size = flash->chip->total_size * 1024; |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1414 | unsigned char *buf = calloc(size, sizeof(char)); |
| 1415 | int ret = 0; |
| 1416 | |
| 1417 | msg_cinfo("Reading flash... "); |
| 1418 | if (!buf) { |
| 1419 | msg_gerr("Memory allocation failed!\n"); |
| 1420 | msg_cinfo("FAILED.\n"); |
| 1421 | return 1; |
| 1422 | } |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1423 | if (!flash->chip->read) { |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1424 | msg_cerr("No read function available for this flash chip.\n"); |
| 1425 | ret = 1; |
| 1426 | goto out_free; |
| 1427 | } |
Nico Huber | 899e4ec | 2016-04-29 18:39:01 +0200 | [diff] [blame] | 1428 | if (read_by_layout(flash, buf)) { |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1429 | msg_cerr("Read operation failed!\n"); |
| 1430 | ret = 1; |
| 1431 | goto out_free; |
| 1432 | } |
| 1433 | |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 1434 | ret = write_buf_to_file(buf, size, filename); |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 1435 | out_free: |
| 1436 | free(buf); |
| 1437 | msg_cinfo("%s.\n", ret ? "FAILED" : "done"); |
| 1438 | return ret; |
| 1439 | } |
| 1440 | |
Stefan Tauner | 96658be | 2014-05-26 22:05:31 +0000 | [diff] [blame] | 1441 | /* Even if an error is found, the function will keep going and check the rest. */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1442 | static int selfcheck_eraseblocks(const struct flashchip *chip) |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1443 | { |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 1444 | int i, j, k; |
| 1445 | int ret = 0; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1446 | |
| 1447 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
| 1448 | unsigned int done = 0; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1449 | struct block_eraser eraser = chip->block_erasers[k]; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1450 | |
| 1451 | for (i = 0; i < NUM_ERASEREGIONS; i++) { |
| 1452 | /* Blocks with zero size are bugs in flashchips.c. */ |
| 1453 | if (eraser.eraseblocks[i].count && |
| 1454 | !eraser.eraseblocks[i].size) { |
| 1455 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1456 | "%i region %i has size 0. Please report" |
| 1457 | " a bug at flashrom@flashrom.org\n", |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1458 | chip->name, k, i); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1459 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1460 | } |
| 1461 | /* Blocks with zero count are bugs in flashchips.c. */ |
| 1462 | if (!eraser.eraseblocks[i].count && |
| 1463 | eraser.eraseblocks[i].size) { |
| 1464 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1465 | "%i region %i has count 0. Please report" |
| 1466 | " a bug at flashrom@flashrom.org\n", |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1467 | chip->name, k, i); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1468 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1469 | } |
| 1470 | done += eraser.eraseblocks[i].count * |
| 1471 | eraser.eraseblocks[i].size; |
| 1472 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1473 | /* Empty eraseblock definition with erase function. */ |
| 1474 | if (!done && eraser.block_erase) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1475 | msg_gspew("Strange: Empty eraseblock definition with " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1476 | "non-empty erase function. Not an error.\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1477 | if (!done) |
| 1478 | continue; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1479 | if (done != chip->total_size * 1024) { |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1480 | msg_gerr("ERROR: Flash chip %s erase function %i " |
| 1481 | "region walking resulted in 0x%06x bytes total," |
| 1482 | " expected 0x%06x bytes. Please report a bug at" |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1483 | " flashrom@flashrom.org\n", chip->name, k, |
| 1484 | done, chip->total_size * 1024); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1485 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1486 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1487 | if (!eraser.block_erase) |
| 1488 | continue; |
| 1489 | /* Check if there are identical erase functions for different |
| 1490 | * layouts. That would imply "magic" erase functions. The |
| 1491 | * easiest way to check this is with function pointers. |
| 1492 | */ |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 1493 | for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) { |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1494 | if (eraser.block_erase == |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1495 | chip->block_erasers[j].block_erase) { |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1496 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1497 | "%i and %i are identical. Please report" |
| 1498 | " a bug at flashrom@flashrom.org\n", |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1499 | chip->name, k, j); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1500 | ret = 1; |
| 1501 | } |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 1502 | } |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1503 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1504 | return ret; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 1507 | static int check_block_eraser(const struct flashctx *flash, int k, int log) |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1508 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1509 | struct block_eraser eraser = flash->chip->block_erasers[k]; |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1510 | |
| 1511 | if (!eraser.block_erase && !eraser.eraseblocks[0].count) { |
| 1512 | if (log) |
| 1513 | msg_cdbg("not defined. "); |
| 1514 | return 1; |
| 1515 | } |
| 1516 | if (!eraser.block_erase && eraser.eraseblocks[0].count) { |
| 1517 | if (log) |
| 1518 | msg_cdbg("eraseblock layout is known, but matching " |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 1519 | "block erase function is not implemented. "); |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1520 | return 1; |
| 1521 | } |
| 1522 | if (eraser.block_erase && !eraser.eraseblocks[0].count) { |
| 1523 | if (log) |
| 1524 | msg_cdbg("block erase function found, but " |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 1525 | "eraseblock layout is not defined. "); |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1526 | return 1; |
| 1527 | } |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1528 | // TODO: Once erase functions are annotated with allowed buses, check that as well. |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1529 | return 0; |
| 1530 | } |
| 1531 | |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1532 | static const struct flashrom_layout *get_layout(const struct flashctx *const flashctx) |
| 1533 | { |
| 1534 | if (flashctx->layout && flashctx->layout->num_entries) |
| 1535 | return flashctx->layout; |
| 1536 | else |
| 1537 | return &flashctx->fallback_layout.base; |
| 1538 | } |
| 1539 | |
| 1540 | /** |
| 1541 | * @brief Reads the included layout regions into a buffer. |
| 1542 | * |
| 1543 | * If there is no layout set in the given flash context, the whole chip will |
| 1544 | * be read. |
| 1545 | * |
| 1546 | * @param flashctx Flash context to be used. |
| 1547 | * @param buffer Buffer of full chip size to read into. |
| 1548 | * @return 0 on success, |
| 1549 | * 1 if any read fails. |
| 1550 | */ |
| 1551 | static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer) |
| 1552 | { |
| 1553 | const struct flashrom_layout *const layout = get_layout(flashctx); |
| 1554 | |
| 1555 | size_t i; |
| 1556 | for (i = 0; i < layout->num_entries; ++i) { |
| 1557 | if (!layout->entries[i].included) |
| 1558 | continue; |
| 1559 | |
| 1560 | const chipoff_t region_start = layout->entries[i].start; |
| 1561 | const chipsize_t region_len = layout->entries[i].end - layout->entries[i].start + 1; |
| 1562 | |
| 1563 | if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len)) |
| 1564 | return 1; |
| 1565 | } |
| 1566 | return 0; |
| 1567 | } |
| 1568 | |
| 1569 | typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len); |
| 1570 | /** |
| 1571 | * @private |
| 1572 | * |
| 1573 | * For read-erase-write, `curcontents` and `newcontents` shall point |
| 1574 | * to buffers of the chip's size. Both are supposed to be prefilled |
| 1575 | * with at least the included layout regions of the current flash |
| 1576 | * contents (`curcontents`) and the data to be written to the flash |
| 1577 | * (`newcontents`). |
| 1578 | * |
| 1579 | * For erase, `curcontents` and `newcontents` shall be NULL-pointers. |
| 1580 | * |
| 1581 | * The `chipoff_t` values are used internally by `walk_by_layout()`. |
| 1582 | */ |
| 1583 | struct walk_info { |
| 1584 | uint8_t *curcontents; |
| 1585 | const uint8_t *newcontents; |
| 1586 | chipoff_t region_start; |
| 1587 | chipoff_t region_end; |
| 1588 | chipoff_t erase_start; |
| 1589 | chipoff_t erase_end; |
| 1590 | }; |
| 1591 | /* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */ |
| 1592 | typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t); |
| 1593 | |
| 1594 | static int walk_eraseblocks(struct flashctx *const flashctx, |
| 1595 | struct walk_info *const info, |
| 1596 | const size_t erasefunction, const per_blockfn_t per_blockfn) |
| 1597 | { |
| 1598 | int ret; |
| 1599 | size_t i, j; |
| 1600 | bool first = true; |
| 1601 | struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction]; |
| 1602 | |
| 1603 | info->erase_start = 0; |
| 1604 | for (i = 0; i < NUM_ERASEREGIONS; ++i) { |
| 1605 | /* count==0 for all automatically initialized array |
| 1606 | members so the loop below won't be executed for them. */ |
| 1607 | for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) { |
| 1608 | info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1; |
| 1609 | |
| 1610 | /* Skip any eraseblock that is completely outside the current region. */ |
| 1611 | if (info->erase_end < info->region_start) |
| 1612 | continue; |
| 1613 | if (info->region_end < info->erase_start) |
| 1614 | break; |
| 1615 | |
| 1616 | /* Print this for every block except the first one. */ |
| 1617 | if (first) |
| 1618 | first = false; |
| 1619 | else |
| 1620 | msg_cdbg(", "); |
| 1621 | msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end); |
| 1622 | |
| 1623 | ret = per_blockfn(flashctx, info, eraser->block_erase); |
| 1624 | if (ret) |
| 1625 | return ret; |
| 1626 | } |
| 1627 | if (info->region_end < info->erase_start) |
| 1628 | break; |
| 1629 | } |
| 1630 | msg_cdbg("\n"); |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
| 1634 | static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info, |
| 1635 | const per_blockfn_t per_blockfn) |
| 1636 | { |
| 1637 | const struct flashrom_layout *const layout = get_layout(flashctx); |
| 1638 | |
| 1639 | all_skipped = true; |
| 1640 | msg_cinfo("Erasing and writing flash chip... "); |
| 1641 | |
| 1642 | size_t i; |
| 1643 | for (i = 0; i < layout->num_entries; ++i) { |
| 1644 | if (!layout->entries[i].included) |
| 1645 | continue; |
| 1646 | |
| 1647 | info->region_start = layout->entries[i].start; |
| 1648 | info->region_end = layout->entries[i].end; |
| 1649 | |
| 1650 | size_t j; |
| 1651 | int error = 1; /* retry as long as it's 1 */ |
| 1652 | for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) { |
| 1653 | if (j != 0) |
| 1654 | msg_cinfo("Looking for another erase function.\n"); |
| 1655 | msg_cdbg("Trying erase function %zi... ", j); |
| 1656 | if (check_block_eraser(flashctx, j, 1)) |
| 1657 | continue; |
| 1658 | |
| 1659 | error = walk_eraseblocks(flashctx, info, j, per_blockfn); |
| 1660 | if (error != 1) |
| 1661 | break; |
| 1662 | |
| 1663 | if (info->curcontents) { |
| 1664 | msg_cinfo("Reading current flash chip contents... "); |
| 1665 | if (read_by_layout(flashctx, info->curcontents)) { |
| 1666 | /* Now we are truly screwed. Read failed as well. */ |
| 1667 | msg_cerr("Can't read anymore! Aborting.\n"); |
| 1668 | /* We have no idea about the flash chip contents, so |
| 1669 | retrying with another erase function is pointless. */ |
| 1670 | error = 2; |
| 1671 | break; |
| 1672 | } |
| 1673 | msg_cinfo("done. "); |
| 1674 | } |
| 1675 | } |
| 1676 | if (error == 1) |
| 1677 | msg_cinfo("No usable erase functions left.\n"); |
| 1678 | if (error) { |
| 1679 | msg_cerr("FAILED!\n"); |
| 1680 | return 1; |
| 1681 | } |
| 1682 | } |
| 1683 | if (all_skipped) |
| 1684 | msg_cinfo("\nWarning: Chip content is identical to the requested image.\n"); |
| 1685 | msg_cinfo("Erase/write done.\n"); |
| 1686 | return 0; |
| 1687 | } |
| 1688 | |
| 1689 | static int erase_block(struct flashctx *const flashctx, |
| 1690 | const struct walk_info *const info, const erasefn_t erasefn) |
| 1691 | { |
| 1692 | const unsigned int erase_len = info->erase_end + 1 - info->erase_start; |
| 1693 | |
| 1694 | all_skipped = false; |
| 1695 | |
| 1696 | msg_cdbg("E"); |
| 1697 | if (erasefn(flashctx, info->erase_start, erase_len)) |
| 1698 | return 1; |
| 1699 | if (check_erased_range(flashctx, info->erase_start, erase_len)) { |
| 1700 | msg_cerr("ERASE FAILED!\n"); |
| 1701 | return 1; |
| 1702 | } |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
| 1706 | /** |
| 1707 | * @brief Erases the included layout regions. |
| 1708 | * |
| 1709 | * If there is no layout set in the given flash context, the whole chip will |
| 1710 | * be erased. |
| 1711 | * |
| 1712 | * @param flashctx Flash context to be used. |
| 1713 | * @param buffer Buffer of full chip size to read into. |
| 1714 | * @return 0 on success, |
| 1715 | * 1 if all available erase functions failed. |
| 1716 | */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1717 | static int erase_by_layout(struct flashctx *const flashctx) |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1718 | { |
| 1719 | struct walk_info info = { 0 }; |
| 1720 | return walk_by_layout(flashctx, &info, &erase_block); |
| 1721 | } |
| 1722 | |
| 1723 | static int read_erase_write_block(struct flashctx *const flashctx, |
| 1724 | const struct walk_info *const info, const erasefn_t erasefn) |
| 1725 | { |
| 1726 | const chipsize_t erase_len = info->erase_end + 1 - info->erase_start; |
| 1727 | const bool region_unaligned = info->region_start > info->erase_start || |
| 1728 | info->erase_end > info->region_end; |
| 1729 | const uint8_t *newcontents = NULL; |
| 1730 | int ret = 2; |
| 1731 | |
| 1732 | /* |
| 1733 | * If the region is not erase-block aligned, merge current flash con- |
| 1734 | * tents into `info->curcontents` and a new buffer `newc`. The former |
| 1735 | * is necessary since we have no guarantee that the full erase block |
| 1736 | * was already read into `info->curcontents`. For the latter a new |
| 1737 | * buffer is used since `info->newcontents` might contain data for |
| 1738 | * other unaligned regions that touch this erase block too. |
| 1739 | */ |
| 1740 | if (region_unaligned) { |
| 1741 | msg_cdbg("R"); |
| 1742 | uint8_t *const newc = malloc(erase_len); |
| 1743 | if (!newc) { |
| 1744 | msg_cerr("Out of memory!\n"); |
| 1745 | return 1; |
| 1746 | } |
| 1747 | memcpy(newc, info->newcontents + info->erase_start, erase_len); |
| 1748 | |
| 1749 | /* Merge data preceding the current region. */ |
| 1750 | if (info->region_start > info->erase_start) { |
| 1751 | const chipoff_t start = info->erase_start; |
| 1752 | const chipsize_t len = info->region_start - info->erase_start; |
| 1753 | if (flashctx->chip->read(flashctx, newc, start, len)) { |
| 1754 | msg_cerr("Can't read! Aborting.\n"); |
| 1755 | goto _free_ret; |
| 1756 | } |
| 1757 | memcpy(info->curcontents + start, newc, len); |
| 1758 | } |
| 1759 | /* Merge data following the current region. */ |
| 1760 | if (info->erase_end > info->region_end) { |
| 1761 | const chipoff_t start = info->region_end + 1; |
| 1762 | const chipoff_t rel_start = start - info->erase_start; /* within this erase block */ |
| 1763 | const chipsize_t len = info->erase_end - info->region_end; |
| 1764 | if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) { |
| 1765 | msg_cerr("Can't read! Aborting.\n"); |
| 1766 | goto _free_ret; |
| 1767 | } |
| 1768 | memcpy(info->curcontents + start, newc + rel_start, len); |
| 1769 | } |
| 1770 | |
| 1771 | newcontents = newc; |
| 1772 | } else { |
| 1773 | newcontents = info->newcontents + info->erase_start; |
| 1774 | } |
| 1775 | |
| 1776 | ret = 1; |
| 1777 | bool skipped = true; |
| 1778 | uint8_t *const curcontents = info->curcontents + info->erase_start; |
| 1779 | if (need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran)) { |
| 1780 | if (erase_block(flashctx, info, erasefn)) |
| 1781 | goto _free_ret; |
| 1782 | /* Erase was successful. Adjust curcontents. */ |
| 1783 | memset(curcontents, 0xff, erase_len); |
| 1784 | skipped = false; |
| 1785 | } |
| 1786 | |
| 1787 | unsigned int starthere = 0, lenhere = 0, writecount = 0; |
| 1788 | /* get_next_write() sets starthere to a new value after the call. */ |
| 1789 | while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere, |
| 1790 | erase_len - starthere, &starthere, flashctx->chip->gran))) { |
| 1791 | if (!writecount++) |
| 1792 | msg_cdbg("W"); |
| 1793 | /* Needs the partial write function signature. */ |
| 1794 | if (flashctx->chip->write(flashctx, newcontents + starthere, |
| 1795 | info->erase_start + starthere, lenhere)) |
| 1796 | goto _free_ret; |
| 1797 | starthere += lenhere; |
| 1798 | skipped = false; |
| 1799 | } |
| 1800 | if (skipped) |
| 1801 | msg_cdbg("S"); |
| 1802 | else |
| 1803 | all_skipped = false; |
| 1804 | |
| 1805 | /* Update curcontents, other regions with overlapping erase blocks |
| 1806 | might rely on this. */ |
| 1807 | memcpy(curcontents, newcontents, erase_len); |
| 1808 | ret = 0; |
| 1809 | |
| 1810 | _free_ret: |
| 1811 | if (region_unaligned) |
| 1812 | free((void *)newcontents); |
| 1813 | return ret; |
| 1814 | } |
| 1815 | |
| 1816 | /** |
| 1817 | * @brief Writes the included layout regions from a given image. |
| 1818 | * |
| 1819 | * If there is no layout set in the given flash context, the whole image |
| 1820 | * will be written. |
| 1821 | * |
| 1822 | * @param flashctx Flash context to be used. |
| 1823 | * @param curcontents A buffer of full chip size with current chip contents of included regions. |
| 1824 | * @param newcontents The new image to be written. |
| 1825 | * @return 0 on success, |
| 1826 | * 1 if anything has gone wrong. |
| 1827 | */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1828 | static int write_by_layout(struct flashctx *const flashctx, |
| 1829 | void *const curcontents, const void *const newcontents) |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1830 | { |
| 1831 | struct walk_info info; |
| 1832 | info.curcontents = curcontents; |
| 1833 | info.newcontents = newcontents; |
| 1834 | return walk_by_layout(flashctx, &info, read_erase_write_block); |
| 1835 | } |
| 1836 | |
| 1837 | /** |
| 1838 | * @brief Compares the included layout regions with content from a buffer. |
| 1839 | * |
| 1840 | * If there is no layout set in the given flash context, the whole chip's |
| 1841 | * contents will be compared. |
| 1842 | * |
| 1843 | * @param flashctx Flash context to be used. |
| 1844 | * @param curcontents A buffer of full chip size to read current chip contents into. |
| 1845 | * @param newcontents The new image to compare to. |
| 1846 | * @return 0 on success, |
| 1847 | * 1 if reading failed, |
| 1848 | * 3 if the contents don't match. |
| 1849 | */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1850 | static int verify_by_layout(struct flashctx *const flashctx, |
| 1851 | void *const curcontents, const uint8_t *const newcontents) |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1852 | { |
| 1853 | const struct flashrom_layout *const layout = get_layout(flashctx); |
| 1854 | |
| 1855 | size_t i; |
| 1856 | for (i = 0; i < layout->num_entries; ++i) { |
| 1857 | if (!layout->entries[i].included) |
| 1858 | continue; |
| 1859 | |
| 1860 | const chipoff_t region_start = layout->entries[i].start; |
| 1861 | const chipsize_t region_len = layout->entries[i].end - layout->entries[i].start + 1; |
| 1862 | |
| 1863 | if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len)) |
| 1864 | return 1; |
| 1865 | if (compare_range(newcontents + region_start, curcontents + region_start, |
| 1866 | region_start, region_len)) |
| 1867 | return 3; |
| 1868 | } |
| 1869 | return 0; |
| 1870 | } |
| 1871 | |
Stefan Tauner | 136388f | 2013-07-15 10:47:53 +0000 | [diff] [blame] | 1872 | static void nonfatal_help_message(void) |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1873 | { |
Stefan Tauner | a58f6e9 | 2014-05-10 09:25:44 +0000 | [diff] [blame] | 1874 | msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n"); |
Stefan Tauner | 136388f | 2013-07-15 10:47:53 +0000 | [diff] [blame] | 1875 | #if CONFIG_INTERNAL == 1 |
| 1876 | if (programmer == PROGRAMMER_INTERNAL) |
| 1877 | msg_gerr("This means we have to add special support for your board, programmer or flash\n" |
| 1878 | "chip. Please report this on IRC at chat.freenode.net (channel #flashrom) or\n" |
| 1879 | "mail flashrom@flashrom.org, thanks!\n" |
| 1880 | "-------------------------------------------------------------------------------\n" |
| 1881 | "You may now reboot or simply leave the machine running.\n"); |
| 1882 | else |
| 1883 | #endif |
| 1884 | msg_gerr("Please check the connections (especially those to write protection pins) between\n" |
| 1885 | "the programmer and the flash chip. If you think the error is caused by flashrom\n" |
| 1886 | "please report this on IRC at chat.freenode.net (channel #flashrom) or\n" |
| 1887 | "mail flashrom@flashrom.org, thanks!\n"); |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
Stefan Tauner | 136388f | 2013-07-15 10:47:53 +0000 | [diff] [blame] | 1890 | static void emergency_help_message(void) |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1891 | { |
Stefan Tauner | 136388f | 2013-07-15 10:47:53 +0000 | [diff] [blame] | 1892 | msg_gerr("Your flash chip is in an unknown state.\n"); |
| 1893 | #if CONFIG_INTERNAL == 1 |
| 1894 | if (programmer == PROGRAMMER_INTERNAL) |
| 1895 | msg_gerr("Get help on IRC at chat.freenode.net (channel #flashrom) or\n" |
| 1896 | "mail flashrom@flashrom.org with the subject \"FAILED: <your board name>\"!\n" |
| 1897 | "-------------------------------------------------------------------------------\n" |
| 1898 | "DO NOT REBOOT OR POWEROFF!\n"); |
| 1899 | else |
| 1900 | #endif |
| 1901 | msg_gerr("Please report this on IRC at chat.freenode.net (channel #flashrom) or\n" |
| 1902 | "mail flashrom@flashrom.org, thanks!\n"); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1905 | /* The way to go if you want a delimited list of programmers */ |
Stefan Tauner | 6665244 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 1906 | void list_programmers(const char *delim) |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1907 | { |
| 1908 | enum programmer p; |
| 1909 | for (p = 0; p < PROGRAMMER_INVALID; p++) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1910 | msg_ginfo("%s", programmer_table[p].name); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1911 | if (p < PROGRAMMER_INVALID - 1) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1912 | msg_ginfo("%s", delim); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1913 | } |
Stefan Tauner | 23e10b8 | 2016-01-23 16:16:49 +0000 | [diff] [blame] | 1914 | msg_ginfo("\n"); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1917 | void list_programmers_linebreak(int startcol, int cols, int paren) |
| 1918 | { |
| 1919 | const char *pname; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 1920 | int pnamelen; |
| 1921 | int remaining = 0, firstline = 1; |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1922 | enum programmer p; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 1923 | int i; |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1924 | |
| 1925 | for (p = 0; p < PROGRAMMER_INVALID; p++) { |
| 1926 | pname = programmer_table[p].name; |
| 1927 | pnamelen = strlen(pname); |
| 1928 | if (remaining - pnamelen - 2 < 0) { |
| 1929 | if (firstline) |
| 1930 | firstline = 0; |
| 1931 | else |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1932 | msg_ginfo("\n"); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1933 | for (i = 0; i < startcol; i++) |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1934 | msg_ginfo(" "); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1935 | remaining = cols - startcol; |
| 1936 | } else { |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1937 | msg_ginfo(" "); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1938 | remaining--; |
| 1939 | } |
| 1940 | if (paren && (p == 0)) { |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1941 | msg_ginfo("("); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1942 | remaining--; |
| 1943 | } |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1944 | msg_ginfo("%s", pname); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1945 | remaining -= pnamelen; |
| 1946 | if (p < PROGRAMMER_INVALID - 1) { |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1947 | msg_ginfo(","); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1948 | remaining--; |
| 1949 | } else { |
| 1950 | if (paren) |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1951 | msg_ginfo(")"); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1952 | } |
| 1953 | } |
| 1954 | } |
| 1955 | |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1956 | void print_sysinfo(void) |
| 1957 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 1958 | #if IS_WINDOWS |
Carl-Daniel Hailfinger | 60d9bd2 | 2012-08-09 23:34:41 +0000 | [diff] [blame] | 1959 | SYSTEM_INFO si; |
| 1960 | OSVERSIONINFOEX osvi; |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1961 | |
Carl-Daniel Hailfinger | 60d9bd2 | 2012-08-09 23:34:41 +0000 | [diff] [blame] | 1962 | memset(&si, 0, sizeof(SYSTEM_INFO)); |
| 1963 | memset(&osvi, 0, sizeof(OSVERSIONINFOEX)); |
| 1964 | msg_ginfo(" on Windows"); |
| 1965 | /* Tell Windows which version of the structure we want. */ |
| 1966 | osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); |
| 1967 | if (GetVersionEx((OSVERSIONINFO*) &osvi)) |
| 1968 | msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion); |
| 1969 | else |
| 1970 | msg_ginfo(" unknown version"); |
| 1971 | GetSystemInfo(&si); |
| 1972 | switch (si.wProcessorArchitecture) { |
| 1973 | case PROCESSOR_ARCHITECTURE_AMD64: |
| 1974 | msg_ginfo(" (x86_64)"); |
| 1975 | break; |
| 1976 | case PROCESSOR_ARCHITECTURE_INTEL: |
| 1977 | msg_ginfo(" (x86)"); |
| 1978 | break; |
| 1979 | default: |
| 1980 | msg_ginfo(" (unknown arch)"); |
| 1981 | break; |
| 1982 | } |
| 1983 | #elif HAVE_UTSNAME == 1 |
| 1984 | struct utsname osinfo; |
| 1985 | |
| 1986 | uname(&osinfo); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1987 | msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release, |
| 1988 | osinfo.machine); |
| 1989 | #else |
| 1990 | msg_ginfo(" on unknown machine"); |
| 1991 | #endif |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | void print_buildinfo(void) |
| 1995 | { |
| 1996 | msg_gdbg("flashrom was built with"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1997 | #if NEED_PCI == 1 |
| 1998 | #ifdef PCILIB_VERSION |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1999 | msg_gdbg(" libpci %s,", PCILIB_VERSION); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 2000 | #else |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 2001 | msg_gdbg(" unknown PCI library,"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 2002 | #endif |
| 2003 | #endif |
| 2004 | #ifdef __clang__ |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 2005 | msg_gdbg(" LLVM Clang"); |
Carl-Daniel Hailfinger | b51e58e | 2010-07-17 14:49:30 +0000 | [diff] [blame] | 2006 | #ifdef __clang_version__ |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 2007 | msg_gdbg(" %s,", __clang_version__); |
Carl-Daniel Hailfinger | b51e58e | 2010-07-17 14:49:30 +0000 | [diff] [blame] | 2008 | #else |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 2009 | msg_gdbg(" unknown version (before r102686),"); |
Carl-Daniel Hailfinger | b51e58e | 2010-07-17 14:49:30 +0000 | [diff] [blame] | 2010 | #endif |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 2011 | #elif defined(__GNUC__) |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 2012 | msg_gdbg(" GCC"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 2013 | #ifdef __VERSION__ |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 2014 | msg_gdbg(" %s,", __VERSION__); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 2015 | #else |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 2016 | msg_gdbg(" unknown version,"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 2017 | #endif |
| 2018 | #else |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 2019 | msg_gdbg(" unknown compiler,"); |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 2020 | #endif |
| 2021 | #if defined (__FLASHROM_LITTLE_ENDIAN__) |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 2022 | msg_gdbg(" little endian"); |
Carl-Daniel Hailfinger | 06b9efa | 2012-08-07 11:59:59 +0000 | [diff] [blame] | 2023 | #elif defined (__FLASHROM_BIG_ENDIAN__) |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 2024 | msg_gdbg(" big endian"); |
Carl-Daniel Hailfinger | 06b9efa | 2012-08-07 11:59:59 +0000 | [diff] [blame] | 2025 | #else |
| 2026 | #error Endianness could not be determined |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 2027 | #endif |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 2028 | msg_gdbg("\n"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Bernhard Walle | 201bde3 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 2031 | void print_version(void) |
| 2032 | { |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 2033 | msg_ginfo("flashrom %s", flashrom_version); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 2034 | print_sysinfo(); |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 2035 | msg_ginfo("\n"); |
Bernhard Walle | 201bde3 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 2038 | void print_banner(void) |
| 2039 | { |
| 2040 | msg_ginfo("flashrom is free software, get the source code at " |
Stefan Tauner | 4c72315 | 2016-01-14 22:47:55 +0000 | [diff] [blame] | 2041 | "https://flashrom.org\n"); |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 2042 | msg_ginfo("\n"); |
| 2043 | } |
| 2044 | |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 2045 | int selfcheck(void) |
| 2046 | { |
Stefan Tauner | 96658be | 2014-05-26 22:05:31 +0000 | [diff] [blame] | 2047 | unsigned int i; |
Stefan Tauner | a6d9648 | 2012-12-26 19:51:23 +0000 | [diff] [blame] | 2048 | int ret = 0; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 2049 | |
| 2050 | /* Safety check. Instead of aborting after the first error, check |
| 2051 | * if more errors exist. |
| 2052 | */ |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 2053 | if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 2054 | msg_gerr("Programmer table miscompilation!\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 2055 | ret = 1; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 2056 | } |
Stefan Tauner | a6d9648 | 2012-12-26 19:51:23 +0000 | [diff] [blame] | 2057 | for (i = 0; i < PROGRAMMER_INVALID; i++) { |
| 2058 | const struct programmer_entry p = programmer_table[i]; |
| 2059 | if (p.name == NULL) { |
| 2060 | msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i); |
| 2061 | ret = 1; |
| 2062 | /* This might hide other problems with this programmer, but allows for better error |
| 2063 | * messages below without jumping through hoops. */ |
| 2064 | continue; |
| 2065 | } |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 2066 | switch (p.type) { |
| 2067 | case USB: |
| 2068 | case PCI: |
| 2069 | case OTHER: |
| 2070 | if (p.devs.note == NULL) { |
| 2071 | if (strcmp("internal", p.name) == 0) |
| 2072 | break; /* This one has its device list stored separately. */ |
| 2073 | msg_gerr("Programmer %s has neither a device list nor a textual description!\n", |
| 2074 | p.name); |
| 2075 | ret = 1; |
| 2076 | } |
| 2077 | break; |
| 2078 | default: |
| 2079 | msg_gerr("Programmer %s does not have a valid type set!\n", p.name); |
| 2080 | ret = 1; |
| 2081 | break; |
| 2082 | } |
Stefan Tauner | a6d9648 | 2012-12-26 19:51:23 +0000 | [diff] [blame] | 2083 | if (p.init == NULL) { |
| 2084 | msg_gerr("Programmer %s does not have a valid init function!\n", p.name); |
| 2085 | ret = 1; |
| 2086 | } |
| 2087 | if (p.delay == NULL) { |
| 2088 | msg_gerr("Programmer %s does not have a valid delay function!\n", p.name); |
| 2089 | ret = 1; |
| 2090 | } |
| 2091 | if (p.map_flash_region == NULL) { |
| 2092 | msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p.name); |
| 2093 | ret = 1; |
| 2094 | } |
| 2095 | if (p.unmap_flash_region == NULL) { |
| 2096 | msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p.name); |
| 2097 | ret = 1; |
| 2098 | } |
| 2099 | } |
Stefan Tauner | 96658be | 2014-05-26 22:05:31 +0000 | [diff] [blame] | 2100 | |
| 2101 | /* It would be favorable if we could check for the correct layout (especially termination) of various |
| 2102 | * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known. |
| 2103 | * They are all defined as externs in this compilation unit so we don't know their sizes which vary |
| 2104 | * depending on compiler flags, e.g. the target architecture, and can sometimes be 0. |
| 2105 | * For 'flashchips' we export the size explicitly to work around this and to be able to implement the |
| 2106 | * checks below. */ |
Stefan Tauner | 6697f71 | 2014-08-06 15:09:15 +0000 | [diff] [blame] | 2107 | if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) { |
Stefan Tauner | 7bcacb1 | 2011-05-26 01:35:19 +0000 | [diff] [blame] | 2108 | msg_gerr("Flashchips table miscompilation!\n"); |
| 2109 | ret = 1; |
Stefan Tauner | 96658be | 2014-05-26 22:05:31 +0000 | [diff] [blame] | 2110 | } else { |
| 2111 | for (i = 0; i < flashchips_size - 1; i++) { |
| 2112 | const struct flashchip *chip = &flashchips[i]; |
| 2113 | if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) { |
| 2114 | ret = 1; |
| 2115 | msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n" |
| 2116 | "Please report a bug at flashrom@flashrom.org\n", i, |
| 2117 | chip->name == NULL ? "unnamed" : chip->name); |
| 2118 | } |
| 2119 | if (selfcheck_eraseblocks(chip)) { |
| 2120 | ret = 1; |
| 2121 | } |
| 2122 | } |
Stefan Tauner | 7bcacb1 | 2011-05-26 01:35:19 +0000 | [diff] [blame] | 2123 | } |
Stefan Tauner | 7bcacb1 | 2011-05-26 01:35:19 +0000 | [diff] [blame] | 2124 | |
Stefan Tauner | 600576b | 2014-06-12 22:57:36 +0000 | [diff] [blame] | 2125 | #if CONFIG_INTERNAL == 1 |
| 2126 | ret |= selfcheck_board_enables(); |
| 2127 | #endif |
| 2128 | |
Stefan Tauner | 96658be | 2014-05-26 22:05:31 +0000 | [diff] [blame] | 2129 | /* TODO: implement similar sanity checks for other arrays where deemed necessary. */ |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 2130 | return ret; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 2133 | /* FIXME: This function signature needs to be improved once doit() has a better |
| 2134 | * function signature. |
| 2135 | */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 2136 | int chip_safety_check(const struct flashctx *flash, int force, int read_it, int write_it, int erase_it, |
| 2137 | int verify_it) |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 2138 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 2139 | const struct flashchip *chip = flash->chip; |
| 2140 | |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 2141 | if (!programmer_may_write && (write_it || erase_it)) { |
| 2142 | msg_perr("Write/erase is not working yet on your programmer in " |
| 2143 | "its current configuration.\n"); |
| 2144 | /* --force is the wrong approach, but it's the best we can do |
| 2145 | * until the generic programmer parameter parser is merged. |
| 2146 | */ |
| 2147 | if (!force) |
| 2148 | return 1; |
| 2149 | msg_cerr("Continuing anyway.\n"); |
| 2150 | } |
| 2151 | |
| 2152 | if (read_it || erase_it || write_it || verify_it) { |
| 2153 | /* Everything needs read. */ |
Stefan Tauner | 6455dff | 2014-05-26 00:36:24 +0000 | [diff] [blame] | 2154 | if (chip->tested.read == BAD) { |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 2155 | msg_cerr("Read is not working on this chip. "); |
| 2156 | if (!force) |
| 2157 | return 1; |
| 2158 | msg_cerr("Continuing anyway.\n"); |
| 2159 | } |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 2160 | if (!chip->read) { |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 2161 | msg_cerr("flashrom has no read function for this " |
| 2162 | "flash chip.\n"); |
| 2163 | return 1; |
| 2164 | } |
| 2165 | } |
| 2166 | if (erase_it || write_it) { |
| 2167 | /* Write needs erase. */ |
Stefan Tauner | 6455dff | 2014-05-26 00:36:24 +0000 | [diff] [blame] | 2168 | if (chip->tested.erase == NA) { |
| 2169 | msg_cerr("Erase is not possible on this chip.\n"); |
| 2170 | return 1; |
| 2171 | } |
| 2172 | if (chip->tested.erase == BAD) { |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 2173 | msg_cerr("Erase is not working on this chip. "); |
| 2174 | if (!force) |
| 2175 | return 1; |
| 2176 | msg_cerr("Continuing anyway.\n"); |
| 2177 | } |
Sylvain "ythier" Hitier | 9db4551 | 2011-07-04 07:27:17 +0000 | [diff] [blame] | 2178 | if(count_usable_erasers(flash) == 0) { |
Stefan Tauner | 5368dca | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 2179 | msg_cerr("flashrom has no erase function for this " |
| 2180 | "flash chip.\n"); |
| 2181 | return 1; |
| 2182 | } |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 2183 | } |
| 2184 | if (write_it) { |
Stefan Tauner | 6455dff | 2014-05-26 00:36:24 +0000 | [diff] [blame] | 2185 | if (chip->tested.write == NA) { |
| 2186 | msg_cerr("Write is not possible on this chip.\n"); |
| 2187 | return 1; |
| 2188 | } |
| 2189 | if (chip->tested.write == BAD) { |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 2190 | msg_cerr("Write is not working on this chip. "); |
| 2191 | if (!force) |
| 2192 | return 1; |
| 2193 | msg_cerr("Continuing anyway.\n"); |
| 2194 | } |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 2195 | if (!chip->write) { |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 2196 | msg_cerr("flashrom has no write function for this " |
| 2197 | "flash chip.\n"); |
| 2198 | return 1; |
| 2199 | } |
| 2200 | } |
| 2201 | return 0; |
| 2202 | } |
| 2203 | |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 2204 | int prepare_flash_access(struct flashctx *const flash, |
| 2205 | const bool read_it, const bool write_it, |
| 2206 | const bool erase_it, const bool verify_it) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 2207 | { |
| 2208 | if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) { |
| 2209 | msg_cerr("Aborting.\n"); |
| 2210 | return 1; |
| 2211 | } |
| 2212 | |
| 2213 | if (flash->layout == get_global_layout() && normalize_romentries(flash)) { |
| 2214 | msg_cerr("Requested regions can not be handled. Aborting.\n"); |
| 2215 | return 1; |
| 2216 | } |
| 2217 | |
| 2218 | if (map_flash(flash) != 0) |
| 2219 | return 1; |
| 2220 | |
| 2221 | /* Given the existence of read locks, we want to unlock for read, |
| 2222 | erase and write. */ |
| 2223 | if (flash->chip->unlock) |
| 2224 | flash->chip->unlock(flash); |
| 2225 | |
Ed Swierk | cc20a9b | 2017-07-03 13:17:18 -0700 | [diff] [blame^] | 2226 | /* Enable/disable 4-byte addressing mode if flash chip supports it */ |
| 2227 | if ((flash->chip->feature_bits & FEATURE_4BA_SUPPORT) && |
| 2228 | flash->chip->four_bytes_addr_funcs.set_4ba) { |
| 2229 | if (flash->chip->four_bytes_addr_funcs.set_4ba(flash)) { |
| 2230 | msg_cerr("Enabling/disabling 4-byte addressing mode failed!\n"); |
| 2231 | return 1; |
Boris Baykov | 7fe8569 | 2016-06-11 18:29:03 +0200 | [diff] [blame] | 2232 | } |
Boris Baykov | 9912718 | 2016-06-11 18:29:00 +0200 | [diff] [blame] | 2233 | } |
| 2234 | |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 2235 | return 0; |
| 2236 | } |
| 2237 | |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 2238 | void finalize_flash_access(struct flashctx *const flash) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 2239 | { |
| 2240 | unmap_flash(flash); |
| 2241 | } |
| 2242 | |
| 2243 | /** |
| 2244 | * @addtogroup flashrom-flash |
| 2245 | * @{ |
| 2246 | */ |
| 2247 | |
| 2248 | /** |
| 2249 | * @brief Erase the specified ROM chip. |
| 2250 | * |
| 2251 | * If a layout is set in the given flash context, only included regions |
| 2252 | * will be erased. |
| 2253 | * |
| 2254 | * @param flashctx The context of the flash chip to erase. |
| 2255 | * @return 0 on success. |
| 2256 | */ |
| 2257 | int flashrom_flash_erase(struct flashctx *const flashctx) |
| 2258 | { |
| 2259 | if (prepare_flash_access(flashctx, false, false, true, false)) |
| 2260 | return 1; |
| 2261 | |
| 2262 | const int ret = erase_by_layout(flashctx); |
| 2263 | |
| 2264 | finalize_flash_access(flashctx); |
| 2265 | |
| 2266 | return ret; |
| 2267 | } |
| 2268 | |
| 2269 | /** @} */ /* end flashrom-flash */ |
| 2270 | |
| 2271 | /** |
| 2272 | * @defgroup flashrom-ops Operations |
| 2273 | * @{ |
| 2274 | */ |
| 2275 | |
| 2276 | /** |
| 2277 | * @brief Read the current image from the specified ROM chip. |
| 2278 | * |
| 2279 | * If a layout is set in the specified flash context, only included regions |
| 2280 | * will be read. |
| 2281 | * |
| 2282 | * @param flashctx The context of the flash chip. |
| 2283 | * @param buffer Target buffer to write image to. |
| 2284 | * @param buffer_len Size of target buffer in bytes. |
| 2285 | * @return 0 on success, |
| 2286 | * 2 if buffer_len is too short for the flash chip's contents, |
| 2287 | * or 1 on any other failure. |
| 2288 | */ |
| 2289 | int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len) |
| 2290 | { |
| 2291 | const size_t flash_size = flashctx->chip->total_size * 1024; |
| 2292 | |
| 2293 | if (flash_size > buffer_len) |
| 2294 | return 2; |
| 2295 | |
| 2296 | if (prepare_flash_access(flashctx, true, false, false, false)) |
| 2297 | return 1; |
| 2298 | |
| 2299 | msg_cinfo("Reading flash... "); |
| 2300 | |
| 2301 | int ret = 1; |
| 2302 | if (read_by_layout(flashctx, buffer)) { |
| 2303 | msg_cerr("Read operation failed!\n"); |
| 2304 | msg_cinfo("FAILED.\n"); |
| 2305 | goto _finalize_ret; |
| 2306 | } |
| 2307 | msg_cinfo("done.\n"); |
| 2308 | ret = 0; |
| 2309 | |
| 2310 | _finalize_ret: |
| 2311 | finalize_flash_access(flashctx); |
| 2312 | return ret; |
| 2313 | } |
| 2314 | |
| 2315 | static void combine_image_by_layout(const struct flashctx *const flashctx, |
| 2316 | uint8_t *const newcontents, const uint8_t *const oldcontents) |
| 2317 | { |
| 2318 | const struct flashrom_layout *const layout = get_layout(flashctx); |
| 2319 | |
| 2320 | size_t i; |
| 2321 | for (i = 0; i < layout->num_entries; ++i) { |
| 2322 | if (layout->entries[i].included) |
| 2323 | continue; |
| 2324 | |
| 2325 | const chipoff_t region_start = layout->entries[i].start; |
| 2326 | const chipsize_t region_len = layout->entries[i].end - layout->entries[i].start + 1; |
| 2327 | |
| 2328 | memcpy(newcontents + region_start, oldcontents + region_start, region_len); |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | /** |
| 2333 | * @brief Write the specified image to the ROM chip. |
| 2334 | * |
| 2335 | * If a layout is set in the specified flash context, only erase blocks |
| 2336 | * containing included regions will be touched. |
| 2337 | * |
| 2338 | * @param flashctx The context of the flash chip. |
Nico Huber | 1b172f2 | 2017-06-19 12:35:24 +0200 | [diff] [blame] | 2339 | * @param buffer Source buffer to read image from (may be altered for full verification). |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 2340 | * @param buffer_len Size of source buffer in bytes. |
| 2341 | * @return 0 on success, |
| 2342 | * 4 if buffer_len doesn't match the size of the flash chip, |
| 2343 | * 3 if write was tried but nothing has changed, |
| 2344 | * 2 if write failed and flash contents changed, |
| 2345 | * or 1 on any other failure. |
| 2346 | */ |
| 2347 | int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len) |
| 2348 | { |
| 2349 | const size_t flash_size = flashctx->chip->total_size * 1024; |
| 2350 | const bool verify_all = flashctx->flags.verify_whole_chip; |
| 2351 | const bool verify = flashctx->flags.verify_after_write; |
| 2352 | |
| 2353 | if (buffer_len != flash_size) |
| 2354 | return 4; |
| 2355 | |
| 2356 | int ret = 1; |
| 2357 | |
| 2358 | uint8_t *const newcontents = buffer; |
| 2359 | uint8_t *const curcontents = malloc(flash_size); |
| 2360 | uint8_t *oldcontents = NULL; |
| 2361 | if (verify_all) |
| 2362 | oldcontents = malloc(flash_size); |
| 2363 | if (!curcontents || (verify_all && !oldcontents)) { |
| 2364 | msg_gerr("Out of memory!\n"); |
| 2365 | goto _free_ret; |
| 2366 | } |
| 2367 | |
| 2368 | #if CONFIG_INTERNAL == 1 |
| 2369 | if (programmer == PROGRAMMER_INTERNAL && cb_check_image(newcontents, flash_size) < 0) { |
| 2370 | if (flashctx->flags.force_boardmismatch) { |
| 2371 | msg_pinfo("Proceeding anyway because user forced us to.\n"); |
| 2372 | } else { |
| 2373 | msg_perr("Aborting. You can override this with " |
| 2374 | "-p internal:boardmismatch=force.\n"); |
| 2375 | goto _free_ret; |
| 2376 | } |
| 2377 | } |
| 2378 | #endif |
| 2379 | |
| 2380 | if (prepare_flash_access(flashctx, false, true, false, verify)) |
| 2381 | goto _free_ret; |
| 2382 | |
| 2383 | /* |
| 2384 | * Read the whole chip to be able to check whether regions need to be |
| 2385 | * erased and to give better diagnostics in case write fails. |
| 2386 | * The alternative is to read only the regions which are to be |
| 2387 | * preserved, but in that case we might perform unneeded erase which |
| 2388 | * takes time as well. |
| 2389 | */ |
| 2390 | msg_cinfo("Reading old flash chip contents... "); |
| 2391 | if (verify_all) { |
| 2392 | if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) { |
| 2393 | msg_cinfo("FAILED.\n"); |
| 2394 | goto _finalize_ret; |
| 2395 | } |
| 2396 | memcpy(curcontents, oldcontents, flash_size); |
| 2397 | } else { |
| 2398 | if (read_by_layout(flashctx, curcontents)) { |
| 2399 | msg_cinfo("FAILED.\n"); |
| 2400 | goto _finalize_ret; |
| 2401 | } |
| 2402 | } |
| 2403 | msg_cinfo("done.\n"); |
| 2404 | |
| 2405 | if (write_by_layout(flashctx, curcontents, newcontents)) { |
| 2406 | msg_cerr("Uh oh. Erase/write failed. "); |
| 2407 | ret = 2; |
| 2408 | if (verify_all) { |
| 2409 | msg_cerr("Checking if anything has changed.\n"); |
| 2410 | msg_cinfo("Reading current flash chip contents... "); |
| 2411 | if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) { |
| 2412 | msg_cinfo("done.\n"); |
| 2413 | if (!memcmp(oldcontents, curcontents, flash_size)) { |
| 2414 | nonfatal_help_message(); |
| 2415 | goto _finalize_ret; |
| 2416 | } |
| 2417 | msg_cerr("Apparently at least some data has changed.\n"); |
| 2418 | } else |
| 2419 | msg_cerr("Can't even read anymore!\n"); |
| 2420 | emergency_help_message(); |
| 2421 | goto _finalize_ret; |
| 2422 | } else { |
| 2423 | msg_cerr("\n"); |
| 2424 | } |
| 2425 | emergency_help_message(); |
| 2426 | goto _finalize_ret; |
| 2427 | } |
| 2428 | |
| 2429 | /* Verify only if we actually changed something. */ |
| 2430 | if (verify && !all_skipped) { |
| 2431 | const struct flashrom_layout *const layout_bak = flashctx->layout; |
| 2432 | |
| 2433 | msg_cinfo("Verifying flash... "); |
| 2434 | |
| 2435 | /* Work around chips which need some time to calm down. */ |
| 2436 | programmer_delay(1000*1000); |
| 2437 | |
| 2438 | if (verify_all) { |
| 2439 | combine_image_by_layout(flashctx, newcontents, oldcontents); |
| 2440 | flashctx->layout = NULL; |
| 2441 | } |
| 2442 | ret = verify_by_layout(flashctx, curcontents, newcontents); |
| 2443 | flashctx->layout = layout_bak; |
| 2444 | /* If we tried to write, and verification now fails, we |
| 2445 | might have an emergency situation. */ |
| 2446 | if (ret) |
| 2447 | emergency_help_message(); |
| 2448 | else |
| 2449 | msg_cinfo("VERIFIED.\n"); |
| 2450 | } else { |
| 2451 | /* We didn't change anything. */ |
| 2452 | ret = 0; |
| 2453 | } |
| 2454 | |
| 2455 | _finalize_ret: |
| 2456 | finalize_flash_access(flashctx); |
| 2457 | _free_ret: |
| 2458 | free(oldcontents); |
| 2459 | free(curcontents); |
| 2460 | return ret; |
| 2461 | } |
| 2462 | |
| 2463 | /** |
| 2464 | * @brief Verify the ROM chip's contents with the specified image. |
| 2465 | * |
| 2466 | * If a layout is set in the specified flash context, only included regions |
| 2467 | * will be verified. |
| 2468 | * |
| 2469 | * @param flashctx The context of the flash chip. |
| 2470 | * @param buffer Source buffer to verify with. |
| 2471 | * @param buffer_len Size of source buffer in bytes. |
| 2472 | * @return 0 on success, |
| 2473 | * 3 if the chip's contents don't match, |
| 2474 | * 2 if buffer_len doesn't match the size of the flash chip, |
| 2475 | * or 1 on any other failure. |
| 2476 | */ |
| 2477 | int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len) |
| 2478 | { |
| 2479 | const size_t flash_size = flashctx->chip->total_size * 1024; |
| 2480 | |
| 2481 | if (buffer_len != flash_size) |
| 2482 | return 2; |
| 2483 | |
| 2484 | const uint8_t *const newcontents = buffer; |
| 2485 | uint8_t *const curcontents = malloc(flash_size); |
| 2486 | if (!curcontents) { |
| 2487 | msg_gerr("Out of memory!\n"); |
| 2488 | return 1; |
| 2489 | } |
| 2490 | |
| 2491 | int ret = 1; |
| 2492 | |
| 2493 | if (prepare_flash_access(flashctx, false, false, false, true)) |
| 2494 | goto _free_ret; |
| 2495 | |
| 2496 | msg_cinfo("Verifying flash... "); |
| 2497 | ret = verify_by_layout(flashctx, curcontents, newcontents); |
| 2498 | if (!ret) |
| 2499 | msg_cinfo("VERIFIED.\n"); |
| 2500 | |
| 2501 | finalize_flash_access(flashctx); |
| 2502 | _free_ret: |
| 2503 | free(curcontents); |
| 2504 | return ret; |
| 2505 | } |
| 2506 | |
| 2507 | /** @} */ /* end flashrom-ops */ |
Nico Huber | 899e4ec | 2016-04-29 18:39:01 +0200 | [diff] [blame] | 2508 | |
| 2509 | int do_read(struct flashctx *const flash, const char *const filename) |
| 2510 | { |
| 2511 | if (prepare_flash_access(flash, true, false, false, false)) |
| 2512 | return 1; |
| 2513 | |
| 2514 | const int ret = read_flash_to_file(flash, filename); |
| 2515 | |
| 2516 | finalize_flash_access(flash); |
| 2517 | |
| 2518 | return ret; |
| 2519 | } |
| 2520 | |
| 2521 | int do_erase(struct flashctx *const flash) |
| 2522 | { |
| 2523 | const int ret = flashrom_flash_erase(flash); |
| 2524 | |
| 2525 | /* |
| 2526 | * FIXME: Do we really want the scary warning if erase failed? |
| 2527 | * After all, after erase the chip is either blank or partially |
| 2528 | * blank or it has the old contents. A blank chip won't boot, |
| 2529 | * so if the user wanted erase and reboots afterwards, the user |
| 2530 | * knows very well that booting won't work. |
| 2531 | */ |
| 2532 | if (ret) |
| 2533 | emergency_help_message(); |
| 2534 | |
| 2535 | return ret; |
| 2536 | } |
| 2537 | |
| 2538 | int do_write(struct flashctx *const flash, const char *const filename) |
| 2539 | { |
| 2540 | const size_t flash_size = flash->chip->total_size * 1024; |
| 2541 | int ret = 1; |
| 2542 | |
| 2543 | uint8_t *const newcontents = malloc(flash_size); |
| 2544 | if (!newcontents) { |
| 2545 | msg_gerr("Out of memory!\n"); |
| 2546 | goto _free_ret; |
| 2547 | } |
| 2548 | |
| 2549 | if (read_buf_from_file(newcontents, flash_size, filename)) |
| 2550 | goto _free_ret; |
| 2551 | |
| 2552 | ret = flashrom_image_write(flash, newcontents, flash_size); |
| 2553 | |
| 2554 | _free_ret: |
| 2555 | free(newcontents); |
| 2556 | return ret; |
| 2557 | } |
| 2558 | |
| 2559 | int do_verify(struct flashctx *const flash, const char *const filename) |
| 2560 | { |
| 2561 | const size_t flash_size = flash->chip->total_size * 1024; |
| 2562 | int ret = 1; |
| 2563 | |
| 2564 | uint8_t *const newcontents = malloc(flash_size); |
| 2565 | if (!newcontents) { |
| 2566 | msg_gerr("Out of memory!\n"); |
| 2567 | goto _free_ret; |
| 2568 | } |
| 2569 | |
| 2570 | if (read_buf_from_file(newcontents, flash_size, filename)) |
| 2571 | goto _free_ret; |
| 2572 | |
| 2573 | ret = flashrom_image_verify(flash, newcontents, flash_size); |
| 2574 | |
| 2575 | _free_ret: |
| 2576 | free(newcontents); |
| 2577 | return ret; |
| 2578 | } |