blob: 7b6f9d575941f86cc7b781925674ee6ef19cfdcc [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00008 *
Uwe Hermannd1107642007-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.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000013 *
Uwe Hermannd1107642007-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.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000018 *
Uwe Hermannd1107642007-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
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000022 */
23
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000024#include <fcntl.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000025#include <sys/types.h>
26#include <sys/stat.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000027#include <string.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000028#include <stdlib.h>
Ollie Lho184a4042005-11-26 21:55:36 +000029#include <getopt.h>
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000030#include "flash.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000031
Ronald G. Minnichceec4202003-07-25 04:37:41 +000032char *chip_to_probe = NULL;
Stefan Reinauere3705282005-12-18 16:41:10 +000033int exclude_start_page, exclude_end_page;
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000034int verbose = 0;
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000035int programmer = PROGRAMMER_INTERNAL;
Stefan Reinauer70385642007-04-06 11:58:03 +000036
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000037const struct programmer_entry programmer_table[] = {
38 {
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000039 .init = internal_init,
40 .shutdown = internal_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000041 .map_flash_region = physmap,
42 .unmap_flash_region = physunmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000043 .chip_readb = internal_chip_readb,
44 .chip_readw = internal_chip_readw,
45 .chip_readl = internal_chip_readl,
46 .chip_writeb = internal_chip_writeb,
47 .chip_writew = internal_chip_writew,
48 .chip_writel = internal_chip_writel,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000049 },
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000050
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000051 {
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000052 .init = dummy_init,
53 .shutdown = dummy_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000054 .map_flash_region = dummy_map,
55 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000056 .chip_readb = dummy_chip_readb,
57 .chip_readw = dummy_chip_readw,
58 .chip_readl = dummy_chip_readl,
59 .chip_writeb = dummy_chip_writeb,
60 .chip_writew = dummy_chip_writew,
61 .chip_writel = dummy_chip_writel,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000062 },
63
Uwe Hermannb4dcb712009-05-13 11:36:06 +000064 {
65 .init = nic3com_init,
66 .shutdown = nic3com_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +000067 .map_flash_region = fallback_map,
68 .unmap_flash_region = fallback_unmap,
Uwe Hermannb4dcb712009-05-13 11:36:06 +000069 .chip_readb = nic3com_chip_readb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +000070 .chip_readw = fallback_chip_readw,
71 .chip_readl = fallback_chip_readl,
Uwe Hermannb4dcb712009-05-13 11:36:06 +000072 .chip_writeb = nic3com_chip_writeb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +000073 .chip_writew = fallback_chip_writew,
74 .chip_writel = fallback_chip_writel,
Uwe Hermannb4dcb712009-05-13 11:36:06 +000075 },
76
Rudolf Marek68720c72009-05-17 19:39:27 +000077 {
78 .init = satasii_init,
79 .shutdown = satasii_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +000080 .map_flash_region = fallback_map,
81 .unmap_flash_region = fallback_unmap,
Rudolf Marek68720c72009-05-17 19:39:27 +000082 .chip_readb = satasii_chip_readb,
83 .chip_readw = fallback_chip_readw,
84 .chip_readl = fallback_chip_readl,
85 .chip_writeb = satasii_chip_writeb,
86 .chip_writew = fallback_chip_writew,
87 .chip_writel = fallback_chip_writel,
88 },
89
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +000090 {
91 .init = it87spi_init,
92 .shutdown = dummy_shutdown,
93 .map_flash_region = dummy_map,
94 .unmap_flash_region = dummy_unmap,
95 .chip_readb = dummy_chip_readb,
96 .chip_readw = dummy_chip_readw,
97 .chip_readl = dummy_chip_readl,
98 .chip_writeb = dummy_chip_writeb,
99 .chip_writew = dummy_chip_writew,
100 .chip_writel = dummy_chip_writel,
101 },
102
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000103 {},
104};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000105
Uwe Hermann09e04f72009-05-16 22:36:00 +0000106int programmer_init(void)
107{
108 return programmer_table[programmer].init();
109}
110
111int programmer_shutdown(void)
112{
113 return programmer_table[programmer].shutdown();
114}
115
116void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
117 size_t len)
118{
119 return programmer_table[programmer].map_flash_region(descr,
120 phys_addr, len);
121}
122
123void programmer_unmap_flash_region(void *virt_addr, size_t len)
124{
125 programmer_table[programmer].unmap_flash_region(virt_addr, len);
126}
127
128void chip_writeb(uint8_t val, chipaddr addr)
129{
130 programmer_table[programmer].chip_writeb(val, addr);
131}
132
133void chip_writew(uint16_t val, chipaddr addr)
134{
135 programmer_table[programmer].chip_writew(val, addr);
136}
137
138void chip_writel(uint32_t val, chipaddr addr)
139{
140 programmer_table[programmer].chip_writel(val, addr);
141}
142
143uint8_t chip_readb(const chipaddr addr)
144{
145 return programmer_table[programmer].chip_readb(addr);
146}
147
148uint16_t chip_readw(const chipaddr addr)
149{
150 return programmer_table[programmer].chip_readw(addr);
151}
152
153uint32_t chip_readl(const chipaddr addr)
154{
155 return programmer_table[programmer].chip_readl(addr);
156}
157
Peter Stuge776d2022009-01-26 00:39:57 +0000158void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000159{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000160 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000161 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000162 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000163}
164
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000165int read_memmapped(struct flashchip *flash, uint8_t *buf)
166{
167 int i;
168
169 /* We could do a memcpy as optimization if the flash is onboard */
170 //memcpy(buf, (const char *)flash->virtual_memory, flash->total_size * 1024);
171 for (i = 0; i < flash->total_size * 1024; i++)
172 buf[i] = chip_readb(flash->virtual_memory + i);
173
174 return 0;
175}
176
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000177char *strcat_realloc(char *dest, const char *src)
178{
179 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
180 if (!dest)
181 return NULL;
182 strcat(dest, src);
183 return dest;
184}
185
186/* Return a string corresponding to the bustype parameter.
187 * Memory is obtained with malloc() and can be freed with free().
188 */
189char *flashbuses_to_text(enum chipbustype bustype)
190{
191 char *ret = calloc(1, 1);
192 if (bustype == CHIP_BUSTYPE_UNKNOWN) {
193 ret = strcat_realloc(ret, "Unknown,");
194 /* FIXME: Once all chipsets and flash chips have been updated, NONSPI
195 * will cease to exist and should be eliminated here as well.
196 */
197 } else if (bustype == CHIP_BUSTYPE_NONSPI) {
198 ret = strcat_realloc(ret, "Non-SPI,");
199 } else {
200 if (bustype & CHIP_BUSTYPE_PARALLEL)
201 ret = strcat_realloc(ret, "Parallel,");
202 if (bustype & CHIP_BUSTYPE_LPC)
203 ret = strcat_realloc(ret, "LPC,");
204 if (bustype & CHIP_BUSTYPE_FWH)
205 ret = strcat_realloc(ret, "FWH,");
206 if (bustype & CHIP_BUSTYPE_SPI)
207 ret = strcat_realloc(ret, "SPI,");
208 if (bustype == CHIP_BUSTYPE_NONE)
209 ret = strcat_realloc(ret, "None,");
210 }
211 /* Kill last comma. */
212 ret[strlen(ret) - 1] = '\0';
213 ret = realloc(ret, strlen(ret) + 1);
214 return ret;
215}
216
Peter Stuge483b8f02008-09-03 23:10:05 +0000217struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000218{
Peter Stuge483b8f02008-09-03 23:10:05 +0000219 struct flashchip *flash;
Peter Stuge7ccce542008-12-03 23:36:48 +0000220 unsigned long base = 0, size;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000221 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000222
Peter Stuge483b8f02008-09-03 23:10:05 +0000223 for (flash = first_flash; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000224 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000225 continue;
Stefan Reinauerac378972008-03-17 22:59:40 +0000226 printf_debug("Probing for %s %s, %d KB: ",
227 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000228 if (!flash->probe && !force) {
Peter Stugef31104c2008-04-28 14:47:30 +0000229 printf_debug("failed! flashrom has no probe function for this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +0000230 continue;
231 }
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000232 if (!(buses_supported & flash->bustype)) {
233 tmp = flashbuses_to_text(buses_supported);
234 printf_debug("skipped. Host bus type %s ", tmp);
235 free(tmp);
236 tmp = flashbuses_to_text(flash->bustype);
237 printf_debug("and chip bus type %s are incompatible.\n", tmp);
238 free(tmp);
239 continue;
240 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000241
Ollie Lhocbbf1252004-03-17 22:22:08 +0000242 size = flash->total_size * 1024;
Stefan Reinauer70385642007-04-06 11:58:03 +0000243
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +0000244 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000245 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000246
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000247 if (force)
248 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000249
Peter Stuge483b8f02008-09-03 23:10:05 +0000250 if (flash->probe(flash) != 1)
251 goto notfound;
252
Uwe Hermann394131e2008-10-18 21:14:13 +0000253 if (first_flash == flashchips
254 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000255 break;
256
Peter Stuge483b8f02008-09-03 23:10:05 +0000257notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000258 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000259 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000260
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000261 if (!flash || !flash->name)
262 return NULL;
263
264 printf("Found chip \"%s %s\" (%d KB) at physical address 0x%lx.\n",
Peter Stuge73bdb922008-12-03 21:39:56 +0000265 flash->vendor, flash->name, flash->total_size, base);
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000266 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000267}
268
Stefan Reinauere3705282005-12-18 16:41:10 +0000269int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000270{
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000271 int idx;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000272 int total_size = flash->total_size * 1024;
Carl-Daniel Hailfingerd7b5bf32008-01-22 15:19:01 +0000273 uint8_t *buf2 = (uint8_t *) calloc(total_size, sizeof(char));
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000274 if (!flash->read) {
275 printf("FAILED!\n");
276 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
277 return 1;
278 } else
Carl-Daniel Hailfingerd7b5bf32008-01-22 15:19:01 +0000279 flash->read(flash, buf2);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000280
Uwe Hermanna502dce2007-10-17 23:55:15 +0000281 printf("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000282
283 if (verbose)
284 printf("address: 0x00000000\b\b\b\b\b\b\b\b\b\b");
285
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000286 for (idx = 0; idx < total_size; idx++) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000287 if (verbose && ((idx & 0xfff) == 0xfff))
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000288 printf("0x%08x", idx);
289
Carl-Daniel Hailfingerd7b5bf32008-01-22 15:19:01 +0000290 if (*(buf2 + idx) != *(buf + idx)) {
Peter Stugef9ad0bb2009-01-13 14:32:27 +0000291 if (verbose)
292 printf("0x%08x FAILED!", idx);
293 else
294 printf("FAILED at 0x%08x!", idx);
295 printf(" Expected=0x%02x, Read=0x%02x\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000296 *(buf + idx), *(buf2 + idx));
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000297 return 1;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000298 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000299
300 if (verbose && ((idx & 0xfff) == 0xfff))
Ollie Lhocbbf1252004-03-17 22:22:08 +0000301 printf("\b\b\b\b\b\b\b\b\b\b");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000302 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000303 if (verbose)
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000304 printf("\b\b\b\b\b\b\b\b\b\b ");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000305
Uwe Hermanna502dce2007-10-17 23:55:15 +0000306 printf("VERIFIED. \n");
Uwe Hermannffec5f32007-08-23 16:08:21 +0000307
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000308 return 0;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000309}
310
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000311int read_flash(struct flashchip *flash, char *filename, unsigned int exclude_start_position, unsigned int exclude_end_position)
312{
313 unsigned long numbytes;
314 FILE *image;
315 unsigned long size = flash->total_size * 1024;
316 unsigned char *buf = calloc(size, sizeof(char));
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000317
318 if (!filename) {
319 printf("Error: No filename specified.\n");
320 return 1;
321 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000322 if ((image = fopen(filename, "w")) == NULL) {
323 perror(filename);
324 exit(1);
325 }
326 printf("Reading flash... ");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000327 if (!flash->read) {
328 printf("FAILED!\n");
329 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
330 return 1;
331 } else
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000332 flash->read(flash, buf);
333
334 if (exclude_end_position - exclude_start_position > 0)
335 memset(buf + exclude_start_position, 0,
336 exclude_end_position - exclude_start_position);
337
338 numbytes = fwrite(buf, 1, size, image);
339 fclose(image);
Stephan Guilloux5a8b2442009-06-01 21:37:00 +0000340 free(buf);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000341 printf("%s.\n", numbytes == size ? "done" : "FAILED");
342 if (numbytes != size)
343 return 1;
344 return 0;
345}
346
347int erase_flash(struct flashchip *flash)
348{
349 uint32_t erasedbytes;
350 unsigned long size = flash->total_size * 1024;
351 unsigned char *buf = calloc(size, sizeof(char));
352 printf("Erasing flash chip... ");
353 if (NULL == flash->erase) {
354 printf("FAILED!\n");
355 fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n");
356 return 1;
357 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +0000358 flash->erase(flash);
359
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000360 if (!flash->read) {
361 printf("FAILED!\n");
362 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
363 return 1;
364 } else
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000365 flash->read(flash, buf);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000366
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000367 for (erasedbytes = 0; erasedbytes < size; erasedbytes++)
368 if (0xff != buf[erasedbytes]) {
369 printf("FAILED!\n");
370 fprintf(stderr, "ERROR at 0x%08x: Expected=0xff, Read=0x%02x\n",
371 erasedbytes, buf[erasedbytes]);
372 return 1;
373 }
374 printf("SUCCESS.\n");
375 return 0;
376}
377
Uwe Hermannad216bf2009-04-24 16:17:41 +0000378#ifndef MAX
Peter Stugef0ef27b2008-12-06 01:37:09 +0000379#define MAX(a, b) ((a) > (b) ? (a) : (b))
Uwe Hermannad216bf2009-04-24 16:17:41 +0000380#endif
Peter Stugef0ef27b2008-12-06 01:37:09 +0000381#define POS_PRINT(x) do { pos += strlen(x); printf(x); } while (0)
382
Uwe Hermanne5ac1642008-03-12 11:54:51 +0000383void print_supported_chips(void)
384{
Uwe Hermanne8ba5382009-05-22 11:37:27 +0000385 int okcol = 0, pos = 0, i;
Peter Stugef0ef27b2008-12-06 01:37:09 +0000386 struct flashchip *f;
Uwe Hermanne5ac1642008-03-12 11:54:51 +0000387
Peter Stugef0ef27b2008-12-06 01:37:09 +0000388 for (f = flashchips; f->name != NULL; f++) {
389 if (GENERIC_DEVICE_ID == f->model_id)
390 continue;
391 okcol = MAX(okcol, strlen(f->vendor) + 1 + strlen(f->name));
392 }
393 okcol = (okcol + 7) & ~7;
Uwe Hermanne5ac1642008-03-12 11:54:51 +0000394
Uwe Hermanne8ba5382009-05-22 11:37:27 +0000395 printf("Supported flash chips:\n\n");
396 POS_PRINT("Vendor: Device:");
Peter Stugef0ef27b2008-12-06 01:37:09 +0000397 while (pos < okcol) {
398 printf("\t");
399 pos += 8 - (pos % 8);
400 }
401 printf("Tested OK operations:\tKnown BAD operations:\n\n");
402
403 for (f = flashchips; f->name != NULL; f++) {
Uwe Hermanne8ba5382009-05-22 11:37:27 +0000404 /* Don't print "unknown XXXX SPI chip" entries. */
405 if (!strncmp(f->name, "unknown", 7))
406 continue;
407
408 printf("%s", f->vendor);
409 for (i = 0; i < 10 - strlen(f->vendor); i++)
410 printf(" ");
411 printf("%s", f->name);
412
413 pos = 10 + strlen(f->name);
Peter Stugef0ef27b2008-12-06 01:37:09 +0000414 while (pos < okcol) {
415 printf("\t");
416 pos += 8 - (pos % 8);
417 }
418 if ((f->tested & TEST_OK_MASK)) {
419 if ((f->tested & TEST_OK_PROBE))
420 POS_PRINT("PROBE ");
421 if ((f->tested & TEST_OK_READ))
422 POS_PRINT("READ ");
423 if ((f->tested & TEST_OK_ERASE))
424 POS_PRINT("ERASE ");
425 if ((f->tested & TEST_OK_WRITE))
426 POS_PRINT("WRITE");
427 }
428 while (pos < okcol + 24) {
429 printf("\t");
430 pos += 8 - (pos % 8);
431 }
432 if ((f->tested & TEST_BAD_MASK)) {
433 if ((f->tested & TEST_BAD_PROBE))
434 printf("PROBE ");
435 if ((f->tested & TEST_BAD_READ))
436 printf("READ ");
437 if ((f->tested & TEST_BAD_ERASE))
438 printf("ERASE ");
439 if ((f->tested & TEST_BAD_WRITE))
440 printf("WRITE");
441 }
442 printf("\n");
443 }
Uwe Hermanne5ac1642008-03-12 11:54:51 +0000444}
445
Ronald G. Minniche5559572003-03-28 03:29:43 +0000446void usage(const char *name)
447{
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +0000448 printf("usage: %s [-EVfLhR] [-r file] [-w file] [-v file] [-c chipname] [-s addr]\n"
Uwe Hermanne8ba5382009-05-22 11:37:27 +0000449 " [-e addr] [-m [vendor:]part] [-l file] [-i image] [-p programmer] [file]\n\n",
Uwe Hermann394131e2008-10-18 21:14:13 +0000450 name);
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +0000451
Uwe Hermanne8ba5382009-05-22 11:37:27 +0000452 printf("Please note that the command line interface for flashrom will "
453 "change before\nflashrom 1.0. Do not use flashrom in scripts "
454 "or other automated tools without\nchecking that your flashrom"
455 " version won't interpret options in a different way.\n\n");
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +0000456
Uwe Hermanna7e05482007-05-09 10:17:44 +0000457 printf
Peter Stuge6b53fed2008-01-27 16:21:21 +0000458 (" -r | --read: read flash and save into file\n"
459 " -w | --write: write file into flash\n"
460 " -v | --verify: verify flash against file\n"
461 " -E | --erase: erase flash device\n"
462 " -V | --verbose: more verbose output\n"
463 " -c | --chip <chipname>: probe only for specified flash chip\n"
464 " -s | --estart <addr>: exclude start position\n"
465 " -e | --eend <addr>: exclude end postion\n"
466 " -m | --mainboard <[vendor:]part>: override mainboard settings\n"
467 " -f | --force: force write without checking image\n"
Uwe Hermanne8ba5382009-05-22 11:37:27 +0000468 " -l | --layout <file.layout>: read ROM layout from file\n"
Peter Stuge6b53fed2008-01-27 16:21:21 +0000469 " -i | --image <name>: only flash image name from flash layout\n"
Uwe Hermanne5ac1642008-03-12 11:54:51 +0000470 " -L | --list-supported: print supported devices\n"
Carl-Daniel Hailfingerce986772009-05-09 00:27:07 +0000471 " -p | --programmer <name>: specify the programmer device\n"
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000472 " (internal, dummy, nic3com, satasii, it87spi)\n"
Uwe Hermanne5ac1642008-03-12 11:54:51 +0000473 " -h | --help: print this help text\n"
Peter Stuge6b53fed2008-01-27 16:21:21 +0000474 " -R | --version: print the version (release)\n"
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +0000475 "\nIf no file is specified, then all that happens"
Uwe Hermanna7e05482007-05-09 10:17:44 +0000476 " is that flash info is dumped.\n\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000477 exit(1);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000478}
479
Bernhard Walle201bde32008-01-21 15:24:22 +0000480void print_version(void)
481{
Carl-Daniel Hailfinger3f8f9b62009-05-04 12:18:10 +0000482 printf("flashrom v%s\n", FLASHROM_VERSION);
Bernhard Walle201bde32008-01-21 15:24:22 +0000483}
484
Ollie Lho761bf1b2004-03-20 16:46:10 +0000485int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000486{
Ollie Lho184a4042005-11-26 21:55:36 +0000487 uint8_t *buf;
Peter Stuge1fec0f32009-01-12 21:00:35 +0000488 unsigned long size, numbytes;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000489 FILE *image;
Claus Gindhart2fd11d62008-05-08 00:31:44 +0000490 /* Probe for up to three flash chips. */
491 struct flashchip *flash, *flashes[3];
Ollie Lhocbbf1252004-03-17 22:22:08 +0000492 int opt;
Ollie Lho184a4042005-11-26 21:55:36 +0000493 int option_index = 0;
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000494 int force = 0;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000495 int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
Uwe Hermann05fab752009-05-16 23:42:17 +0000496 int list_supported = 0;
Claus Gindhart2fd11d62008-05-08 00:31:44 +0000497 int ret = 0, i;
Ollie Lho184a4042005-11-26 21:55:36 +0000498
Uwe Hermanna7e05482007-05-09 10:17:44 +0000499 static struct option long_options[] = {
500 {"read", 0, 0, 'r'},
501 {"write", 0, 0, 'w'},
502 {"erase", 0, 0, 'E'},
503 {"verify", 0, 0, 'v'},
504 {"chip", 1, 0, 'c'},
505 {"estart", 1, 0, 's'},
506 {"eend", 1, 0, 'e'},
507 {"mainboard", 1, 0, 'm'},
508 {"verbose", 0, 0, 'V'},
509 {"force", 0, 0, 'f'},
510 {"layout", 1, 0, 'l'},
511 {"image", 1, 0, 'i'},
Uwe Hermanne5ac1642008-03-12 11:54:51 +0000512 {"list-supported", 0, 0, 'L'},
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000513 {"programmer", 1, 0, 'p'},
Uwe Hermanna7e05482007-05-09 10:17:44 +0000514 {"help", 0, 0, 'h'},
Bernhard Walle201bde32008-01-21 15:24:22 +0000515 {"version", 0, 0, 'R'},
Uwe Hermanna7e05482007-05-09 10:17:44 +0000516 {0, 0, 0, 0}
Ollie Lho184a4042005-11-26 21:55:36 +0000517 };
Uwe Hermanna7e05482007-05-09 10:17:44 +0000518
Ollie Lhocbbf1252004-03-17 22:22:08 +0000519 char *filename = NULL;
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000520
Uwe Hermanna7e05482007-05-09 10:17:44 +0000521 unsigned int exclude_start_position = 0, exclude_end_position = 0; // [x,y)
522 char *tempstr = NULL, *tempstr2 = NULL;
Yinghai Luad8ffd22004-10-20 05:07:16 +0000523
Carl-Daniel Hailfinger259fa012009-05-07 00:59:53 +0000524 print_version();
525
Yinghai Luad8ffd22004-10-20 05:07:16 +0000526 if (argc > 1) {
527 /* Yes, print them. */
528 int i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000529 printf_debug("The arguments are:\n");
Yinghai Luad8ffd22004-10-20 05:07:16 +0000530 for (i = 1; i < argc; ++i)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000531 printf_debug("%s\n", argv[i]);
Yinghai Luad8ffd22004-10-20 05:07:16 +0000532 }
533
Ollie Lhocbbf1252004-03-17 22:22:08 +0000534 setbuf(stdout, NULL);
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000535 while ((opt = getopt_long(argc, argv, "rRwvVEfc:s:e:m:l:i:p:Lh",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000536 long_options, &option_index)) != EOF) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000537 switch (opt) {
538 case 'r':
539 read_it = 1;
540 break;
541 case 'w':
542 write_it = 1;
543 break;
544 case 'v':
545 verify_it = 1;
546 break;
547 case 'c':
548 chip_to_probe = strdup(optarg);
549 break;
Ollie Lho789ad3d2004-03-18 20:27:33 +0000550 case 'V':
551 verbose = 1;
552 break;
Ollie Lhoefa28582004-12-08 20:10:01 +0000553 case 'E':
554 erase_it = 1;
555 break;
Yinghai Luad8ffd22004-10-20 05:07:16 +0000556 case 's':
557 tempstr = strdup(optarg);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000558 sscanf(tempstr, "%x", &exclude_start_position);
Yinghai Luad8ffd22004-10-20 05:07:16 +0000559 break;
560 case 'e':
Ollie Lho98bea8a2004-12-07 03:15:51 +0000561 tempstr = strdup(optarg);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000562 sscanf(tempstr, "%x", &exclude_end_position);
Ollie Lho98bea8a2004-12-07 03:15:51 +0000563 break;
Ollie Lho184a4042005-11-26 21:55:36 +0000564 case 'm':
565 tempstr = strdup(optarg);
566 strtok(tempstr, ":");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000567 tempstr2 = strtok(NULL, ":");
Ollie Lho184a4042005-11-26 21:55:36 +0000568 if (tempstr2) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000569 lb_vendor = tempstr;
570 lb_part = tempstr2;
Ollie Lho184a4042005-11-26 21:55:36 +0000571 } else {
Peter Stuge6b53fed2008-01-27 16:21:21 +0000572 lb_vendor = NULL;
573 lb_part = tempstr;
Ollie Lho184a4042005-11-26 21:55:36 +0000574 }
575 break;
576 case 'f':
Uwe Hermanna7e05482007-05-09 10:17:44 +0000577 force = 1;
Ollie Lho184a4042005-11-26 21:55:36 +0000578 break;
579 case 'l':
Uwe Hermanna7e05482007-05-09 10:17:44 +0000580 tempstr = strdup(optarg);
Stefan Reinauer0a05d672007-04-14 16:32:59 +0000581 if (read_romlayout(tempstr))
582 exit(1);
Ollie Lho184a4042005-11-26 21:55:36 +0000583 break;
584 case 'i':
Uwe Hermanna7e05482007-05-09 10:17:44 +0000585 tempstr = strdup(optarg);
Ollie Lho184a4042005-11-26 21:55:36 +0000586 find_romentry(tempstr);
587 break;
Uwe Hermanne5ac1642008-03-12 11:54:51 +0000588 case 'L':
Uwe Hermann05fab752009-05-16 23:42:17 +0000589 list_supported = 1;
Uwe Hermanne5ac1642008-03-12 11:54:51 +0000590 break;
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000591 case 'p':
592 if (strncmp(optarg, "internal", 8) == 0) {
593 programmer = PROGRAMMER_INTERNAL;
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000594 } else if (strncmp(optarg, "dummy", 5) == 0) {
595 programmer = PROGRAMMER_DUMMY;
Carl-Daniel Hailfinger3504b532009-06-01 00:02:11 +0000596 if (optarg[5] == '=')
597 dummytype = strdup(optarg + 6);
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000598 } else if (strncmp(optarg, "nic3com", 7) == 0) {
599 programmer = PROGRAMMER_NIC3COM;
Christian Ruppert0cdb0312009-05-14 18:57:26 +0000600 if (optarg[7] == '=')
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000601 pcidev_bdf = strdup(optarg + 8);
Rudolf Marek68720c72009-05-17 19:39:27 +0000602 } else if (strncmp(optarg, "satasii", 7) == 0) {
603 programmer = PROGRAMMER_SATASII;
604 if (optarg[7] == '=')
605 pcidev_bdf = strdup(optarg + 8);
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000606 } else if (strncmp(optarg, "it87spi", 7) == 0) {
607 programmer = PROGRAMMER_IT87SPI;
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000608 } else {
609 printf("Error: Unknown programmer.\n");
610 exit(1);
611 }
612 break;
Bernhard Walle201bde32008-01-21 15:24:22 +0000613 case 'R':
Carl-Daniel Hailfinger259fa012009-05-07 00:59:53 +0000614 /* print_version() is always called during startup. */
Bernhard Walle201bde32008-01-21 15:24:22 +0000615 exit(0);
616 break;
Ollie Lho184a4042005-11-26 21:55:36 +0000617 case 'h':
Ollie Lhocbbf1252004-03-17 22:22:08 +0000618 default:
619 usage(argv[0]);
620 break;
621 }
622 }
Yinghai Luad8ffd22004-10-20 05:07:16 +0000623
Uwe Hermann05fab752009-05-16 23:42:17 +0000624 if (list_supported) {
625 print_supported_chips();
626 print_supported_chipsets();
627 print_supported_boards();
628 printf("\nSupported PCI devices flashrom can use "
629 "as programmer:\n\n");
630 print_supported_pcidevs(nics_3com);
Uwe Hermanneaefb482009-05-17 22:57:34 +0000631 print_supported_pcidevs(satas_sii);
Uwe Hermann05fab752009-05-16 23:42:17 +0000632 exit(0);
633 }
634
Ollie Lhocbbf1252004-03-17 22:22:08 +0000635 if (read_it && write_it) {
Uwe Hermann793bdcd2008-05-22 22:47:04 +0000636 printf("Error: -r and -w are mutually exclusive.\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000637 usage(argv[0]);
638 }
Ronald G. Minniche5559572003-03-28 03:29:43 +0000639
Ollie Lhocbbf1252004-03-17 22:22:08 +0000640 if (optind < argc)
641 filename = argv[optind++];
Ronald G. Minniche5559572003-03-28 03:29:43 +0000642
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000643 ret = programmer_init();
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000644
Ollie Lhocbbf1252004-03-17 22:22:08 +0000645 myusec_calibrate_delay();
Ollie Lho184a4042005-11-26 21:55:36 +0000646
Claus Gindhart2fd11d62008-05-08 00:31:44 +0000647 for (i = 0; i < ARRAY_SIZE(flashes); i++) {
Uwe Hermann394131e2008-10-18 21:14:13 +0000648 flashes[i] =
649 probe_flash(i ? flashes[i - 1] + 1 : flashchips, 0);
Claus Gindhart2fd11d62008-05-08 00:31:44 +0000650 if (!flashes[i])
651 for (i++; i < ARRAY_SIZE(flashes); i++)
652 flashes[i] = NULL;
653 }
654
655 if (flashes[1]) {
656 printf("Multiple flash chips were detected:");
657 for (i = 0; i < ARRAY_SIZE(flashes) && flashes[i]; i++)
658 printf(" %s", flashes[i]->name);
659 printf("\nPlease specify which chip to use with the -c <chipname> option.\n");
660 exit(1);
661 } else if (!flashes[0]) {
Ollie Lho184a4042005-11-26 21:55:36 +0000662 printf("No EEPROM/flash device found.\n");
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000663 if (!force || !chip_to_probe) {
664 printf("If you know which flash chip you have, and if this version of flashrom\n");
665 printf("supports a similar flash chip, you can try to force read your chip. Run:\n");
666 printf("flashrom -f -r -c similar_supported_flash_chip filename\n");
667 printf("\n");
668 printf("Note: flashrom can never write when the flash chip isn't found automatically.\n");
669 }
670 if (force && read_it && chip_to_probe) {
671 printf("Force read (-f -r -c) requested, forcing chip probe success:\n");
672 flashes[0] = probe_flash(flashchips, 1);
673 if (!flashes[0]) {
674 printf("flashrom does not support a flash chip named '%s'.\n", chip_to_probe);
675 printf("Run flashrom -L to view the hardware supported in this flashrom version.\n");
676 exit(1);
677 }
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000678 return read_flash(flashes[0], filename, exclude_start_position, exclude_end_position);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000679 }
Jordan Crouse144ede62007-10-04 06:26:41 +0000680 // FIXME: flash writes stay enabled!
Ollie Lhocbbf1252004-03-17 22:22:08 +0000681 exit(1);
682 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000683
Claus Gindhart2fd11d62008-05-08 00:31:44 +0000684 flash = flashes[0];
685
Peter Stuge1159d582008-05-03 04:34:37 +0000686 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
687 printf("===\n");
688 if (flash->tested & TEST_BAD_MASK) {
689 printf("This flash part has status NOT WORKING for operations:");
690 if (flash->tested & TEST_BAD_PROBE)
691 printf(" PROBE");
692 if (flash->tested & TEST_BAD_READ)
693 printf(" READ");
694 if (flash->tested & TEST_BAD_ERASE)
695 printf(" ERASE");
696 if (flash->tested & TEST_BAD_WRITE)
697 printf(" WRITE");
698 printf("\n");
Carl-Daniel Hailfinger6a0a25c2008-11-28 23:45:27 +0000699 }
700 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
701 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
702 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
703 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
Peter Stuge1159d582008-05-03 04:34:37 +0000704 printf("This flash part has status UNTESTED for operations:");
Carl-Daniel Hailfinger6a0a25c2008-11-28 23:45:27 +0000705 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
Peter Stuge1159d582008-05-03 04:34:37 +0000706 printf(" PROBE");
Carl-Daniel Hailfinger6a0a25c2008-11-28 23:45:27 +0000707 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
Peter Stuge1159d582008-05-03 04:34:37 +0000708 printf(" READ");
Carl-Daniel Hailfinger6a0a25c2008-11-28 23:45:27 +0000709 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
Peter Stuge1159d582008-05-03 04:34:37 +0000710 printf(" ERASE");
Carl-Daniel Hailfinger6a0a25c2008-11-28 23:45:27 +0000711 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
Peter Stuge1159d582008-05-03 04:34:37 +0000712 printf(" WRITE");
713 printf("\n");
714 }
Uwe Hermanna9720402009-05-21 15:55:46 +0000715 printf("Please email a report to flashrom@coreboot.org if any "
716 "of the above operations\nwork correctly for you with "
717 "this flash part. Please include the flashrom\noutput "
718 "with the additional -V option for all operations you "
719 "tested (-V, -rV,\n-wV, -EV), and mention which "
720 "mainboard you tested. Thanks for your help!\n===\n");
Peter Stuge1159d582008-05-03 04:34:37 +0000721 }
Ollie Lho184a4042005-11-26 21:55:36 +0000722
Jordan Crouse144ede62007-10-04 06:26:41 +0000723 if (!(read_it | write_it | verify_it | erase_it)) {
724 printf("No operations were specified.\n");
725 // FIXME: flash writes stay enabled!
726 exit(1);
727 }
728
Ollie Lhoefa28582004-12-08 20:10:01 +0000729 if (!filename && !erase_it) {
Jordan Crouse144ede62007-10-04 06:26:41 +0000730 printf("Error: No filename specified.\n");
731 // FIXME: flash writes stay enabled!
732 exit(1);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000733 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000734
Ollie Lho184a4042005-11-26 21:55:36 +0000735 size = flash->total_size * 1024;
736 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +0000737
Ollie Lhoefa28582004-12-08 20:10:01 +0000738 if (erase_it) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000739 if (erase_flash(flash))
Peter Stugef31104c2008-04-28 14:47:30 +0000740 return 1;
Ollie Lhoefa28582004-12-08 20:10:01 +0000741 } else if (read_it) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000742 if (read_flash(flash, filename, exclude_start_position, exclude_end_position))
Peter Stuge1fec0f32009-01-12 21:00:35 +0000743 return 1;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000744 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +0000745 struct stat image_stat;
746
Ollie Lho761bf1b2004-03-20 16:46:10 +0000747 if ((image = fopen(filename, "r")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000748 perror(filename);
749 exit(1);
750 }
Stefan Reinauer018aca82006-11-21 23:48:51 +0000751 if (fstat(fileno(image), &image_stat) != 0) {
752 perror(filename);
753 exit(1);
754 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000755 if (image_stat.st_size != flash->total_size * 1024) {
Uwe Hermann793bdcd2008-05-22 22:47:04 +0000756 fprintf(stderr, "Error: Image size doesn't match\n");
Stefan Reinauer018aca82006-11-21 23:48:51 +0000757 exit(1);
758 }
759
Peter Stuge1fec0f32009-01-12 21:00:35 +0000760 numbytes = fread(buf, 1, size, image);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000761 show_id(buf, size, force);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000762 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +0000763 if (numbytes != size) {
764 fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
765 return 1;
766 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000767 }
768
Ollie Lho184a4042005-11-26 21:55:36 +0000769 /* exclude range stuff. Nice idea, but at the moment it is only
770 * supported in hardware by the pm49fl004 chips.
771 * Instead of implementing this for all chips I suggest advancing
772 * it to the rom layout feature below and drop exclude range
773 * completely once all flash chips can do rom layouts. stepan
774 */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000775
Ollie Lho184a4042005-11-26 21:55:36 +0000776 // ////////////////////////////////////////////////////////////
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000777 /* FIXME: This memcpy will not work for SPI nor external flashers.
778 * Convert to chip_readb.
779 */
Ollie Lho98bea8a2004-12-07 03:15:51 +0000780 if (exclude_end_position - exclude_start_position > 0)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000781 memcpy(buf + exclude_start_position,
Uwe Hermanna8808852007-05-24 19:17:29 +0000782 (const char *)flash->virtual_memory +
783 exclude_start_position,
Uwe Hermanna7e05482007-05-09 10:17:44 +0000784 exclude_end_position - exclude_start_position);
Ollie Lho98bea8a2004-12-07 03:15:51 +0000785
Uwe Hermanna7e05482007-05-09 10:17:44 +0000786 exclude_start_page = exclude_start_position / flash->page_size;
787 if ((exclude_start_position % flash->page_size) != 0) {
Ollie Lho98bea8a2004-12-07 03:15:51 +0000788 exclude_start_page++;
789 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000790 exclude_end_page = exclude_end_position / flash->page_size;
Ollie Lho184a4042005-11-26 21:55:36 +0000791 // ////////////////////////////////////////////////////////////
Yinghai Luad8ffd22004-10-20 05:07:16 +0000792
Ollie Lho184a4042005-11-26 21:55:36 +0000793 // This should be moved into each flash part's code to do it
794 // cleanly. This does the job.
Stefan Reinauerce532972007-05-23 17:20:56 +0000795 handle_romentries(buf, (uint8_t *) flash->virtual_memory);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000796
Ollie Lho184a4042005-11-26 21:55:36 +0000797 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +0000798
Peter Stugef31104c2008-04-28 14:47:30 +0000799 if (write_it) {
800 if (!flash->write) {
801 fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
802 return 1;
803 }
Stefan Reinauer143da0b2006-01-04 16:42:57 +0000804 ret |= flash->write(flash, buf);
Peter Stugef31104c2008-04-28 14:47:30 +0000805 }
Ollie Lho184a4042005-11-26 21:55:36 +0000806
Ollie Lhocbbf1252004-03-17 22:22:08 +0000807 if (verify_it)
Stefan Reinauer143da0b2006-01-04 16:42:57 +0000808 ret |= verify_flash(flash, buf);
Ollie Lho184a4042005-11-26 21:55:36 +0000809
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000810 programmer_shutdown();
811
Stefan Reinauer143da0b2006-01-04 16:42:57 +0000812 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000813}