blob: 3928ded323d6fc236879740d199857356161be68 [file] [log] [blame]
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
6 * Copyright (C) 2005-2008 coresystems GmbH
7 * Copyright (C) 2008,2009,2010 Carl-Daniel Hailfinger
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000024#include <stdio.h>
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000025#include <fcntl.h>
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000026#include <sys/stat.h>
27#include <string.h>
28#include <stdlib.h>
29#include <getopt.h>
30#include "flash.h"
31#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000032#include "programmer.h"
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000033
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +000034#if CONFIG_INTERNAL == 1
35static enum programmer default_programmer = PROGRAMMER_INTERNAL;
36#elif CONFIG_DUMMY == 1
37static enum programmer default_programmer = PROGRAMMER_DUMMY;
38#else
39/* If neither internal nor dummy are selected, we must pick a sensible default.
40 * Since there is no reason to prefer a particular external programmer, we fail
41 * if more than one of them is selected. If only one is selected, it is clear
42 * that the user wants that one to become the default.
43 */
44#if CONFIG_NIC3COM+CONFIG_NICREALTEK+CONFIG_NICNATSEMI+CONFIG_GFXNVIDIA+CONFIG_DRKAISER+CONFIG_SATASII+CONFIG_ATAHPT+CONFIG_FT2232_SPI+CONFIG_SERPROG+CONFIG_BUSPIRATE_SPI+CONFIG_DEDIPROG+CONFIG_RAYER_SPI+CONFIG_NICINTEL+CONFIG_NICINTEL_SPI+CONFIG_OGP_SPI+CONFIG_SATAMV > 1
45#error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all programmers except one.
46#endif
47static enum programmer default_programmer =
48#if CONFIG_NIC3COM == 1
49 PROGRAMMER_NIC3COM
50#endif
51#if CONFIG_NICREALTEK == 1
52 PROGRAMMER_NICREALTEK
53#endif
54#if CONFIG_NICNATSEMI == 1
55 PROGRAMMER_NICNATSEMI
56#endif
57#if CONFIG_GFXNVIDIA == 1
58 PROGRAMMER_GFXNVIDIA
59#endif
60#if CONFIG_DRKAISER == 1
61 PROGRAMMER_DRKAISER
62#endif
63#if CONFIG_SATASII == 1
64 PROGRAMMER_SATASII
65#endif
66#if CONFIG_ATAHPT == 1
67 PROGRAMMER_ATAHPT
68#endif
69#if CONFIG_FT2232_SPI == 1
70 PROGRAMMER_FT2232_SPI
71#endif
72#if CONFIG_SERPROG == 1
73 PROGRAMMER_SERPROG
74#endif
75#if CONFIG_BUSPIRATE_SPI == 1
76 PROGRAMMER_BUSPIRATE_SPI
77#endif
78#if CONFIG_DEDIPROG == 1
79 PROGRAMMER_DEDIPROG
80#endif
81#if CONFIG_RAYER_SPI == 1
82 PROGRAMMER_RAYER_SPI
83#endif
84#if CONFIG_NICINTEL == 1
85 PROGRAMMER_NICINTEL
86#endif
87#if CONFIG_NICINTEL_SPI == 1
88 PROGRAMMER_NICINTEL_SPI
89#endif
90#if CONFIG_OGP_SPI == 1
91 PROGRAMMER_OGP_SPI
92#endif
93#if CONFIG_SATAMV == 1
94 PROGRAMMER_SATAMV
95#endif
96#if CONFIG_LINUX_SPI == 1
97 PROGRAMMER_LINUX_SPI
98#endif
99;
100#endif
101
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000102static void cli_classic_usage(const char *name)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000103{
Uwe Hermann2db77a02010-06-04 17:07:39 +0000104 printf("Usage: flashrom [-n] [-V] [-f] [-h|-R|-L|"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000105#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000106 "-z|"
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000107#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000108 "-E|-r <file>|-w <file>|-v <file>]\n"
109 " [-c <chipname>] [-m [<vendor>:]<part>] [-l <file>]\n"
Uwe Hermann2db77a02010-06-04 17:07:39 +0000110 " [-i <image>] [-p <programmername>[:<parameters>]]\n\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000111
112 printf("Please note that the command line interface for flashrom has "
113 "changed between\n"
114 "0.9.1 and 0.9.2 and will change again before flashrom 1.0.\n"
115 "Do not use flashrom in scripts or other automated tools "
116 "without checking\n"
117 "that your flashrom version won't interpret options in a "
118 "different way.\n\n");
119
120 printf(" -h | --help print this help text\n"
121 " -R | --version print version (release)\n"
122 " -r | --read <file> read flash and save to "
123 "<file>\n"
124 " -w | --write <file> write <file> to flash\n"
125 " -v | --verify <file> verify flash against "
126 "<file>\n"
127 " -E | --erase erase flash device\n"
128 " -V | --verbose more verbose output\n"
129 " -c | --chip <chipname> probe only for specified "
130 "flash chip\n"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000131#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000132 /* FIXME: --mainboard should be a programmer parameter */
133 " -m | --mainboard <[vendor:]part> override mainboard "
134 "detection\n"
135#endif
136 " -f | --force force specific operations "
137 "(see man page)\n"
138 " -n | --noverify don't auto-verify\n"
139 " -l | --layout <file> read ROM layout from "
140 "<file>\n"
141 " -i | --image <name> only flash image <name> "
142 "from flash layout\n"
143 " -L | --list-supported print supported devices\n"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000144#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000145 " -z | --list-supported-wiki print supported devices "
146 "in wiki syntax\n"
147#endif
148 " -p | --programmer <name>[:<param>] specify the programmer "
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +0000149 "device\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000150
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +0000151 list_programmers_linebreak(37, 80, 1);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000152 printf("\nYou can specify one of -h, -R, -L, "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000153#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000154 "-z, "
155#endif
156 "-E, -r, -w, -v or no operation.\n"
157 "If no operation is specified, flashrom will only probe for "
158 "flash chips.\n\n");
159}
160
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000161static void cli_classic_abort_usage(void)
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000162{
Uwe Hermann2db77a02010-06-04 17:07:39 +0000163 printf("Please run \"flashrom --help\" for usage info.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000164 exit(1);
165}
166
Uwe Hermann394ee782011-08-20 14:14:22 +0000167int main(int argc, char *argv[])
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000168{
169 unsigned long size;
170 /* Probe for up to three flash chips. */
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000171 const struct flashchip *flash;
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000172 struct flashctx flashes[3];
173 struct flashctx *fill_flash;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000174 const char *name;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000175 int namelen, opt, i, j;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000176 int startchip = 0, chipcount = 0, option_index = 0, force = 0;
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000177#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000178 int list_supported_wiki = 0;
179#endif
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000180 int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
181 int dont_verify_it = 0, list_supported = 0, operation_specified = 0;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000182 enum programmer prog = PROGRAMMER_INVALID;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000183 int ret = 0;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000184
Mathias Krausea60faab2011-01-17 07:50:42 +0000185 static const char optstring[] = "r:Rw:v:nVEfc:m:l:i:p:Lzh";
186 static const struct option long_options[] = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000187 {"read", 1, NULL, 'r'},
188 {"write", 1, NULL, 'w'},
189 {"erase", 0, NULL, 'E'},
190 {"verify", 1, NULL, 'v'},
191 {"noverify", 0, NULL, 'n'},
192 {"chip", 1, NULL, 'c'},
193 {"mainboard", 1, NULL, 'm'},
194 {"verbose", 0, NULL, 'V'},
195 {"force", 0, NULL, 'f'},
196 {"layout", 1, NULL, 'l'},
197 {"image", 1, NULL, 'i'},
198 {"list-supported", 0, NULL, 'L'},
199 {"list-supported-wiki", 0, NULL, 'z'},
200 {"programmer", 1, NULL, 'p'},
201 {"help", 0, NULL, 'h'},
202 {"version", 0, NULL, 'R'},
203 {NULL, 0, NULL, 0},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000204 };
205
206 char *filename = NULL;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000207 char *tempstr = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000208 char *pparam = NULL;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000209
210 print_version();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000211 print_banner();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000212
213 if (selfcheck())
214 exit(1);
215
216 setbuf(stdout, NULL);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000217 /* FIXME: Delay all operation_specified checks until after command
218 * line parsing to allow --help overriding everything else.
219 */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000220 while ((opt = getopt_long(argc, argv, optstring,
221 long_options, &option_index)) != EOF) {
222 switch (opt) {
223 case 'r':
224 if (++operation_specified > 1) {
225 fprintf(stderr, "More than one operation "
226 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000227 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000228 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000229 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000230 read_it = 1;
231 break;
232 case 'w':
233 if (++operation_specified > 1) {
234 fprintf(stderr, "More than one operation "
235 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000236 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000237 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000238 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000239 write_it = 1;
240 break;
241 case 'v':
242 //FIXME: gracefully handle superfluous -v
243 if (++operation_specified > 1) {
244 fprintf(stderr, "More than one operation "
245 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000246 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000247 }
248 if (dont_verify_it) {
249 fprintf(stderr, "--verify and --noverify are"
250 "mutually exclusive. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000251 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000252 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000253 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000254 verify_it = 1;
255 break;
256 case 'n':
257 if (verify_it) {
258 fprintf(stderr, "--verify and --noverify are"
259 "mutually exclusive. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000260 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000261 }
262 dont_verify_it = 1;
263 break;
264 case 'c':
265 chip_to_probe = strdup(optarg);
266 break;
267 case 'V':
Sean Nelson51e97d72010-01-07 20:09:33 +0000268 verbose++;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000269 break;
270 case 'E':
271 if (++operation_specified > 1) {
272 fprintf(stderr, "More than one operation "
273 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000274 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000275 }
276 erase_it = 1;
277 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000278 case 'm':
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000279#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000280 tempstr = strdup(optarg);
281 lb_vendor_dev_from_string(tempstr);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000282#else
283 fprintf(stderr, "Error: Internal programmer support "
284 "was not compiled in and --mainboard only\n"
285 "applies to the internal programmer. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000286 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000287#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000288 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000289 case 'f':
290 force = 1;
291 break;
292 case 'l':
293 tempstr = strdup(optarg);
294 if (read_romlayout(tempstr))
Uwe Hermann2db77a02010-06-04 17:07:39 +0000295 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000296 break;
297 case 'i':
298 tempstr = strdup(optarg);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000299 if (register_include_arg(tempstr))
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000300 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000301 break;
302 case 'L':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000303 if (++operation_specified > 1) {
304 fprintf(stderr, "More than one operation "
305 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000306 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000307 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000308 list_supported = 1;
309 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000310 case 'z':
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000311#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000312 if (++operation_specified > 1) {
313 fprintf(stderr, "More than one operation "
314 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000315 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000316 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000317 list_supported_wiki = 1;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000318#else
319 fprintf(stderr, "Error: Wiki output was not compiled "
320 "in. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000321 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000322#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000323 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000324 case 'p':
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000325 if (prog != PROGRAMMER_INVALID) {
326 fprintf(stderr, "Error: --programmer specified "
327 "more than once. You can separate "
328 "multiple\nparameters for a programmer "
329 "with \",\". Please see the man page "
330 "for details.\n");
331 cli_classic_abort_usage();
332 }
333 for (prog = 0; prog < PROGRAMMER_INVALID; prog++) {
334 name = programmer_table[prog].name;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000335 namelen = strlen(name);
336 if (strncmp(optarg, name, namelen) == 0) {
337 switch (optarg[namelen]) {
338 case ':':
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000339 pparam = strdup(optarg + namelen + 1);
340 if (!strlen(pparam)) {
341 free(pparam);
342 pparam = NULL;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000343 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000344 break;
345 case '\0':
346 break;
347 default:
348 /* The continue refers to the
349 * for loop. It is here to be
350 * able to differentiate between
351 * foo and foobar.
352 */
353 continue;
354 }
355 break;
356 }
357 }
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000358 if (prog == PROGRAMMER_INVALID) {
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000359 fprintf(stderr, "Error: Unknown programmer "
360 "%s.\n", optarg);
Uwe Hermann2db77a02010-06-04 17:07:39 +0000361 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000362 }
363 break;
364 case 'R':
365 /* print_version() is always called during startup. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000366 if (++operation_specified > 1) {
367 fprintf(stderr, "More than one operation "
368 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000369 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000370 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000371 exit(0);
372 break;
373 case 'h':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000374 if (++operation_specified > 1) {
375 fprintf(stderr, "More than one operation "
376 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000377 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000378 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000379 cli_classic_usage(argv[0]);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000380 exit(0);
381 break;
382 default:
Uwe Hermann2db77a02010-06-04 17:07:39 +0000383 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000384 break;
385 }
386 }
387
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000388 if (optind < argc) {
389 fprintf(stderr, "Error: Extra parameter found.\n");
390 cli_classic_abort_usage();
391 }
392
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000393 if (process_include_args())
394 cli_classic_abort_usage();
395
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000396 /* FIXME: Print the actions flashrom will take. */
397
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000398 if (list_supported) {
399 print_supported();
400 exit(0);
401 }
402
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000403#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000404 if (list_supported_wiki) {
405 print_supported_wiki();
406 exit(0);
407 }
408#endif
409
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000410 /* Does a chip with the requested name exist in the flashchips array? */
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000411 if (chip_to_probe) {
412 for (flash = flashchips; flash && flash->name; flash++)
413 if (!strcmp(flash->name, chip_to_probe))
414 break;
415 if (!flash || !flash->name) {
416 fprintf(stderr, "Error: Unknown chip '%s' specified.\n",
417 chip_to_probe);
418 printf("Run flashrom -L to view the hardware supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000419 "in this flashrom version.\n");
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000420 exit(1);
421 }
422 /* Clean up after the check. */
423 flash = NULL;
424 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000425
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000426 if (prog == PROGRAMMER_INVALID)
427 prog = default_programmer;
428
429#if CONFIG_INTERNAL == 1
430 if ((prog != PROGRAMMER_INTERNAL) && (lb_part || lb_vendor)) {
431 fprintf(stderr, "Error: --mainboard requires the internal "
432 "programmer. Aborting.\n");
433 cli_classic_abort_usage();
434 }
435#endif
436
Carl-Daniel Hailfinger49884202010-05-22 07:10:46 +0000437 /* FIXME: Delay calibration should happen in programmer code. */
438 myusec_calibrate_delay();
439
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000440 if (programmer_init(prog, pparam)) {
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000441 fprintf(stderr, "Error: Programmer initialization failed.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000442 ret = 1;
443 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000444 }
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000445 tempstr = flashbuses_to_text(get_buses_supported());
446 msg_pdbg("The following protocols are supported: %s.\n",
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000447 tempstr);
448 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000449
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000450 for (j = 0; j < registered_programmer_count; j++) {
451 startchip = 0;
Michael Karcher222bf102011-12-22 23:27:03 +0000452 while (chipcount < ARRAY_SIZE(flashes)) {
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000453 startchip = probe_flash(&registered_programmers[j],
Michael Karcher222bf102011-12-22 23:27:03 +0000454 startchip,
455 &flashes[chipcount], 0);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000456 if (startchip == -1)
457 break;
458 chipcount++;
459 startchip++;
460 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000461 }
462
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000463 if (chipcount > 1) {
Stefan Tauner716e0982011-07-25 20:38:52 +0000464 printf("Multiple flash chips were detected: \"%s\"",
465 flashes[0].name);
466 for (i = 1; i < chipcount; i++)
467 printf(", \"%s\"", flashes[i].name);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000468 printf("\nPlease specify which chip to use with the "
469 "-c <chipname> option.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000470 ret = 1;
471 goto out_shutdown;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000472 } else if (!chipcount) {
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000473 printf("No EEPROM/flash device found.\n");
474 if (!force || !chip_to_probe) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000475 printf("Note: flashrom can never write if the flash "
476 "chip isn't found automatically.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000477 }
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000478#if 0 // FIXME: What happens for a forced chip read if multiple compatible programmers are registered?
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000479 if (force && read_it && chip_to_probe) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000480 printf("Force read (-f -r -c) requested, pretending "
481 "the chip is there:\n");
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000482 startchip = probe_flash(0, &flashes[0], 1);
483 if (startchip == -1) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000484 printf("Probing for flash chip '%s' failed.\n",
485 chip_to_probe);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000486 ret = 1;
487 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000488 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000489 printf("Please note that forced reads most likely "
490 "contain garbage.\n");
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000491 return read_flash_to_file(&flashes[0], filename);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000492 }
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000493#endif
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000494 ret = 1;
495 goto out_shutdown;
Stefan Tauner1d947632011-09-11 22:08:58 +0000496 } else if (!chip_to_probe) {
497 /* repeat for convenience when looking at foreign logs */
498 tempstr = flashbuses_to_text(flashes[0].bustype);
499 msg_gdbg("Found %s flash chip \"%s\" (%d kB, %s).\n",
500 flashes[0].vendor, flashes[0].name,
501 flashes[0].total_size, tempstr);
502 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000503 }
504
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000505 fill_flash = &flashes[0];
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000506
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000507 check_chip_supported(fill_flash);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000508
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000509 size = fill_flash->total_size * 1024;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000510 if (check_max_decode(fill_flash->pgm->buses_supported & fill_flash->bustype, size) &&
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000511 (!force)) {
512 fprintf(stderr, "Chip is too big for this programmer "
513 "(-V gives details). Use --force to override.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000514 ret = 1;
515 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000516 }
517
518 if (!(read_it | write_it | verify_it | erase_it)) {
519 printf("No operations were specified.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000520 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000521 }
522
523 if (!filename && !erase_it) {
524 printf("Error: No filename specified.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000525 ret = 1;
526 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000527 }
528
529 /* Always verify write operations unless -n is used. */
530 if (write_it && !dont_verify_it)
531 verify_it = 1;
532
Carl-Daniel Hailfinger9ad42552010-09-15 10:20:16 +0000533 /* FIXME: We should issue an unconditional chip reset here. This can be
534 * done once we have a .reset function in struct flashchip.
535 * Give the chip time to settle.
536 */
537 programmer_delay(100000);
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000538 return doit(fill_flash, force, filename, read_it, write_it, erase_it, verify_it);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000539
540out_shutdown:
541 programmer_shutdown();
542 return ret;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000543}