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