blob: edcef8e2a513539cc8f8d513f370618704ad98d4 [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 Hailfinger2d927fb2012-01-04 00:48:27 +0000109 " [-c <chipname>] [-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 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"
138 " -L | --list-supported print supported devices\n"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000139#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000140 " -z | --list-supported-wiki print supported devices "
141 "in wiki syntax\n"
142#endif
143 " -p | --programmer <name>[:<param>] specify the programmer "
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +0000144 "device\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000145
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +0000146 list_programmers_linebreak(37, 80, 1);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000147 printf("\nYou can specify one of -h, -R, -L, "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000148#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000149 "-z, "
150#endif
151 "-E, -r, -w, -v or no operation.\n"
152 "If no operation is specified, flashrom will only probe for "
153 "flash chips.\n\n");
154}
155
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000156static void cli_classic_abort_usage(void)
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000157{
Uwe Hermann2db77a02010-06-04 17:07:39 +0000158 printf("Please run \"flashrom --help\" for usage info.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000159 exit(1);
160}
161
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000162static int check_filename(char *filename, char *type)
163{
164 if (!filename || (filename[0] == '\0')) {
165 fprintf(stderr, "Error: No %s file specified.\n", type);
166 return 1;
167 }
168 /* Not an error, but maybe the user intended to specify a CLI option instead of a file name. */
169 if (filename[0] == '-')
170 fprintf(stderr, "Warning: Supplied %s file name starts with -\n", type);
171 return 0;
172}
173
Uwe Hermann394ee782011-08-20 14:14:22 +0000174int main(int argc, char *argv[])
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000175{
176 unsigned long size;
177 /* Probe for up to three flash chips. */
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000178 const struct flashchip *flash;
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000179 struct flashctx flashes[3];
180 struct flashctx *fill_flash;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000181 const char *name;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000182 int namelen, opt, i, j;
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000183 int startchip = -1, chipcount = 0, option_index = 0, force = 0;
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000184#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000185 int list_supported_wiki = 0;
186#endif
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000187 int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
188 int dont_verify_it = 0, list_supported = 0, operation_specified = 0;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000189 enum programmer prog = PROGRAMMER_INVALID;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000190 int ret = 0;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000191
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000192 static const char optstring[] = "r:Rw:v:nVEfc:l:i:p:Lzh";
Mathias Krausea60faab2011-01-17 07:50:42 +0000193 static const struct option long_options[] = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000194 {"read", 1, NULL, 'r'},
195 {"write", 1, NULL, 'w'},
196 {"erase", 0, NULL, 'E'},
197 {"verify", 1, NULL, 'v'},
198 {"noverify", 0, NULL, 'n'},
199 {"chip", 1, NULL, 'c'},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000200 {"verbose", 0, NULL, 'V'},
201 {"force", 0, NULL, 'f'},
202 {"layout", 1, NULL, 'l'},
203 {"image", 1, NULL, 'i'},
204 {"list-supported", 0, NULL, 'L'},
205 {"list-supported-wiki", 0, NULL, 'z'},
206 {"programmer", 1, NULL, 'p'},
207 {"help", 0, NULL, 'h'},
208 {"version", 0, NULL, 'R'},
209 {NULL, 0, NULL, 0},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000210 };
211
212 char *filename = NULL;
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000213 char *layoutfile = NULL;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000214 char *tempstr = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000215 char *pparam = NULL;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000216
217 print_version();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000218 print_banner();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000219
220 if (selfcheck())
221 exit(1);
222
223 setbuf(stdout, NULL);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000224 /* FIXME: Delay all operation_specified checks until after command
225 * line parsing to allow --help overriding everything else.
226 */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000227 while ((opt = getopt_long(argc, argv, optstring,
228 long_options, &option_index)) != EOF) {
229 switch (opt) {
230 case 'r':
231 if (++operation_specified > 1) {
232 fprintf(stderr, "More than one operation "
233 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000234 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000235 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000236 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000237 read_it = 1;
238 break;
239 case 'w':
240 if (++operation_specified > 1) {
241 fprintf(stderr, "More than one operation "
242 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000243 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000244 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000245 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000246 write_it = 1;
247 break;
248 case 'v':
249 //FIXME: gracefully handle superfluous -v
250 if (++operation_specified > 1) {
251 fprintf(stderr, "More than one operation "
252 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000253 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000254 }
255 if (dont_verify_it) {
256 fprintf(stderr, "--verify and --noverify are"
257 "mutually exclusive. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000258 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000259 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000260 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000261 verify_it = 1;
262 break;
263 case 'n':
264 if (verify_it) {
265 fprintf(stderr, "--verify and --noverify are"
266 "mutually exclusive. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000267 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000268 }
269 dont_verify_it = 1;
270 break;
271 case 'c':
272 chip_to_probe = strdup(optarg);
273 break;
274 case 'V':
Sean Nelson51e97d72010-01-07 20:09:33 +0000275 verbose++;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000276 break;
277 case 'E':
278 if (++operation_specified > 1) {
279 fprintf(stderr, "More than one operation "
280 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000281 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000282 }
283 erase_it = 1;
284 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000285 case 'f':
286 force = 1;
287 break;
288 case 'l':
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000289 if (layoutfile) {
290 fprintf(stderr, "Error: --layout specified "
291 "more than once. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000292 cli_classic_abort_usage();
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000293 }
294 layoutfile = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000295 break;
296 case 'i':
297 tempstr = strdup(optarg);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000298 if (register_include_arg(tempstr))
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000299 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000300 break;
301 case 'L':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000302 if (++operation_specified > 1) {
303 fprintf(stderr, "More than one operation "
304 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000305 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000306 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000307 list_supported = 1;
308 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000309 case 'z':
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000310#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000311 if (++operation_specified > 1) {
312 fprintf(stderr, "More than one operation "
313 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000314 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000315 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000316 list_supported_wiki = 1;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000317#else
318 fprintf(stderr, "Error: Wiki output was not compiled "
319 "in. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000320 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000321#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000322 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000323 case 'p':
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000324 if (prog != PROGRAMMER_INVALID) {
325 fprintf(stderr, "Error: --programmer specified "
326 "more than once. You can separate "
327 "multiple\nparameters for a programmer "
328 "with \",\". Please see the man page "
329 "for details.\n");
330 cli_classic_abort_usage();
331 }
332 for (prog = 0; prog < PROGRAMMER_INVALID; prog++) {
333 name = programmer_table[prog].name;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000334 namelen = strlen(name);
335 if (strncmp(optarg, name, namelen) == 0) {
336 switch (optarg[namelen]) {
337 case ':':
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000338 pparam = strdup(optarg + namelen + 1);
339 if (!strlen(pparam)) {
340 free(pparam);
341 pparam = NULL;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000342 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000343 break;
344 case '\0':
345 break;
346 default:
347 /* The continue refers to the
348 * for loop. It is here to be
349 * able to differentiate between
350 * foo and foobar.
351 */
352 continue;
353 }
354 break;
355 }
356 }
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000357 if (prog == PROGRAMMER_INVALID) {
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000358 fprintf(stderr, "Error: Unknown programmer "
359 "%s.\n", optarg);
Uwe Hermann2db77a02010-06-04 17:07:39 +0000360 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000361 }
362 break;
363 case 'R':
364 /* print_version() is always called during startup. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000365 if (++operation_specified > 1) {
366 fprintf(stderr, "More than one operation "
367 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000368 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000369 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000370 exit(0);
371 break;
372 case 'h':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000373 if (++operation_specified > 1) {
374 fprintf(stderr, "More than one operation "
375 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000376 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000377 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000378 cli_classic_usage(argv[0]);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000379 exit(0);
380 break;
381 default:
Uwe Hermann2db77a02010-06-04 17:07:39 +0000382 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000383 break;
384 }
385 }
386
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000387 if (optind < argc) {
388 fprintf(stderr, "Error: Extra parameter found.\n");
389 cli_classic_abort_usage();
390 }
391
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000392 if ((read_it | write_it | verify_it) && check_filename(filename, "image")) {
393 cli_classic_abort_usage();
394 }
395 if (layoutfile && check_filename(layoutfile, "layout")) {
396 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000397 }
398
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000399#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000400 if (list_supported_wiki) {
401 print_supported_wiki();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000402 ret = 0;
403 goto out;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000404 }
405#endif
406
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000407 if (list_supported) {
408 print_supported();
409 ret = 0;
410 goto out;
411 }
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000412
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000413 msg_gdbg("Command line (%i args):", argc - 1);
414 for (i = 0; i < argc; i++) {
415 msg_gdbg(" %s", argv[i]);
416 }
417 msg_gdbg("\n");
418
419 if (layoutfile && read_romlayout(layoutfile)) {
420 ret = 1;
421 goto out;
422 }
423 if (process_include_args()) {
424 ret = 1;
425 goto out;
426 }
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000427 /* Does a chip with the requested name exist in the flashchips array? */
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000428 if (chip_to_probe) {
429 for (flash = flashchips; flash && flash->name; flash++)
430 if (!strcmp(flash->name, chip_to_probe))
431 break;
432 if (!flash || !flash->name) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000433 msg_cerr("Error: Unknown chip '%s' specified.\n", chip_to_probe);
434 msg_gerr("Run flashrom -L to view the hardware supported in this flashrom version.\n");
435 ret = 1;
436 goto out;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000437 }
438 /* Clean up after the check. */
439 flash = NULL;
440 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000441
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000442 if (prog == PROGRAMMER_INVALID)
443 prog = default_programmer;
444
Carl-Daniel Hailfinger49884202010-05-22 07:10:46 +0000445 /* FIXME: Delay calibration should happen in programmer code. */
446 myusec_calibrate_delay();
447
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000448 if (programmer_init(prog, pparam)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000449 msg_perr("Error: Programmer initialization failed.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000450 ret = 1;
451 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000452 }
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000453 tempstr = flashbuses_to_text(get_buses_supported());
454 msg_pdbg("The following protocols are supported: %s.\n",
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000455 tempstr);
456 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000457
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000458 for (j = 0; j < registered_programmer_count; j++) {
459 startchip = 0;
Michael Karcher222bf102011-12-22 23:27:03 +0000460 while (chipcount < ARRAY_SIZE(flashes)) {
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000461 startchip = probe_flash(&registered_programmers[j],
Michael Karcher222bf102011-12-22 23:27:03 +0000462 startchip,
463 &flashes[chipcount], 0);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000464 if (startchip == -1)
465 break;
466 chipcount++;
467 startchip++;
468 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000469 }
470
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000471 if (chipcount > 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000472 msg_cinfo("Multiple flash chips were detected: \"%s\"", flashes[0].name);
Stefan Tauner716e0982011-07-25 20:38:52 +0000473 for (i = 1; i < chipcount; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000474 msg_cinfo(", \"%s\"", flashes[i].name);
475 msg_cinfo("\nPlease specify which chip to use with the -c <chipname> option.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000476 ret = 1;
477 goto out_shutdown;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000478 } else if (!chipcount) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000479 msg_cinfo("No EEPROM/flash device found.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000480 if (!force || !chip_to_probe) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000481 msg_cinfo("Note: flashrom can never write if the flash chip isn't found "
482 "automatically.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000483 }
484 if (force && read_it && chip_to_probe) {
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000485 struct registered_programmer *pgm;
486 int compatible_programmers = 0;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000487 msg_cinfo("Force read (-f -r -c) requested, pretending the chip is there:\n");
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000488 /* This loop just counts compatible controllers. */
489 for (j = 0; j < registered_programmer_count; j++) {
490 pgm = &registered_programmers[j];
491 if (pgm->buses_supported & flashes[0].bustype)
492 compatible_programmers++;
493 }
494 if (compatible_programmers > 1)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000495 msg_cinfo("More than one compatible controller found for the requested flash "
496 "chip, using the first one.\n");
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000497 for (j = 0; j < registered_programmer_count; j++) {
498 pgm = &registered_programmers[j];
499 startchip = probe_flash(pgm, 0, &flashes[0], 1);
500 if (startchip != -1)
501 break;
502 }
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000503 if (startchip == -1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000504 msg_cinfo("Probing for flash chip '%s' failed.\n", chip_to_probe);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000505 ret = 1;
506 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000507 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000508 msg_cinfo("Please note that forced reads most likely contain garbage.\n");
509 ret = read_flash_to_file(&flashes[0], filename);
510 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000511 }
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000512 ret = 1;
513 goto out_shutdown;
Stefan Tauner1d947632011-09-11 22:08:58 +0000514 } else if (!chip_to_probe) {
515 /* repeat for convenience when looking at foreign logs */
516 tempstr = flashbuses_to_text(flashes[0].bustype);
517 msg_gdbg("Found %s flash chip \"%s\" (%d kB, %s).\n",
518 flashes[0].vendor, flashes[0].name,
519 flashes[0].total_size, tempstr);
520 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000521 }
522
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000523 fill_flash = &flashes[0];
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000524
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000525 check_chip_supported(fill_flash);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000526
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000527 size = fill_flash->total_size * 1024;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000528 if (check_max_decode(fill_flash->pgm->buses_supported & fill_flash->bustype, size) && (!force)) {
529 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 +0000530 ret = 1;
531 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000532 }
533
534 if (!(read_it | write_it | verify_it | erase_it)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000535 msg_ginfo("No operations were specified.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000536 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000537 }
538
539 /* Always verify write operations unless -n is used. */
540 if (write_it && !dont_verify_it)
541 verify_it = 1;
542
Carl-Daniel Hailfinger9ad42552010-09-15 10:20:16 +0000543 /* FIXME: We should issue an unconditional chip reset here. This can be
544 * done once we have a .reset function in struct flashchip.
545 * Give the chip time to settle.
546 */
547 programmer_delay(100000);
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000548 ret |= doit(fill_flash, force, filename, read_it, write_it, erase_it, verify_it);
549 /* Note: doit() already calls programmer_shutdown(). */
550 goto out;
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000551
552out_shutdown:
553 programmer_shutdown();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000554out:
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000555 return ret;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000556}