blob: b4150763d0a3c06cbdf9ec16ce7e1246ce1d2637 [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"
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000109 " [-c <chipname>] [-l <file>] [-o <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 Hailfinger8841d3e2010-05-15 15:04:37 +0000131 " -f | --force force specific operations "
132 "(see man page)\n"
133 " -n | --noverify don't auto-verify\n"
134 " -l | --layout <file> read ROM layout from "
135 "<file>\n"
136 " -i | --image <name> only flash image <name> "
137 "from flash layout\n"
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000138 " -o | --output <name> log to file <name>\n"
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000139 " -L | --list-supported print supported devices\n"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000140#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000141 " -z | --list-supported-wiki print supported devices "
142 "in wiki syntax\n"
143#endif
144 " -p | --programmer <name>[:<param>] specify the programmer "
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +0000145 "device\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000146
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +0000147 list_programmers_linebreak(37, 80, 1);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000148 printf("\nYou can specify one of -h, -R, -L, "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000149#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000150 "-z, "
151#endif
152 "-E, -r, -w, -v or no operation.\n"
153 "If no operation is specified, flashrom will only probe for "
154 "flash chips.\n\n");
155}
156
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000157static void cli_classic_abort_usage(void)
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000158{
Uwe Hermann2db77a02010-06-04 17:07:39 +0000159 printf("Please run \"flashrom --help\" for usage info.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000160 exit(1);
161}
162
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000163static int check_filename(char *filename, char *type)
164{
165 if (!filename || (filename[0] == '\0')) {
166 fprintf(stderr, "Error: No %s file specified.\n", type);
167 return 1;
168 }
169 /* Not an error, but maybe the user intended to specify a CLI option instead of a file name. */
170 if (filename[0] == '-')
171 fprintf(stderr, "Warning: Supplied %s file name starts with -\n", type);
172 return 0;
173}
174
Uwe Hermann394ee782011-08-20 14:14:22 +0000175int main(int argc, char *argv[])
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000176{
177 unsigned long size;
178 /* Probe for up to three flash chips. */
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000179 const struct flashchip *flash;
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000180 struct flashctx flashes[3];
181 struct flashctx *fill_flash;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000182 const char *name;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000183 int namelen, opt, i, j;
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000184 int startchip = -1, chipcount = 0, option_index = 0, force = 0;
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000185#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000186 int list_supported_wiki = 0;
187#endif
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000188 int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
189 int dont_verify_it = 0, list_supported = 0, operation_specified = 0;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000190 enum programmer prog = PROGRAMMER_INVALID;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000191 int ret = 0;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000192
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000193 static const char optstring[] = "r:Rw:v:nVEfc:l:i:p:Lzho:";
Mathias Krausea60faab2011-01-17 07:50:42 +0000194 static const struct option long_options[] = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000195 {"read", 1, NULL, 'r'},
196 {"write", 1, NULL, 'w'},
197 {"erase", 0, NULL, 'E'},
198 {"verify", 1, NULL, 'v'},
199 {"noverify", 0, NULL, 'n'},
200 {"chip", 1, NULL, 'c'},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000201 {"verbose", 0, NULL, 'V'},
202 {"force", 0, NULL, 'f'},
203 {"layout", 1, NULL, 'l'},
204 {"image", 1, NULL, 'i'},
205 {"list-supported", 0, NULL, 'L'},
206 {"list-supported-wiki", 0, NULL, 'z'},
207 {"programmer", 1, NULL, 'p'},
208 {"help", 0, NULL, 'h'},
209 {"version", 0, NULL, 'R'},
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000210 {"output", 1, NULL, 'o'},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000211 {NULL, 0, NULL, 0},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000212 };
213
214 char *filename = NULL;
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000215 char *layoutfile = NULL;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000216 char *logfile = NULL;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000217 char *tempstr = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000218 char *pparam = NULL;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000219
220 print_version();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000221 print_banner();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000222
223 if (selfcheck())
224 exit(1);
225
226 setbuf(stdout, NULL);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000227 /* FIXME: Delay all operation_specified checks until after command
228 * line parsing to allow --help overriding everything else.
229 */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000230 while ((opt = getopt_long(argc, argv, optstring,
231 long_options, &option_index)) != EOF) {
232 switch (opt) {
233 case 'r':
234 if (++operation_specified > 1) {
235 fprintf(stderr, "More than one operation "
236 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000237 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000238 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000239 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000240 read_it = 1;
241 break;
242 case 'w':
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 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000248 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000249 write_it = 1;
250 break;
251 case 'v':
252 //FIXME: gracefully handle superfluous -v
253 if (++operation_specified > 1) {
254 fprintf(stderr, "More than one operation "
255 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000256 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000257 }
258 if (dont_verify_it) {
259 fprintf(stderr, "--verify and --noverify are"
260 "mutually exclusive. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000261 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000262 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000263 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000264 verify_it = 1;
265 break;
266 case 'n':
267 if (verify_it) {
268 fprintf(stderr, "--verify and --noverify are"
269 "mutually exclusive. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000270 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000271 }
272 dont_verify_it = 1;
273 break;
274 case 'c':
275 chip_to_probe = strdup(optarg);
276 break;
277 case 'V':
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000278 verbose_screen++;
279 if (verbose_screen > MSG_DEBUG2)
280 verbose_logfile = verbose_screen;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000281 break;
282 case 'E':
283 if (++operation_specified > 1) {
284 fprintf(stderr, "More than one operation "
285 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000286 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000287 }
288 erase_it = 1;
289 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000290 case 'f':
291 force = 1;
292 break;
293 case 'l':
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000294 if (layoutfile) {
295 fprintf(stderr, "Error: --layout specified "
296 "more than once. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000297 cli_classic_abort_usage();
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000298 }
299 layoutfile = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000300 break;
301 case 'i':
302 tempstr = strdup(optarg);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000303 if (register_include_arg(tempstr))
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000304 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000305 break;
306 case 'L':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000307 if (++operation_specified > 1) {
308 fprintf(stderr, "More than one operation "
309 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000310 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000311 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000312 list_supported = 1;
313 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000314 case 'z':
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000315#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000316 if (++operation_specified > 1) {
317 fprintf(stderr, "More than one operation "
318 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000319 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000320 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000321 list_supported_wiki = 1;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000322#else
323 fprintf(stderr, "Error: Wiki output was not compiled "
324 "in. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000325 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000326#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000327 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000328 case 'p':
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000329 if (prog != PROGRAMMER_INVALID) {
330 fprintf(stderr, "Error: --programmer specified "
331 "more than once. You can separate "
332 "multiple\nparameters for a programmer "
333 "with \",\". Please see the man page "
334 "for details.\n");
335 cli_classic_abort_usage();
336 }
337 for (prog = 0; prog < PROGRAMMER_INVALID; prog++) {
338 name = programmer_table[prog].name;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000339 namelen = strlen(name);
340 if (strncmp(optarg, name, namelen) == 0) {
341 switch (optarg[namelen]) {
342 case ':':
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000343 pparam = strdup(optarg + namelen + 1);
344 if (!strlen(pparam)) {
345 free(pparam);
346 pparam = NULL;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000347 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000348 break;
349 case '\0':
350 break;
351 default:
352 /* The continue refers to the
353 * for loop. It is here to be
354 * able to differentiate between
355 * foo and foobar.
356 */
357 continue;
358 }
359 break;
360 }
361 }
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000362 if (prog == PROGRAMMER_INVALID) {
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000363 fprintf(stderr, "Error: Unknown programmer "
364 "%s.\n", optarg);
Uwe Hermann2db77a02010-06-04 17:07:39 +0000365 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000366 }
367 break;
368 case 'R':
369 /* print_version() is always called during startup. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000370 if (++operation_specified > 1) {
371 fprintf(stderr, "More than one operation "
372 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000373 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000374 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000375 exit(0);
376 break;
377 case 'h':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000378 if (++operation_specified > 1) {
379 fprintf(stderr, "More than one operation "
380 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000381 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000382 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000383 cli_classic_usage(argv[0]);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000384 exit(0);
385 break;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000386 case 'o':
387#ifdef STANDALONE
388 fprintf(stderr, "Log file not supported in standalone mode. Aborting.\n");
389 cli_classic_abort_usage();
390#else /* STANDALONE */
391 logfile = strdup(optarg);
392 if (logfile[0] == '\0') {
393 fprintf(stderr, "No log filename specified.\n");
394 cli_classic_abort_usage();
395 }
396#endif /* STANDALONE */
397 break;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000398 default:
Uwe Hermann2db77a02010-06-04 17:07:39 +0000399 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000400 break;
401 }
402 }
403
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000404 if (optind < argc) {
405 fprintf(stderr, "Error: Extra parameter found.\n");
406 cli_classic_abort_usage();
407 }
408
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000409 if ((read_it | write_it | verify_it) && check_filename(filename, "image")) {
410 cli_classic_abort_usage();
411 }
412 if (layoutfile && check_filename(layoutfile, "layout")) {
413 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000414 }
415
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000416#ifndef STANDALONE
417 if (logfile && check_filename(logfile, "log"))
418 cli_classic_abort_usage();
419 if (logfile && open_logfile(logfile))
420 return 1;
421#endif /* !STANDALONE */
422
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000423#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000424 if (list_supported_wiki) {
425 print_supported_wiki();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000426 ret = 0;
427 goto out;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000428 }
429#endif
430
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000431 if (list_supported) {
432 print_supported();
433 ret = 0;
434 goto out;
435 }
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000436
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000437#ifndef STANDALONE
438 start_logging();
439#endif /* !STANDALONE */
440
441 print_buildinfo();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000442 msg_gdbg("Command line (%i args):", argc - 1);
443 for (i = 0; i < argc; i++) {
444 msg_gdbg(" %s", argv[i]);
445 }
446 msg_gdbg("\n");
447
448 if (layoutfile && read_romlayout(layoutfile)) {
449 ret = 1;
450 goto out;
451 }
452 if (process_include_args()) {
453 ret = 1;
454 goto out;
455 }
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000456 /* Does a chip with the requested name exist in the flashchips array? */
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000457 if (chip_to_probe) {
458 for (flash = flashchips; flash && flash->name; flash++)
459 if (!strcmp(flash->name, chip_to_probe))
460 break;
461 if (!flash || !flash->name) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000462 msg_cerr("Error: Unknown chip '%s' specified.\n", chip_to_probe);
463 msg_gerr("Run flashrom -L to view the hardware supported in this flashrom version.\n");
464 ret = 1;
465 goto out;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000466 }
467 /* Clean up after the check. */
468 flash = NULL;
469 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000470
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000471 if (prog == PROGRAMMER_INVALID)
472 prog = default_programmer;
473
Carl-Daniel Hailfinger49884202010-05-22 07:10:46 +0000474 /* FIXME: Delay calibration should happen in programmer code. */
475 myusec_calibrate_delay();
476
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000477 if (programmer_init(prog, pparam)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000478 msg_perr("Error: Programmer initialization failed.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000479 ret = 1;
480 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000481 }
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000482 tempstr = flashbuses_to_text(get_buses_supported());
483 msg_pdbg("The following protocols are supported: %s.\n",
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000484 tempstr);
485 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000486
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000487 for (j = 0; j < registered_programmer_count; j++) {
488 startchip = 0;
Michael Karcher222bf102011-12-22 23:27:03 +0000489 while (chipcount < ARRAY_SIZE(flashes)) {
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000490 startchip = probe_flash(&registered_programmers[j],
Michael Karcher222bf102011-12-22 23:27:03 +0000491 startchip,
492 &flashes[chipcount], 0);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000493 if (startchip == -1)
494 break;
495 chipcount++;
496 startchip++;
497 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000498 }
499
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000500 if (chipcount > 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000501 msg_cinfo("Multiple flash chips were detected: \"%s\"", flashes[0].name);
Stefan Tauner716e0982011-07-25 20:38:52 +0000502 for (i = 1; i < chipcount; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000503 msg_cinfo(", \"%s\"", flashes[i].name);
504 msg_cinfo("\nPlease specify which chip to use with the -c <chipname> option.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000505 ret = 1;
506 goto out_shutdown;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000507 } else if (!chipcount) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000508 msg_cinfo("No EEPROM/flash device found.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000509 if (!force || !chip_to_probe) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000510 msg_cinfo("Note: flashrom can never write if the flash chip isn't found "
511 "automatically.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000512 }
513 if (force && read_it && chip_to_probe) {
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000514 struct registered_programmer *pgm;
515 int compatible_programmers = 0;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000516 msg_cinfo("Force read (-f -r -c) requested, pretending the chip is there:\n");
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000517 /* This loop just counts compatible controllers. */
518 for (j = 0; j < registered_programmer_count; j++) {
519 pgm = &registered_programmers[j];
520 if (pgm->buses_supported & flashes[0].bustype)
521 compatible_programmers++;
522 }
523 if (compatible_programmers > 1)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000524 msg_cinfo("More than one compatible controller found for the requested flash "
525 "chip, using the first one.\n");
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000526 for (j = 0; j < registered_programmer_count; j++) {
527 pgm = &registered_programmers[j];
528 startchip = probe_flash(pgm, 0, &flashes[0], 1);
529 if (startchip != -1)
530 break;
531 }
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000532 if (startchip == -1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000533 msg_cinfo("Probing for flash chip '%s' failed.\n", chip_to_probe);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000534 ret = 1;
535 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000536 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000537 msg_cinfo("Please note that forced reads most likely contain garbage.\n");
538 ret = read_flash_to_file(&flashes[0], filename);
539 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000540 }
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000541 ret = 1;
542 goto out_shutdown;
Stefan Tauner1d947632011-09-11 22:08:58 +0000543 } else if (!chip_to_probe) {
544 /* repeat for convenience when looking at foreign logs */
545 tempstr = flashbuses_to_text(flashes[0].bustype);
546 msg_gdbg("Found %s flash chip \"%s\" (%d kB, %s).\n",
547 flashes[0].vendor, flashes[0].name,
548 flashes[0].total_size, tempstr);
549 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000550 }
551
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000552 fill_flash = &flashes[0];
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000553
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000554 check_chip_supported(fill_flash);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000555
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000556 size = fill_flash->total_size * 1024;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000557 if (check_max_decode(fill_flash->pgm->buses_supported & fill_flash->bustype, size) && (!force)) {
558 msg_cerr("Chip is too big for this programmer (-V gives details). Use --force to override.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000559 ret = 1;
560 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000561 }
562
563 if (!(read_it | write_it | verify_it | erase_it)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000564 msg_ginfo("No operations were specified.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000565 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000566 }
567
568 /* Always verify write operations unless -n is used. */
569 if (write_it && !dont_verify_it)
570 verify_it = 1;
571
Carl-Daniel Hailfinger9ad42552010-09-15 10:20:16 +0000572 /* FIXME: We should issue an unconditional chip reset here. This can be
573 * done once we have a .reset function in struct flashchip.
574 * Give the chip time to settle.
575 */
576 programmer_delay(100000);
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000577 ret |= doit(fill_flash, force, filename, read_it, write_it, erase_it, verify_it);
578 /* Note: doit() already calls programmer_shutdown(). */
579 goto out;
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000580
581out_shutdown:
582 programmer_shutdown();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000583out:
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000584#ifndef STANDALONE
585 ret |= close_logfile();
586#endif /* !STANDALONE */
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000587 return ret;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000588}