blob: bba7a3a536e9833c8efd93a99a71b511968cc9fb [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
rminnich8d3ff912003-10-25 17:01:29 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
uwe4475e902009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
hailfinger23060112009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
rminnich8d3ff912003-10-25 17:01:29 +00008 *
uweb25f1ea2007-08-29 17:52:32 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
rminnich8d3ff912003-10-25 17:01:29 +000013 *
uweb25f1ea2007-08-29 17:52:32 +000014 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
rminnich8d3ff912003-10-25 17:01:29 +000018 *
uweb25f1ea2007-08-29 17:52:32 +000019 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
rminnich8d3ff912003-10-25 17:01:29 +000022 */
23
rminnich8d3ff912003-10-25 17:01:29 +000024#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000025#include <sys/types.h>
26#include <sys/stat.h>
rminnich8d3ff912003-10-25 17:01:29 +000027#include <string.h>
28#include <stdlib.h>
ollie6a600992005-11-26 21:55:36 +000029#include <getopt.h>
rminnich8d3ff912003-10-25 17:01:29 +000030#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000031#include "flashchips.h"
rminnich8d3ff912003-10-25 17:01:29 +000032
hailfinger2d83b5b2009-07-22 20:13:00 +000033const char *flashrom_version = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000034char *chip_to_probe = NULL;
stuge98c09aa2008-06-18 02:08:40 +000035int verbose = 0;
hailfinger80422e22009-12-13 22:28:00 +000036
37#if INTERNAL_SUPPORT == 1
hailfinger6fe23d62009-08-12 11:39:29 +000038enum programmer programmer = PROGRAMMER_INTERNAL;
hailfinger80422e22009-12-13 22:28:00 +000039#elif DUMMY_SUPPORT == 1
40enum programmer programmer = PROGRAMMER_DUMMY;
41#else
hailfingerf95c8f62010-01-10 13:28:48 +000042/* If neither internal nor dummy are selected, we must pick a sensible default.
43 * Since there is no reason to prefer a particular external programmer, we fail
44 * if more than one of them is selected. If only one is selected, it is clear
45 * that the user wants that one to become the default.
46 */
47#if NIC3COM_SUPPORT+GFXNVIDIA_SUPPORT+DRKAISER_SUPPORT+SATASII_SUPPORT+FT2232_SPI_SUPPORT+SERPROG_SUPPORT+BUSPIRATE_SPI_SUPPORT > 1
48#error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all external programmers except one.
49#endif
50enum programmer programmer =
51#if NIC3COM_SUPPORT == 1
52 PROGRAMMER_NIC3COM
53#endif
54#if GFXNVIDIA_SUPPORT == 1
55 PROGRAMMER_GFXNVIDIA
56#endif
57#if DRKAISER_SUPPORT == 1
58 PROGRAMMER_DRKAISER
59#endif
60#if SATASII_SUPPORT == 1
61 PROGRAMMER_SATASII
62#endif
63#if FT2232_SPI_SUPPORT == 1
64 PROGRAMMER_FT2232SPI
65#endif
66#if SERPROG_SUPPORT == 1
67 PROGRAMMER_SERPROG
68#endif
69#if BUSPIRATE_SPI_SUPPORT == 1
70 PROGRAMMER_BUSPIRATESPI
71#endif
72;
hailfinger80422e22009-12-13 22:28:00 +000073#endif
74
hailfinger4f45a4f2009-08-12 13:32:56 +000075char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000076
hailfinger80422e22009-12-13 22:28:00 +000077/**
78 * flashrom defaults to Parallel/LPC/FWH flash devices. If a known host
79 * controller is found, the init routine sets the buses_supported bitfield to
80 * contain the supported buses for that controller.
81 */
82enum chipbustype buses_supported = CHIP_BUSTYPE_NONSPI;
83
84/**
85 * Programmers supporting multiple buses can have differing size limits on
86 * each bus. Store the limits for each bus in a common struct.
87 */
88struct decode_sizes max_rom_decode = {
89 .parallel = 0xffffffff,
90 .lpc = 0xffffffff,
91 .fwh = 0xffffffff,
92 .spi = 0xffffffff
93};
94
hailfingerabe249e2009-05-08 17:43:22 +000095const struct programmer_entry programmer_table[] = {
hailfinger80422e22009-12-13 22:28:00 +000096#if INTERNAL_SUPPORT == 1
hailfingerabe249e2009-05-08 17:43:22 +000097 {
hailfinger3548a9a2009-08-12 14:34:35 +000098 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000099 .init = internal_init,
100 .shutdown = internal_shutdown,
hailfinger11ae3c42009-05-11 14:13:25 +0000101 .map_flash_region = physmap,
102 .unmap_flash_region = physunmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000103 .chip_readb = internal_chip_readb,
104 .chip_readw = internal_chip_readw,
105 .chip_readl = internal_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000106 .chip_readn = internal_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000107 .chip_writeb = internal_chip_writeb,
108 .chip_writew = internal_chip_writew,
109 .chip_writel = internal_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000110 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000111 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +0000112 },
hailfinger80422e22009-12-13 22:28:00 +0000113#endif
stepan927d4e22007-04-04 22:45:58 +0000114
hailfinger571a6b32009-09-16 10:09:21 +0000115#if DUMMY_SUPPORT == 1
hailfingera9df33c2009-05-09 00:54:55 +0000116 {
hailfinger3548a9a2009-08-12 14:34:35 +0000117 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000118 .init = dummy_init,
119 .shutdown = dummy_shutdown,
hailfinger11ae3c42009-05-11 14:13:25 +0000120 .map_flash_region = dummy_map,
121 .unmap_flash_region = dummy_unmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000122 .chip_readb = dummy_chip_readb,
123 .chip_readw = dummy_chip_readw,
124 .chip_readl = dummy_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000125 .chip_readn = dummy_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000126 .chip_writeb = dummy_chip_writeb,
127 .chip_writew = dummy_chip_writew,
128 .chip_writel = dummy_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000129 .chip_writen = dummy_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000130 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000131 },
hailfinger571a6b32009-09-16 10:09:21 +0000132#endif
hailfingera9df33c2009-05-09 00:54:55 +0000133
hailfinger571a6b32009-09-16 10:09:21 +0000134#if NIC3COM_SUPPORT == 1
uwe0f5a3a22009-05-13 11:36:06 +0000135 {
hailfinger3548a9a2009-08-12 14:34:35 +0000136 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000137 .init = nic3com_init,
138 .shutdown = nic3com_shutdown,
uwe3e656bd2009-05-17 23:12:17 +0000139 .map_flash_region = fallback_map,
140 .unmap_flash_region = fallback_unmap,
uwe0f5a3a22009-05-13 11:36:06 +0000141 .chip_readb = nic3com_chip_readb,
hailfinger43716942009-05-16 01:23:55 +0000142 .chip_readw = fallback_chip_readw,
143 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000144 .chip_readn = fallback_chip_readn,
uwe0f5a3a22009-05-13 11:36:06 +0000145 .chip_writeb = nic3com_chip_writeb,
hailfinger43716942009-05-16 01:23:55 +0000146 .chip_writew = fallback_chip_writew,
147 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000148 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000149 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000150 },
hailfinger571a6b32009-09-16 10:09:21 +0000151#endif
uwe0f5a3a22009-05-13 11:36:06 +0000152
uweff4576d2009-09-30 18:29:55 +0000153#if GFXNVIDIA_SUPPORT == 1
154 {
155 .name = "gfxnvidia",
156 .init = gfxnvidia_init,
157 .shutdown = gfxnvidia_shutdown,
158 .map_flash_region = fallback_map,
159 .unmap_flash_region = fallback_unmap,
160 .chip_readb = gfxnvidia_chip_readb,
161 .chip_readw = fallback_chip_readw,
162 .chip_readl = fallback_chip_readl,
163 .chip_readn = fallback_chip_readn,
164 .chip_writeb = gfxnvidia_chip_writeb,
165 .chip_writew = fallback_chip_writew,
166 .chip_writel = fallback_chip_writel,
167 .chip_writen = fallback_chip_writen,
168 .delay = internal_delay,
169 },
170#endif
171
hailfinger571a6b32009-09-16 10:09:21 +0000172#if DRKAISER_SUPPORT == 1
ruikda922a12009-05-17 19:39:27 +0000173 {
uwee2f95ef2009-09-02 23:00:46 +0000174 .name = "drkaiser",
175 .init = drkaiser_init,
176 .shutdown = drkaiser_shutdown,
177 .map_flash_region = fallback_map,
178 .unmap_flash_region = fallback_unmap,
179 .chip_readb = drkaiser_chip_readb,
180 .chip_readw = fallback_chip_readw,
181 .chip_readl = fallback_chip_readl,
182 .chip_readn = fallback_chip_readn,
183 .chip_writeb = drkaiser_chip_writeb,
184 .chip_writew = fallback_chip_writew,
185 .chip_writel = fallback_chip_writel,
186 .chip_writen = fallback_chip_writen,
187 .delay = internal_delay,
188 },
hailfinger571a6b32009-09-16 10:09:21 +0000189#endif
uwee2f95ef2009-09-02 23:00:46 +0000190
hailfinger571a6b32009-09-16 10:09:21 +0000191#if SATASII_SUPPORT == 1
uwee2f95ef2009-09-02 23:00:46 +0000192 {
hailfinger3548a9a2009-08-12 14:34:35 +0000193 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000194 .init = satasii_init,
195 .shutdown = satasii_shutdown,
uwe3e656bd2009-05-17 23:12:17 +0000196 .map_flash_region = fallback_map,
197 .unmap_flash_region = fallback_unmap,
ruikda922a12009-05-17 19:39:27 +0000198 .chip_readb = satasii_chip_readb,
199 .chip_readw = fallback_chip_readw,
200 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000201 .chip_readn = fallback_chip_readn,
ruikda922a12009-05-17 19:39:27 +0000202 .chip_writeb = satasii_chip_writeb,
203 .chip_writew = fallback_chip_writew,
204 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000205 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000206 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000207 },
hailfinger571a6b32009-09-16 10:09:21 +0000208#endif
ruikda922a12009-05-17 19:39:27 +0000209
hailfinger80422e22009-12-13 22:28:00 +0000210#if INTERNAL_SUPPORT == 1
hailfinger26e212b2009-05-31 18:00:57 +0000211 {
hailfinger3548a9a2009-08-12 14:34:35 +0000212 .name = "it87spi",
hailfinger26e212b2009-05-31 18:00:57 +0000213 .init = it87spi_init,
hailfinger571a6b32009-09-16 10:09:21 +0000214 .shutdown = noop_shutdown,
hailfinger6fe23d62009-08-12 11:39:29 +0000215 .map_flash_region = fallback_map,
216 .unmap_flash_region = fallback_unmap,
hailfinger571a6b32009-09-16 10:09:21 +0000217 .chip_readb = noop_chip_readb,
hailfinger9d987ef2009-06-05 18:32:07 +0000218 .chip_readw = fallback_chip_readw,
219 .chip_readl = fallback_chip_readl,
220 .chip_readn = fallback_chip_readn,
hailfinger571a6b32009-09-16 10:09:21 +0000221 .chip_writeb = noop_chip_writeb,
hailfinger9d987ef2009-06-05 18:32:07 +0000222 .chip_writew = fallback_chip_writew,
223 .chip_writel = fallback_chip_writel,
224 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000225 .delay = internal_delay,
hailfinger26e212b2009-05-31 18:00:57 +0000226 },
hailfinger80422e22009-12-13 22:28:00 +0000227#endif
hailfinger26e212b2009-05-31 18:00:57 +0000228
hailfingerd9dcfbd2009-08-19 13:27:58 +0000229#if FT2232_SPI_SUPPORT == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000230 {
hailfinger3548a9a2009-08-12 14:34:35 +0000231 .name = "ft2232spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000232 .init = ft2232_spi_init,
hailfinger571a6b32009-09-16 10:09:21 +0000233 .shutdown = noop_shutdown, /* Missing shutdown */
hailfinger6fe23d62009-08-12 11:39:29 +0000234 .map_flash_region = fallback_map,
235 .unmap_flash_region = fallback_unmap,
hailfinger571a6b32009-09-16 10:09:21 +0000236 .chip_readb = noop_chip_readb,
hailfingerf31da3d2009-06-16 21:08:06 +0000237 .chip_readw = fallback_chip_readw,
238 .chip_readl = fallback_chip_readl,
239 .chip_readn = fallback_chip_readn,
hailfinger571a6b32009-09-16 10:09:21 +0000240 .chip_writeb = noop_chip_writeb,
hailfingerf31da3d2009-06-16 21:08:06 +0000241 .chip_writew = fallback_chip_writew,
242 .chip_writel = fallback_chip_writel,
243 .chip_writen = fallback_chip_writen,
244 .delay = internal_delay,
245 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000246#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000247
hailfinger74d88a72009-08-12 16:17:41 +0000248#if SERPROG_SUPPORT == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000249 {
hailfinger3548a9a2009-08-12 14:34:35 +0000250 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000251 .init = serprog_init,
252 .shutdown = serprog_shutdown,
253 .map_flash_region = fallback_map,
254 .unmap_flash_region = fallback_unmap,
255 .chip_readb = serprog_chip_readb,
256 .chip_readw = fallback_chip_readw,
257 .chip_readl = fallback_chip_readl,
258 .chip_readn = serprog_chip_readn,
259 .chip_writeb = serprog_chip_writeb,
260 .chip_writew = fallback_chip_writew,
261 .chip_writel = fallback_chip_writel,
262 .chip_writen = fallback_chip_writen,
263 .delay = serprog_delay,
264 },
hailfinger74d88a72009-08-12 16:17:41 +0000265#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000266
hailfinger9c5add72009-11-24 00:20:03 +0000267#if BUSPIRATE_SPI_SUPPORT == 1
268 {
269 .name = "buspiratespi",
270 .init = buspirate_spi_init,
271 .shutdown = buspirate_spi_shutdown,
272 .map_flash_region = fallback_map,
273 .unmap_flash_region = fallback_unmap,
274 .chip_readb = noop_chip_readb,
275 .chip_readw = fallback_chip_readw,
276 .chip_readl = fallback_chip_readl,
277 .chip_readn = fallback_chip_readn,
278 .chip_writeb = noop_chip_writeb,
279 .chip_writew = fallback_chip_writew,
280 .chip_writel = fallback_chip_writel,
281 .chip_writen = fallback_chip_writen,
282 .delay = internal_delay,
283 },
284#endif
285
hailfinger3548a9a2009-08-12 14:34:35 +0000286 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000287};
stepan927d4e22007-04-04 22:45:58 +0000288
uweabe92a52009-05-16 22:36:00 +0000289int programmer_init(void)
290{
291 return programmer_table[programmer].init();
292}
293
294int programmer_shutdown(void)
295{
296 return programmer_table[programmer].shutdown();
297}
298
299void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
300 size_t len)
301{
302 return programmer_table[programmer].map_flash_region(descr,
303 phys_addr, len);
304}
305
306void programmer_unmap_flash_region(void *virt_addr, size_t len)
307{
308 programmer_table[programmer].unmap_flash_region(virt_addr, len);
309}
310
311void chip_writeb(uint8_t val, chipaddr addr)
312{
313 programmer_table[programmer].chip_writeb(val, addr);
314}
315
316void chip_writew(uint16_t val, chipaddr addr)
317{
318 programmer_table[programmer].chip_writew(val, addr);
319}
320
321void chip_writel(uint32_t val, chipaddr addr)
322{
323 programmer_table[programmer].chip_writel(val, addr);
324}
325
hailfinger9d987ef2009-06-05 18:32:07 +0000326void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
327{
328 programmer_table[programmer].chip_writen(buf, addr, len);
329}
330
uweabe92a52009-05-16 22:36:00 +0000331uint8_t chip_readb(const chipaddr addr)
332{
333 return programmer_table[programmer].chip_readb(addr);
334}
335
336uint16_t chip_readw(const chipaddr addr)
337{
338 return programmer_table[programmer].chip_readw(addr);
339}
340
341uint32_t chip_readl(const chipaddr addr)
342{
343 return programmer_table[programmer].chip_readl(addr);
344}
345
hailfinger9d987ef2009-06-05 18:32:07 +0000346void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
347{
348 programmer_table[programmer].chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000349}
350
hailfingere5829f62009-06-05 17:48:08 +0000351void programmer_delay(int usecs)
352{
353 programmer_table[programmer].delay(usecs);
354}
355
stuge5ff0e6c2009-01-26 00:39:57 +0000356void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000357{
stepan15e64bc2007-05-24 08:48:10 +0000358 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000359 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000360 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfinger82719632009-05-16 21:22:56 +0000361 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000362}
363
hailfinger0f08b7a2009-06-16 08:55:44 +0000364int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfinger23060112009-05-08 12:49:03 +0000365{
hailfinger0f08b7a2009-06-16 08:55:44 +0000366 chip_readn(buf, flash->virtual_memory + start, len);
hailfinger23060112009-05-08 12:49:03 +0000367
368 return 0;
369}
370
hailfinger80422e22009-12-13 22:28:00 +0000371unsigned long flashbase = 0;
372
hailfinger7b414742009-06-13 12:04:03 +0000373int min(int a, int b)
374{
375 return (a < b) ? a : b;
376}
377
hailfinger7af83692009-06-15 17:23:36 +0000378int max(int a, int b)
379{
380 return (a > b) ? a : b;
381}
382
hailfingeraec9c962009-10-31 01:53:09 +0000383int bitcount(unsigned long a)
384{
385 int i = 0;
386 for (; a != 0; a >>= 1)
387 if (a & 1)
388 i++;
389 return i;
390}
391
hailfingera916b422009-06-01 02:08:58 +0000392char *strcat_realloc(char *dest, const char *src)
393{
394 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
395 if (!dest)
396 return NULL;
397 strcat(dest, src);
398 return dest;
399}
400
hailfinger6e5a52a2009-11-24 18:27:10 +0000401/* This is a somewhat hacked function similar in some ways to strtok().
402 * It will look for needle in haystack, return a copy of needle and remove
403 * everything from the first occurrence of needle to the next delimiter
404 * from haystack.
405 */
406char *extract_param(char **haystack, char *needle, char *delim)
407{
408 char *param_pos, *rest, *tmp;
409 char *dev = NULL;
410 int devlen;
411
412 param_pos = strstr(*haystack, needle);
413 do {
414 if (!param_pos)
415 return NULL;
416 /* Beginning of the string? */
417 if (param_pos == *haystack)
418 break;
419 /* After a delimiter? */
420 if (strchr(delim, *(param_pos - 1)))
421 break;
422 /* Continue searching. */
423 param_pos++;
424 param_pos = strstr(param_pos, needle);
425 } while (1);
426
427 if (param_pos) {
428 param_pos += strlen(needle);
429 devlen = strcspn(param_pos, delim);
430 if (devlen) {
431 dev = malloc(devlen + 1);
432 if (!dev) {
433 fprintf(stderr, "Out of memory!\n");
434 exit(1);
435 }
436 strncpy(dev, param_pos, devlen);
437 dev[devlen] = '\0';
438 }
439 rest = param_pos + devlen;
440 rest += strspn(rest, delim);
441 param_pos -= strlen(needle);
442 memmove(param_pos, rest, strlen(rest) + 1);
443 tmp = realloc(*haystack, strlen(*haystack) + 1);
444 if (!tmp) {
445 fprintf(stderr, "Out of memory!\n");
446 exit(1);
447 }
448 *haystack = tmp;
449 }
450
451
452 return dev;
453}
454
hailfinger7af83692009-06-15 17:23:36 +0000455/* start is an offset to the base address of the flash chip */
456int check_erased_range(struct flashchip *flash, int start, int len)
457{
458 int ret;
459 uint8_t *cmpbuf = malloc(len);
460
461 if (!cmpbuf) {
462 fprintf(stderr, "Could not allocate memory!\n");
463 exit(1);
464 }
465 memset(cmpbuf, 0xff, len);
466 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
467 free(cmpbuf);
468 return ret;
469}
470
471/**
hailfinger7af3d192009-11-25 17:05:52 +0000472 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
473 flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000474 * @start offset to the base address of the flash chip
475 * @len length of the verified area
476 * @message string to print in the "FAILED" message
477 * @return 0 for success, -1 for failure
478 */
479int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
480{
481 int i, j, starthere, lenhere, ret = 0;
hailfinger7af83692009-06-15 17:23:36 +0000482 int page_size = flash->page_size;
483 uint8_t *readbuf = malloc(page_size);
hailfinger5be6c0f2009-07-23 01:42:56 +0000484 int failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000485
486 if (!len)
487 goto out_free;
488
hailfingerb0f4d122009-06-24 08:20:45 +0000489 if (!flash->read) {
490 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
491 return 1;
492 }
hailfinger7af83692009-06-15 17:23:36 +0000493 if (!readbuf) {
494 fprintf(stderr, "Could not allocate memory!\n");
495 exit(1);
496 }
497
498 if (start + len > flash->total_size * 1024) {
499 fprintf(stderr, "Error: %s called with start 0x%x + len 0x%x >"
500 " total_size 0x%x\n", __func__, start, len,
501 flash->total_size * 1024);
502 ret = -1;
503 goto out_free;
504 }
505 if (!message)
506 message = "VERIFY";
507
508 /* Warning: This loop has a very unusual condition and body.
509 * The loop needs to go through each page with at least one affected
510 * byte. The lowest page number is (start / page_size) since that
511 * division rounds down. The highest page number we want is the page
512 * where the last byte of the range lives. That last byte has the
513 * address (start + len - 1), thus the highest page number is
514 * (start + len - 1) / page_size. Since we want to include that last
515 * page as well, the loop condition uses <=.
516 */
517 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
518 /* Byte position of the first byte in the range in this page. */
519 starthere = max(start, i * page_size);
520 /* Length of bytes in the range in this page. */
521 lenhere = min(start + len, (i + 1) * page_size) - starthere;
hailfingerb0f4d122009-06-24 08:20:45 +0000522 flash->read(flash, readbuf, starthere, lenhere);
hailfinger7af83692009-06-15 17:23:36 +0000523 for (j = 0; j < lenhere; j++) {
524 if (cmpbuf[starthere - start + j] != readbuf[j]) {
hailfinger5be6c0f2009-07-23 01:42:56 +0000525 /* Only print the first failure. */
526 if (!failcount++)
527 fprintf(stderr, "%s FAILED at 0x%08x! "
528 "Expected=0x%02x, Read=0x%02x,",
529 message, starthere + j,
530 cmpbuf[starthere - start + j],
531 readbuf[j]);
hailfinger7af83692009-06-15 17:23:36 +0000532 }
533 }
534 }
hailfinger5be6c0f2009-07-23 01:42:56 +0000535 if (failcount) {
536 fprintf(stderr, " failed byte count from 0x%08x-0x%08x: 0x%x\n",
537 start, start + len - 1, failcount);
538 ret = -1;
539 }
hailfinger7af83692009-06-15 17:23:36 +0000540
541out_free:
542 free(readbuf);
543 return ret;
544}
545
hailfinger0c515352009-11-23 12:55:31 +0000546/* This function generates various test patterns useful for testing controller
547 * and chip communication as well as chip behaviour.
548 *
549 * If a byte can be written multiple times, each time keeping 0-bits at 0
550 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
551 * is essentially an AND operation. That's also the reason why this function
552 * provides the result of AND between various patterns.
553 *
554 * Below is a list of patterns (and their block length).
555 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
556 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
557 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
558 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
559 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
560 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
561 * Pattern 6 is 00 (1 Byte)
562 * Pattern 7 is ff (1 Byte)
563 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
564 * byte block.
565 *
566 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
567 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
568 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
569 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
570 * Pattern 12 is 00 (1 Byte)
571 * Pattern 13 is ff (1 Byte)
572 * Patterns 8-13 have no block number.
573 *
574 * Patterns 0-3 are created to detect and efficiently diagnose communication
575 * slips like missed bits or bytes and their repetitive nature gives good visual
576 * cues to the person inspecting the results. In addition, the following holds:
577 * AND Pattern 0/1 == Pattern 4
578 * AND Pattern 2/3 == Pattern 5
579 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
580 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
581 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
582 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
583 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
584 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
585 * Besides that, they provide for bit testing of the last two bytes of every
586 * 256 byte block which contains the block number for patterns 0-6.
587 * Patterns 10-11 are special purpose for detecting subblock aliasing with
588 * block sizes >256 bytes (some Dataflash chips etc.)
589 * AND Pattern 8/9 == Pattern 12
590 * AND Pattern 10/11 == Pattern 12
591 * Pattern 13 is the completely erased state.
592 * None of the patterns can detect aliasing at boundaries which are a multiple
593 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
594 */
595int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
596{
597 int i;
598
599 if (!buf) {
600 fprintf(stderr, "Invalid buffer!\n");
601 return 1;
602 }
603
604 switch (variant) {
605 case 0:
606 for (i = 0; i < size; i++)
607 buf[i] = (i & 0xf) << 4 | 0x5;
608 break;
609 case 1:
610 for (i = 0; i < size; i++)
611 buf[i] = (i & 0xf) << 4 | 0xa;
612 break;
613 case 2:
614 for (i = 0; i < size; i++)
615 buf[i] = 0x50 | (i & 0xf);
616 break;
617 case 3:
618 for (i = 0; i < size; i++)
619 buf[i] = 0xa0 | (i & 0xf);
620 break;
621 case 4:
622 for (i = 0; i < size; i++)
623 buf[i] = (i & 0xf) << 4;
624 break;
625 case 5:
626 for (i = 0; i < size; i++)
627 buf[i] = i & 0xf;
628 break;
629 case 6:
630 memset(buf, 0x00, size);
631 break;
632 case 7:
633 memset(buf, 0xff, size);
634 break;
635 case 8:
636 for (i = 0; i < size; i++)
637 buf[i] = i & 0xff;
638 break;
639 case 9:
640 for (i = 0; i < size; i++)
641 buf[i] = ~(i & 0xff);
642 break;
643 case 10:
644 for (i = 0; i < size % 2; i++) {
645 buf[i * 2] = (i >> 8) & 0xff;
646 buf[i * 2 + 1] = i & 0xff;
647 }
648 if (size & 0x1)
649 buf[i * 2] = (i >> 8) & 0xff;
650 break;
651 case 11:
652 for (i = 0; i < size % 2; i++) {
653 buf[i * 2] = ~((i >> 8) & 0xff);
654 buf[i * 2 + 1] = ~(i & 0xff);
655 }
656 if (size & 0x1)
657 buf[i * 2] = ~((i >> 8) & 0xff);
658 break;
659 case 12:
660 memset(buf, 0x00, size);
661 break;
662 case 13:
663 memset(buf, 0xff, size);
664 break;
665 }
666
667 if ((variant >= 0) && (variant <= 7)) {
668 /* Write block number in the last two bytes of each 256-byte
669 * block, big endian for easier reading of the hexdump.
670 * Note that this wraps around for chips larger than 2^24 bytes
671 * (16 MB).
672 */
673 for (i = 0; i < size / 256; i++) {
674 buf[i * 256 + 254] = (i >> 8) & 0xff;
675 buf[i * 256 + 255] = i & 0xff;
676 }
677 }
678
679 return 0;
680}
681
hailfingeraec9c962009-10-31 01:53:09 +0000682int check_max_decode(enum chipbustype buses, uint32_t size)
683{
684 int limitexceeded = 0;
685 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
686 (max_rom_decode.parallel < size)) {
687 limitexceeded++;
688 printf_debug("Chip size %u kB is bigger than supported "
689 "size %u kB of chipset/board/programmer "
690 "for %s interface, "
691 "probe/read/erase/write may fail. ", size / 1024,
692 max_rom_decode.parallel / 1024, "Parallel");
693 }
694 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
695 limitexceeded++;
696 printf_debug("Chip size %u kB is bigger than supported "
697 "size %u kB of chipset/board/programmer "
698 "for %s interface, "
699 "probe/read/erase/write may fail. ", size / 1024,
700 max_rom_decode.lpc / 1024, "LPC");
701 }
702 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
703 limitexceeded++;
704 printf_debug("Chip size %u kB is bigger than supported "
705 "size %u kB of chipset/board/programmer "
706 "for %s interface, "
707 "probe/read/erase/write may fail. ", size / 1024,
708 max_rom_decode.fwh / 1024, "FWH");
709 }
710 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
711 limitexceeded++;
712 printf_debug("Chip size %u kB is bigger than supported "
713 "size %u kB of chipset/board/programmer "
714 "for %s interface, "
715 "probe/read/erase/write may fail. ", size / 1024,
716 max_rom_decode.spi / 1024, "SPI");
717 }
718 if (!limitexceeded)
719 return 0;
720 /* Sometimes chip and programmer have more than one bus in common,
721 * and the limit is not exceeded on all buses. Tell the user.
722 */
723 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +0000724 /* FIXME: This message is designed towards CLI users. */
hailfingeraec9c962009-10-31 01:53:09 +0000725 printf_debug("There is at least one common chip/programmer "
726 "interface which can support a chip of this size. "
727 "You can try --force at your own risk.\n");
728 return 1;
729}
730
stuge56300c32008-09-03 23:10:05 +0000731struct flashchip *probe_flash(struct flashchip *first_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +0000732{
stuge56300c32008-09-03 23:10:05 +0000733 struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +0000734 unsigned long base = 0;
735 uint32_t size;
736 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +0000737 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +0000738
stuge56300c32008-09-03 23:10:05 +0000739 for (flash = first_flash; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +0000740 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +0000741 continue;
stepanb8361b92008-03-17 22:59:40 +0000742 printf_debug("Probing for %s %s, %d KB: ",
743 flash->vendor, flash->name, flash->total_size);
stuge98c09aa2008-06-18 02:08:40 +0000744 if (!flash->probe && !force) {
stuge8ce3a3c2008-04-28 14:47:30 +0000745 printf_debug("failed! flashrom has no probe function for this flash chip.\n");
stuge8ce3a3c2008-04-28 14:47:30 +0000746 continue;
747 }
hailfingeraec9c962009-10-31 01:53:09 +0000748 buses_common = buses_supported & flash->bustype;
749 if (!buses_common) {
hailfingera916b422009-06-01 02:08:58 +0000750 tmp = flashbuses_to_text(buses_supported);
751 printf_debug("skipped. Host bus type %s ", tmp);
752 free(tmp);
753 tmp = flashbuses_to_text(flash->bustype);
754 printf_debug("and chip bus type %s are incompatible.\n", tmp);
755 free(tmp);
756 continue;
757 }
stepan782fb172007-04-06 11:58:03 +0000758
ollie5672ac62004-03-17 22:22:08 +0000759 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +0000760 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +0000761
hailfinger72d3b982009-05-09 07:27:23 +0000762 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger82719632009-05-16 21:22:56 +0000763 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +0000764
stugec1e55fe2008-07-02 17:15:47 +0000765 if (force)
766 break;
stepanc98b80b2006-03-16 16:57:41 +0000767
stuge56300c32008-09-03 23:10:05 +0000768 if (flash->probe(flash) != 1)
769 goto notfound;
770
uwefa98ca12008-10-18 21:14:13 +0000771 if (first_flash == flashchips
772 || flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +0000773 break;
774
stuge56300c32008-09-03 23:10:05 +0000775notfound:
hailfinger82719632009-05-16 21:22:56 +0000776 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +0000777 }
uwebe4477b2007-08-23 16:08:21 +0000778
stugec1e55fe2008-07-02 17:15:47 +0000779 if (!flash || !flash->name)
780 return NULL;
781
uwe9e6811e2009-06-28 21:47:57 +0000782 printf("Found chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
783 flash->vendor, flash->name, flash->total_size,
784 flashbuses_to_text(flash->bustype), base);
785
stugec1e55fe2008-07-02 17:15:47 +0000786 return flash;
rminnich8d3ff912003-10-25 17:01:29 +0000787}
788
stepan193c9c22005-12-18 16:41:10 +0000789int verify_flash(struct flashchip *flash, uint8_t *buf)
rminnich8d3ff912003-10-25 17:01:29 +0000790{
hailfingerb0f4d122009-06-24 08:20:45 +0000791 int ret;
ollie5b621572004-03-20 16:46:10 +0000792 int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +0000793
uwefd2d0fe2007-10-17 23:55:15 +0000794 printf("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +0000795
hailfingerb0f4d122009-06-24 08:20:45 +0000796 ret = verify_range(flash, buf, 0, total_size, NULL);
uwef6641642007-05-09 10:17:44 +0000797
hailfingerb0f4d122009-06-24 08:20:45 +0000798 if (!ret)
799 printf("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +0000800
hailfingerb0f4d122009-06-24 08:20:45 +0000801 return ret;
rminnich8d3ff912003-10-25 17:01:29 +0000802}
803
hailfinger09bd6f02009-06-19 11:23:57 +0000804int read_flash(struct flashchip *flash, char *filename)
hailfingerd219a232009-01-28 00:27:54 +0000805{
806 unsigned long numbytes;
807 FILE *image;
808 unsigned long size = flash->total_size * 1024;
809 unsigned char *buf = calloc(size, sizeof(char));
hailfingerde345862009-06-01 22:07:52 +0000810
811 if (!filename) {
812 printf("Error: No filename specified.\n");
813 return 1;
814 }
hailfingerd219a232009-01-28 00:27:54 +0000815 if ((image = fopen(filename, "w")) == NULL) {
816 perror(filename);
817 exit(1);
818 }
819 printf("Reading flash... ");
hailfinger23060112009-05-08 12:49:03 +0000820 if (!flash->read) {
821 printf("FAILED!\n");
822 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
823 return 1;
824 } else
hailfinger0f08b7a2009-06-16 08:55:44 +0000825 flash->read(flash, buf, 0, size);
hailfingerd219a232009-01-28 00:27:54 +0000826
hailfingerd219a232009-01-28 00:27:54 +0000827 numbytes = fwrite(buf, 1, size, image);
828 fclose(image);
hailfingera2615252009-06-01 21:37:00 +0000829 free(buf);
hailfingerd219a232009-01-28 00:27:54 +0000830 printf("%s.\n", numbytes == size ? "done" : "FAILED");
831 if (numbytes != size)
832 return 1;
833 return 0;
834}
835
hailfinger9fed35d2010-01-19 06:42:46 +0000836/* This function shares a lot of its structure with erase_flash().
837 * Even if an error is found, the function will keep going and check the rest.
838 */
hailfinger45177872010-01-18 08:14:43 +0000839int selfcheck_eraseblocks(struct flashchip *flash)
840{
hailfinger9fed35d2010-01-19 06:42:46 +0000841 int i, j, k;
842 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +0000843
844 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
845 unsigned int done = 0;
846 struct block_eraser eraser = flash->block_erasers[k];
847
848 for (i = 0; i < NUM_ERASEREGIONS; i++) {
849 /* Blocks with zero size are bugs in flashchips.c. */
850 if (eraser.eraseblocks[i].count &&
851 !eraser.eraseblocks[i].size) {
852 msg_gerr("ERROR: Flash chip %s erase function "
853 "%i region %i has size 0. Please report"
854 " a bug at flashrom@flashrom.org\n",
855 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +0000856 ret = 1;
hailfinger45177872010-01-18 08:14:43 +0000857 }
858 /* Blocks with zero count are bugs in flashchips.c. */
859 if (!eraser.eraseblocks[i].count &&
860 eraser.eraseblocks[i].size) {
861 msg_gerr("ERROR: Flash chip %s erase function "
862 "%i region %i has count 0. Please report"
863 " a bug at flashrom@flashrom.org\n",
864 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +0000865 ret = 1;
hailfinger45177872010-01-18 08:14:43 +0000866 }
867 done += eraser.eraseblocks[i].count *
868 eraser.eraseblocks[i].size;
869 }
hailfinger9fed35d2010-01-19 06:42:46 +0000870 /* Empty eraseblock definition with erase function. */
871 if (!done && eraser.block_erase)
872 msg_pspew("Strange: Empty eraseblock definition with "
873 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +0000874 if (!done)
875 continue;
876 if (done != flash->total_size * 1024) {
877 msg_gerr("ERROR: Flash chip %s erase function %i "
878 "region walking resulted in 0x%06x bytes total,"
879 " expected 0x%06x bytes. Please report a bug at"
880 " flashrom@flashrom.org\n", flash->name, k,
881 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +0000882 ret = 1;
hailfinger45177872010-01-18 08:14:43 +0000883 }
hailfinger9fed35d2010-01-19 06:42:46 +0000884 if (!eraser.block_erase)
885 continue;
886 /* Check if there are identical erase functions for different
887 * layouts. That would imply "magic" erase functions. The
888 * easiest way to check this is with function pointers.
889 */
890 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++)
891 if (eraser.block_erase ==
892 flash->block_erasers[j].block_erase) {
893 msg_gerr("ERROR: Flash chip %s erase function "
894 "%i and %i are identical. Please report"
895 " a bug at flashrom@flashrom.org\n",
896 flash->name, k, j);
897 ret = 1;
898 }
hailfinger45177872010-01-18 08:14:43 +0000899 }
hailfinger9fed35d2010-01-19 06:42:46 +0000900 return ret;
hailfinger45177872010-01-18 08:14:43 +0000901}
902
hailfingerd219a232009-01-28 00:27:54 +0000903int erase_flash(struct flashchip *flash)
904{
hailfinger7df21362009-09-05 02:30:58 +0000905 int i, j, k, ret = 0, found = 0;
hailfingere1af56b2009-12-14 04:04:18 +0000906 unsigned int start, len;
hailfinger7df21362009-09-05 02:30:58 +0000907
hailfingerd219a232009-01-28 00:27:54 +0000908 printf("Erasing flash chip... ");
hailfinger7df21362009-09-05 02:30:58 +0000909 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
hailfingere1af56b2009-12-14 04:04:18 +0000910 unsigned int done = 0;
hailfinger7df21362009-09-05 02:30:58 +0000911 struct block_eraser eraser = flash->block_erasers[k];
912
913 printf_debug("Looking at blockwise erase function %i... ", k);
914 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
915 printf_debug("not defined. "
916 "Looking for another erase function.\n");
917 continue;
918 }
919 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
920 printf_debug("eraseblock layout is known, but no "
921 "matching block erase function found. "
922 "Looking for another erase function.\n");
923 continue;
924 }
925 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
926 printf_debug("block erase function found, but "
927 "eraseblock layout is unknown. "
928 "Looking for another erase function.\n");
929 continue;
930 }
931 found = 1;
932 printf_debug("trying... ");
933 for (i = 0; i < NUM_ERASEREGIONS; i++) {
934 /* count==0 for all automatically initialized array
935 * members so the loop below won't be executed for them.
936 */
937 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingere1af56b2009-12-14 04:04:18 +0000938 start = done + eraser.eraseblocks[i].size * j;
939 len = eraser.eraseblocks[i].size;
940 printf_debug("0x%06x-0x%06x, ", start,
941 start + len - 1);
942 ret = eraser.block_erase(flash, start, len);
hailfinger7df21362009-09-05 02:30:58 +0000943 if (ret)
944 break;
945 }
946 if (ret)
947 break;
hailfingere1af56b2009-12-14 04:04:18 +0000948 done += eraser.eraseblocks[i].count *
949 eraser.eraseblocks[i].size;
hailfinger7df21362009-09-05 02:30:58 +0000950 }
hailfingere1af56b2009-12-14 04:04:18 +0000951 printf_debug("\n");
hailfinger7df21362009-09-05 02:30:58 +0000952 /* If everything is OK, don't try another erase function. */
953 if (!ret)
954 break;
955 }
956 /* If no block erase function was found or block erase failed, retry. */
957 if ((!found || ret) && (flash->erase)) {
958 found = 1;
959 printf_debug("Trying whole-chip erase function... ");
960 ret = flash->erase(flash);
961 }
962 if (!found) {
hailfingerd219a232009-01-28 00:27:54 +0000963 fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n");
964 return 1;
965 }
hailfinger1e9ee0f2009-05-08 17:15:15 +0000966
hailfinger7df21362009-09-05 02:30:58 +0000967 if (ret) {
968 fprintf(stderr, "FAILED!\n");
969 } else {
970 printf("SUCCESS.\n");
971 }
972 return ret;
hailfingerd219a232009-01-28 00:27:54 +0000973}
974
uweb34ec9f2009-10-01 18:40:02 +0000975void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +0000976{
977 fprintf(stderr, "Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +0000978 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
979 "mail flashrom@flashrom.org!\n--------------------"
980 "-----------------------------------------------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +0000981 "DO NOT REBOOT OR POWEROFF!\n");
982}
983
hailfingerc77acb52009-12-24 02:15:55 +0000984/* The way to go if you want a delimited list of programmers*/
985void list_programmers(char *delim)
986{
987 enum programmer p;
988 for (p = 0; p < PROGRAMMER_INVALID; p++) {
989 printf("%s", programmer_table[p].name);
990 if (p < PROGRAMMER_INVALID - 1)
991 printf("%s", delim);
992 }
993 printf("\n");
994}
995
uwefdeca092008-01-21 15:24:22 +0000996void print_version(void)
997{
hailfinger2d83b5b2009-07-22 20:13:00 +0000998 printf("flashrom v%s\n", flashrom_version);
uwefdeca092008-01-21 15:24:22 +0000999}
1000
hailfingerc77acb52009-12-24 02:15:55 +00001001int selfcheck(void)
1002{
hailfinger45177872010-01-18 08:14:43 +00001003 int ret = 0;
1004 struct flashchip *flash;
1005
1006 /* Safety check. Instead of aborting after the first error, check
1007 * if more errors exist.
1008 */
hailfingerc77acb52009-12-24 02:15:55 +00001009 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
1010 fprintf(stderr, "Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001011 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001012 }
1013 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
1014 fprintf(stderr, "SPI programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001015 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001016 }
1017#if BITBANG_SPI_SUPPORT == 1
1018 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
1019 fprintf(stderr, "Bitbanging SPI master table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001020 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001021 }
1022#endif
hailfinger45177872010-01-18 08:14:43 +00001023 for (flash = flashchips; flash && flash->name; flash++)
1024 if (selfcheck_eraseblocks(flash))
1025 ret = 1;
1026 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001027}
1028
1029void check_chip_supported(struct flashchip *flash)
1030{
1031 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
1032 printf("===\n");
1033 if (flash->tested & TEST_BAD_MASK) {
1034 printf("This flash part has status NOT WORKING for operations:");
1035 if (flash->tested & TEST_BAD_PROBE)
1036 printf(" PROBE");
1037 if (flash->tested & TEST_BAD_READ)
1038 printf(" READ");
1039 if (flash->tested & TEST_BAD_ERASE)
1040 printf(" ERASE");
1041 if (flash->tested & TEST_BAD_WRITE)
1042 printf(" WRITE");
1043 printf("\n");
1044 }
1045 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1046 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1047 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1048 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
1049 printf("This flash part has status UNTESTED for operations:");
1050 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
1051 printf(" PROBE");
1052 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
1053 printf(" READ");
1054 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
1055 printf(" ERASE");
1056 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
1057 printf(" WRITE");
1058 printf("\n");
1059 }
hailfinger92cd8e32010-01-07 03:24:05 +00001060 /* FIXME: This message is designed towards CLI users. */
hailfingerc77acb52009-12-24 02:15:55 +00001061 printf("Please email a report to flashrom@flashrom.org if any "
1062 "of the above operations\nwork correctly for you with "
1063 "this flash part. Please include the flashrom\noutput "
1064 "with the additional -V option for all operations you "
1065 "tested (-V, -rV,\n-wV, -EV), and mention which "
1066 "mainboard or programmer you tested. Thanks for your "
1067 "help!\n===\n");
1068 }
1069}
1070
ollie5b621572004-03-20 16:46:10 +00001071int main(int argc, char *argv[])
rminnich8d3ff912003-10-25 17:01:29 +00001072{
hailfinger92cd8e32010-01-07 03:24:05 +00001073 return cli_classic(argc, argv);
hailfingerc77acb52009-12-24 02:15:55 +00001074}
1075
1076/* This function signature is horrible. We need to design a better interface,
1077 * but right now it allows us to split off the CLI code.
1078 */
1079int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1080{
1081 uint8_t *buf;
1082 unsigned long numbytes;
1083 FILE *image;
1084 int ret = 0;
1085 unsigned long size;
1086
1087 size = flash->total_size * 1024;
ollie6a600992005-11-26 21:55:36 +00001088 buf = (uint8_t *) calloc(size, sizeof(char));
uwef6641642007-05-09 10:17:44 +00001089
ollie0eb62d62004-12-08 20:10:01 +00001090 if (erase_it) {
hailfinger0459e1c2009-08-19 13:55:34 +00001091 if (flash->tested & TEST_BAD_ERASE) {
1092 fprintf(stderr, "Erase is not working on this chip. ");
1093 if (!force) {
1094 fprintf(stderr, "Aborting.\n");
uweff4576d2009-09-30 18:29:55 +00001095 programmer_shutdown();
hailfinger0459e1c2009-08-19 13:55:34 +00001096 return 1;
1097 } else {
1098 fprintf(stderr, "Continuing anyway.\n");
1099 }
1100 }
1101 if (erase_flash(flash)) {
1102 emergency_help_message();
uweff4576d2009-09-30 18:29:55 +00001103 programmer_shutdown();
stuge8ce3a3c2008-04-28 14:47:30 +00001104 return 1;
hailfinger0459e1c2009-08-19 13:55:34 +00001105 }
ollie0eb62d62004-12-08 20:10:01 +00001106 } else if (read_it) {
uweff4576d2009-09-30 18:29:55 +00001107 if (read_flash(flash, filename)) {
1108 programmer_shutdown();
stugebbcc2f12009-01-12 21:00:35 +00001109 return 1;
uweff4576d2009-09-30 18:29:55 +00001110 }
ollie5672ac62004-03-17 22:22:08 +00001111 } else {
stepan1da96c02006-11-21 23:48:51 +00001112 struct stat image_stat;
1113
hailfinger0459e1c2009-08-19 13:55:34 +00001114 if (flash->tested & TEST_BAD_ERASE) {
1115 fprintf(stderr, "Erase is not working on this chip "
1116 "and erase is needed for write. ");
1117 if (!force) {
1118 fprintf(stderr, "Aborting.\n");
uweff4576d2009-09-30 18:29:55 +00001119 programmer_shutdown();
hailfinger0459e1c2009-08-19 13:55:34 +00001120 return 1;
1121 } else {
1122 fprintf(stderr, "Continuing anyway.\n");
1123 }
1124 }
1125 if (flash->tested & TEST_BAD_WRITE) {
1126 fprintf(stderr, "Write is not working on this chip. ");
1127 if (!force) {
1128 fprintf(stderr, "Aborting.\n");
uweff4576d2009-09-30 18:29:55 +00001129 programmer_shutdown();
hailfinger0459e1c2009-08-19 13:55:34 +00001130 return 1;
1131 } else {
1132 fprintf(stderr, "Continuing anyway.\n");
1133 }
1134 }
ollie5b621572004-03-20 16:46:10 +00001135 if ((image = fopen(filename, "r")) == NULL) {
ollie5672ac62004-03-17 22:22:08 +00001136 perror(filename);
uweff4576d2009-09-30 18:29:55 +00001137 programmer_shutdown();
ollie5672ac62004-03-17 22:22:08 +00001138 exit(1);
1139 }
stepan1da96c02006-11-21 23:48:51 +00001140 if (fstat(fileno(image), &image_stat) != 0) {
1141 perror(filename);
uweff4576d2009-09-30 18:29:55 +00001142 programmer_shutdown();
stepan1da96c02006-11-21 23:48:51 +00001143 exit(1);
1144 }
uwef6641642007-05-09 10:17:44 +00001145 if (image_stat.st_size != flash->total_size * 1024) {
uwe017911e2008-05-22 22:47:04 +00001146 fprintf(stderr, "Error: Image size doesn't match\n");
uweff4576d2009-09-30 18:29:55 +00001147 programmer_shutdown();
stepan1da96c02006-11-21 23:48:51 +00001148 exit(1);
1149 }
1150
stugebbcc2f12009-01-12 21:00:35 +00001151 numbytes = fread(buf, 1, size, image);
hailfinger80422e22009-12-13 22:28:00 +00001152#if INTERNAL_SUPPORT == 1
stuge98c09aa2008-06-18 02:08:40 +00001153 show_id(buf, size, force);
hailfinger80422e22009-12-13 22:28:00 +00001154#endif
ollie5672ac62004-03-17 22:22:08 +00001155 fclose(image);
stugebbcc2f12009-01-12 21:00:35 +00001156 if (numbytes != size) {
1157 fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
uweff4576d2009-09-30 18:29:55 +00001158 programmer_shutdown();
stugebbcc2f12009-01-12 21:00:35 +00001159 return 1;
1160 }
ollie5672ac62004-03-17 22:22:08 +00001161 }
1162
ollie6a600992005-11-26 21:55:36 +00001163 // This should be moved into each flash part's code to do it
1164 // cleanly. This does the job.
hailfinger051b3442009-08-19 15:19:18 +00001165 handle_romentries(buf, flash);
uwef6641642007-05-09 10:17:44 +00001166
ollie6a600992005-11-26 21:55:36 +00001167 // ////////////////////////////////////////////////////////////
uwef6641642007-05-09 10:17:44 +00001168
stuge8ce3a3c2008-04-28 14:47:30 +00001169 if (write_it) {
hailfinger4e6f1fa2009-06-12 08:04:08 +00001170 printf("Writing flash chip... ");
stuge8ce3a3c2008-04-28 14:47:30 +00001171 if (!flash->write) {
1172 fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
uweff4576d2009-09-30 18:29:55 +00001173 programmer_shutdown();
stuge8ce3a3c2008-04-28 14:47:30 +00001174 return 1;
1175 }
hailfingerdffbe6b2009-07-24 12:18:54 +00001176 ret = flash->write(flash, buf);
1177 if (ret) {
1178 fprintf(stderr, "FAILED!\n");
hailfinger0459e1c2009-08-19 13:55:34 +00001179 emergency_help_message();
uweff4576d2009-09-30 18:29:55 +00001180 programmer_shutdown();
hailfingerdffbe6b2009-07-24 12:18:54 +00001181 return 1;
1182 } else {
1183 printf("COMPLETE.\n");
1184 }
stuge8ce3a3c2008-04-28 14:47:30 +00001185 }
ollie6a600992005-11-26 21:55:36 +00001186
hailfinger0459e1c2009-08-19 13:55:34 +00001187 if (verify_it) {
1188 /* Work around chips which need some time to calm down. */
1189 if (write_it)
1190 programmer_delay(1000*1000);
hailfingerdffbe6b2009-07-24 12:18:54 +00001191 ret = verify_flash(flash, buf);
hailfingera50d60e2009-11-17 09:57:34 +00001192 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00001193 * might have an emergency situation.
1194 */
1195 if (ret && write_it)
1196 emergency_help_message();
1197 }
ollie6a600992005-11-26 21:55:36 +00001198
hailfingerabe249e2009-05-08 17:43:22 +00001199 programmer_shutdown();
1200
stepan83eca252006-01-04 16:42:57 +00001201 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001202}