blob: 7e3dfd5b563b9d082c358258c0a7bb4e3ac9c7cb [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"
Nico Huber18781102012-12-10 13:34:12 +000033#include "libflashrom.h"
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000034
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000035static void cli_classic_usage(const char *name)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000036{
Stefan Taunerb226cb12012-11-24 18:59:39 +000037 printf("Please note that the command line interface for flashrom has changed between\n"
38 "0.9.5 and 0.9.6 and will change again before flashrom 1.0.\n\n");
39
40 printf("Usage: %s [-h|-R|-L|"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000041#if CONFIG_PRINT_WIKI == 1
Stefan Taunerb226cb12012-11-24 18:59:39 +000042 "-z|"
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000043#endif
Stefan Taunerb226cb12012-11-24 18:59:39 +000044 "-p <programmername>[:<parameters>] [-c <chipname>]\n"
Nico Huber99d15952016-05-02 16:54:24 +020045 "[-E|(-r|-w|-v) <file>] [-l <layoutfile> [-i <imagename>]...] [-n] [-N] [-f]]\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000046 "[-V[V[V]]] [-o <logfile>]\n\n", name);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000047
Stefan Taunerb226cb12012-11-24 18:59:39 +000048 printf(" -h | --help print this help text\n"
49 " -R | --version print version (release)\n"
50 " -r | --read <file> read flash and save to <file>\n"
51 " -w | --write <file> write <file> to flash\n"
52 " -v | --verify <file> verify flash against <file>\n"
53 " -E | --erase erase flash memory\n"
54 " -V | --verbose more verbose output\n"
55 " -c | --chip <chipname> probe only for specified flash chip\n"
56 " -f | --force force specific operations (see man page)\n"
57 " -n | --noverify don't auto-verify\n"
Nico Huber99d15952016-05-02 16:54:24 +020058 " -N | --noverify-all verify included regions only (cf. -i)\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000059 " -l | --layout <layoutfile> read ROM layout from <layoutfile>\n"
60 " -i | --image <name> only flash image <name> from flash layout\n"
61 " -o | --output <logfile> log output to <logfile>\n"
62 " -L | --list-supported print supported devices\n"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000063#if CONFIG_PRINT_WIKI == 1
Stefan Taunerb226cb12012-11-24 18:59:39 +000064 " -z | --list-supported-wiki print supported devices in wiki syntax\n"
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000065#endif
Stefan Taunerb226cb12012-11-24 18:59:39 +000066 " -p | --programmer <name>[:<param>] specify the programmer device. One of\n");
67 list_programmers_linebreak(4, 80, 0);
68 printf(".\n\nYou can specify one of -h, -R, -L, "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000069#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000070 "-z, "
71#endif
72 "-E, -r, -w, -v or no operation.\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +000073 "If no operation is specified, flashrom will only probe for flash chips.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000074}
75
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000076static void cli_classic_abort_usage(void)
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000077{
Uwe Hermann2db77a02010-06-04 17:07:39 +000078 printf("Please run \"flashrom --help\" for usage info.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000079 exit(1);
80}
81
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +000082static int check_filename(char *filename, char *type)
83{
84 if (!filename || (filename[0] == '\0')) {
85 fprintf(stderr, "Error: No %s file specified.\n", type);
86 return 1;
87 }
88 /* Not an error, but maybe the user intended to specify a CLI option instead of a file name. */
89 if (filename[0] == '-')
90 fprintf(stderr, "Warning: Supplied %s file name starts with -\n", type);
91 return 0;
92}
93
Uwe Hermann394ee782011-08-20 14:14:22 +000094int main(int argc, char *argv[])
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000095{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +000096 const struct flashchip *chip = NULL;
Jernej Škrabece814a9b2014-12-12 00:32:03 +000097 /* Probe for up to eight flash chips. */
98 struct flashctx flashes[8] = {{0}};
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000099 struct flashctx *fill_flash;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000100 const char *name;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000101 int namelen, opt, i, j;
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000102 int startchip = -1, chipcount = 0, option_index = 0, force = 0;
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000103#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000104 int list_supported_wiki = 0;
105#endif
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000106 int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
Nico Huber99d15952016-05-02 16:54:24 +0200107 int dont_verify_it = 0, dont_verify_all = 0, list_supported = 0, operation_specified = 0;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000108 enum programmer prog = PROGRAMMER_INVALID;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000109 int ret = 0;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000110
Nico Huber99d15952016-05-02 16:54:24 +0200111 static const char optstring[] = "r:Rw:v:nNVEfc:l:i:p:Lzho:";
Mathias Krausea60faab2011-01-17 07:50:42 +0000112 static const struct option long_options[] = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000113 {"read", 1, NULL, 'r'},
114 {"write", 1, NULL, 'w'},
115 {"erase", 0, NULL, 'E'},
116 {"verify", 1, NULL, 'v'},
117 {"noverify", 0, NULL, 'n'},
Nico Huber99d15952016-05-02 16:54:24 +0200118 {"noverify-all", 0, NULL, 'N'},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000119 {"chip", 1, NULL, 'c'},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000120 {"verbose", 0, NULL, 'V'},
121 {"force", 0, NULL, 'f'},
122 {"layout", 1, NULL, 'l'},
123 {"image", 1, NULL, 'i'},
124 {"list-supported", 0, NULL, 'L'},
125 {"list-supported-wiki", 0, NULL, 'z'},
126 {"programmer", 1, NULL, 'p'},
127 {"help", 0, NULL, 'h'},
128 {"version", 0, NULL, 'R'},
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000129 {"output", 1, NULL, 'o'},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000130 {NULL, 0, NULL, 0},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000131 };
132
133 char *filename = NULL;
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000134 char *layoutfile = NULL;
Stefan Taunerb8911d62012-12-26 07:55:00 +0000135#ifndef STANDALONE
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000136 char *logfile = NULL;
Stefan Taunerb8911d62012-12-26 07:55:00 +0000137#endif /* !STANDALONE */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000138 char *tempstr = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000139 char *pparam = NULL;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000140
Nico Huber18781102012-12-10 13:34:12 +0000141 flashrom_set_log_callback((flashrom_log_callback *)&flashrom_print_cb);
142
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000143 print_version();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000144 print_banner();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000145
146 if (selfcheck())
147 exit(1);
148
149 setbuf(stdout, NULL);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000150 /* FIXME: Delay all operation_specified checks until after command
151 * line parsing to allow --help overriding everything else.
152 */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000153 while ((opt = getopt_long(argc, argv, optstring,
154 long_options, &option_index)) != EOF) {
155 switch (opt) {
156 case 'r':
157 if (++operation_specified > 1) {
158 fprintf(stderr, "More than one operation "
159 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000160 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000161 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000162 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000163 read_it = 1;
164 break;
165 case 'w':
166 if (++operation_specified > 1) {
167 fprintf(stderr, "More than one operation "
168 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000169 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000170 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000171 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000172 write_it = 1;
173 break;
174 case 'v':
175 //FIXME: gracefully handle superfluous -v
176 if (++operation_specified > 1) {
177 fprintf(stderr, "More than one operation "
178 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000179 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000180 }
181 if (dont_verify_it) {
Stefan Taunerc4f44df2013-08-12 22:58:43 +0000182 fprintf(stderr, "--verify and --noverify are mutually exclusive. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000183 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000184 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000185 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000186 verify_it = 1;
187 break;
188 case 'n':
189 if (verify_it) {
Stefan Taunerc4f44df2013-08-12 22:58:43 +0000190 fprintf(stderr, "--verify and --noverify are mutually exclusive. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000191 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000192 }
193 dont_verify_it = 1;
194 break;
Nico Huber99d15952016-05-02 16:54:24 +0200195 case 'N':
196 dont_verify_all = 1;
197 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000198 case 'c':
199 chip_to_probe = strdup(optarg);
200 break;
201 case 'V':
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000202 verbose_screen++;
203 if (verbose_screen > MSG_DEBUG2)
204 verbose_logfile = verbose_screen;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000205 break;
206 case 'E':
207 if (++operation_specified > 1) {
208 fprintf(stderr, "More than one operation "
209 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000210 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000211 }
212 erase_it = 1;
213 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000214 case 'f':
215 force = 1;
216 break;
217 case 'l':
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000218 if (layoutfile) {
219 fprintf(stderr, "Error: --layout specified "
220 "more than once. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000221 cli_classic_abort_usage();
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000222 }
223 layoutfile = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000224 break;
225 case 'i':
226 tempstr = strdup(optarg);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000227 if (register_include_arg(tempstr)) {
228 free(tempstr);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000229 cli_classic_abort_usage();
Stefan Taunerb8911d62012-12-26 07:55:00 +0000230 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000231 break;
232 case 'L':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000233 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 Hailfinger8841d3e2010-05-15 15:04:37 +0000237 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000238 list_supported = 1;
239 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000240 case 'z':
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000241#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000242 if (++operation_specified > 1) {
243 fprintf(stderr, "More than one operation "
244 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000245 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000246 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000247 list_supported_wiki = 1;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000248#else
249 fprintf(stderr, "Error: Wiki output was not compiled "
250 "in. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000251 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000252#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000253 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000254 case 'p':
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000255 if (prog != PROGRAMMER_INVALID) {
256 fprintf(stderr, "Error: --programmer specified "
257 "more than once. You can separate "
258 "multiple\nparameters for a programmer "
259 "with \",\". Please see the man page "
260 "for details.\n");
261 cli_classic_abort_usage();
262 }
263 for (prog = 0; prog < PROGRAMMER_INVALID; prog++) {
264 name = programmer_table[prog].name;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000265 namelen = strlen(name);
266 if (strncmp(optarg, name, namelen) == 0) {
267 switch (optarg[namelen]) {
268 case ':':
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000269 pparam = strdup(optarg + namelen + 1);
270 if (!strlen(pparam)) {
271 free(pparam);
272 pparam = NULL;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000273 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000274 break;
275 case '\0':
276 break;
277 default:
278 /* The continue refers to the
279 * for loop. It is here to be
280 * able to differentiate between
281 * foo and foobar.
282 */
283 continue;
284 }
285 break;
286 }
287 }
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000288 if (prog == PROGRAMMER_INVALID) {
Carl-Daniel Hailfinger4e3391f2012-07-22 12:01:43 +0000289 fprintf(stderr, "Error: Unknown programmer \"%s\". Valid choices are:\n",
290 optarg);
291 list_programmers_linebreak(0, 80, 0);
Stefan Taunerb226cb12012-11-24 18:59:39 +0000292 msg_ginfo(".\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000293 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000294 }
295 break;
296 case 'R':
297 /* print_version() is always called during startup. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000298 if (++operation_specified > 1) {
299 fprintf(stderr, "More than one operation "
300 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000301 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000302 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000303 exit(0);
304 break;
305 case 'h':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000306 if (++operation_specified > 1) {
307 fprintf(stderr, "More than one operation "
308 "specified. Aborting.\n");
Uwe Hermann2db77a02010-06-04 17:07:39 +0000309 cli_classic_abort_usage();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000310 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000311 cli_classic_usage(argv[0]);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000312 exit(0);
313 break;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000314 case 'o':
315#ifdef STANDALONE
316 fprintf(stderr, "Log file not supported in standalone mode. Aborting.\n");
317 cli_classic_abort_usage();
318#else /* STANDALONE */
319 logfile = strdup(optarg);
320 if (logfile[0] == '\0') {
321 fprintf(stderr, "No log filename specified.\n");
322 cli_classic_abort_usage();
323 }
324#endif /* STANDALONE */
325 break;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000326 default:
Uwe Hermann2db77a02010-06-04 17:07:39 +0000327 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000328 break;
329 }
330 }
331
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000332 if (optind < argc) {
333 fprintf(stderr, "Error: Extra parameter found.\n");
334 cli_classic_abort_usage();
335 }
336
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000337 if ((read_it | write_it | verify_it) && check_filename(filename, "image")) {
338 cli_classic_abort_usage();
339 }
340 if (layoutfile && check_filename(layoutfile, "layout")) {
341 cli_classic_abort_usage();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000342 }
343
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000344#ifndef STANDALONE
345 if (logfile && check_filename(logfile, "log"))
346 cli_classic_abort_usage();
347 if (logfile && open_logfile(logfile))
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000348 cli_classic_abort_usage();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000349#endif /* !STANDALONE */
350
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000351#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000352 if (list_supported_wiki) {
353 print_supported_wiki();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000354 goto out;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000355 }
356#endif
357
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000358 if (list_supported) {
Niklas Söderlundede2fa42012-10-23 13:06:46 +0000359 if (print_supported())
360 ret = 1;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000361 goto out;
362 }
Carl-Daniel Hailfinger46284452012-01-11 02:10:11 +0000363
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000364#ifndef STANDALONE
365 start_logging();
366#endif /* !STANDALONE */
367
368 print_buildinfo();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000369 msg_gdbg("Command line (%i args):", argc - 1);
370 for (i = 0; i < argc; i++) {
371 msg_gdbg(" %s", argv[i]);
372 }
373 msg_gdbg("\n");
374
375 if (layoutfile && read_romlayout(layoutfile)) {
376 ret = 1;
377 goto out;
378 }
379 if (process_include_args()) {
380 ret = 1;
381 goto out;
382 }
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000383 /* Does a chip with the requested name exist in the flashchips array? */
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000384 if (chip_to_probe) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000385 for (chip = flashchips; chip && chip->name; chip++)
386 if (!strcmp(chip->name, chip_to_probe))
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000387 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000388 if (!chip || !chip->name) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000389 msg_cerr("Error: Unknown chip '%s' specified.\n", chip_to_probe);
390 msg_gerr("Run flashrom -L to view the hardware supported in this flashrom version.\n");
391 ret = 1;
392 goto out;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000393 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000394 /* Keep chip around for later usage in case a forced read is requested. */
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000395 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000396
Carl-Daniel Hailfinger4e3391f2012-07-22 12:01:43 +0000397 if (prog == PROGRAMMER_INVALID) {
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000398 if (CONFIG_DEFAULT_PROGRAMMER != PROGRAMMER_INVALID) {
399 prog = CONFIG_DEFAULT_PROGRAMMER;
Stefan Tauner265fcac2014-06-02 00:12:23 +0000400 /* We need to strdup here because we free(pparam) unconditionally later. */
401 pparam = strdup(CONFIG_DEFAULT_PROGRAMMER_ARGS);
402 msg_pinfo("Using default programmer \"%s\" with arguments \"%s\".\n",
403 programmer_table[CONFIG_DEFAULT_PROGRAMMER].name, pparam);
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000404 } else {
405 msg_perr("Please select a programmer with the --programmer parameter.\n"
Stefan Taunerb226cb12012-11-24 18:59:39 +0000406 "Previously this was not necessary because there was a default set.\n"
407#if CONFIG_INTERNAL == 1
408 "To choose the mainboard of this computer use 'internal'. "
409#endif
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000410 "Valid choices are:\n");
411 list_programmers_linebreak(0, 80, 0);
Stefan Taunerb226cb12012-11-24 18:59:39 +0000412 msg_ginfo(".\n");
Stefan Taunerfd0d4132012-09-25 21:24:55 +0000413 ret = 1;
414 goto out;
415 }
Carl-Daniel Hailfinger4e3391f2012-07-22 12:01:43 +0000416 }
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000417
Carl-Daniel Hailfinger49884202010-05-22 07:10:46 +0000418 /* FIXME: Delay calibration should happen in programmer code. */
419 myusec_calibrate_delay();
420
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000421 if (programmer_init(prog, pparam)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000422 msg_perr("Error: Programmer initialization failed.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000423 ret = 1;
424 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000425 }
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000426 tempstr = flashbuses_to_text(get_buses_supported());
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000427 msg_pdbg("The following protocols are supported: %s.\n", tempstr);
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000428 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000429
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000430 for (j = 0; j < registered_master_count; j++) {
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000431 startchip = 0;
Michael Karcher222bf102011-12-22 23:27:03 +0000432 while (chipcount < ARRAY_SIZE(flashes)) {
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000433 startchip = probe_flash(&registered_masters[j], startchip, &flashes[chipcount], 0);
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000434 if (startchip == -1)
435 break;
436 chipcount++;
437 startchip++;
438 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000439 }
440
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000441 if (chipcount > 1) {
Stefan Tauner0554ca52013-07-25 22:54:25 +0000442 msg_cinfo("Multiple flash chip definitions match the detected chip(s): \"%s\"",
443 flashes[0].chip->name);
Stefan Tauner716e0982011-07-25 20:38:52 +0000444 for (i = 1; i < chipcount; i++)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000445 msg_cinfo(", \"%s\"", flashes[i].chip->name);
Stefan Tauner0554ca52013-07-25 22:54:25 +0000446 msg_cinfo("\nPlease specify which chip definition to use with the -c <chipname> option.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000447 ret = 1;
448 goto out_shutdown;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000449 } else if (!chipcount) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000450 msg_cinfo("No EEPROM/flash device found.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000451 if (!force || !chip_to_probe) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000452 msg_cinfo("Note: flashrom can never write if the flash chip isn't found "
453 "automatically.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000454 }
455 if (force && read_it && chip_to_probe) {
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000456 struct registered_master *mst;
457 int compatible_masters = 0;
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000458 msg_cinfo("Force read (-f -r -c) requested, pretending the chip is there:\n");
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000459 /* This loop just counts compatible controllers. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000460 for (j = 0; j < registered_master_count; j++) {
461 mst = &registered_masters[j];
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000462 /* chip is still set from the chip_to_probe earlier in this function. */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000463 if (mst->buses_supported & chip->bustype)
464 compatible_masters++;
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000465 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000466 if (!compatible_masters) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000467 msg_cinfo("No compatible controller found for the requested flash chip.\n");
468 ret = 1;
469 goto out_shutdown;
470 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000471 if (compatible_masters > 1)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000472 msg_cinfo("More than one compatible controller found for the requested flash "
473 "chip, using the first one.\n");
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000474 for (j = 0; j < registered_master_count; j++) {
475 mst = &registered_masters[j];
476 startchip = probe_flash(mst, 0, &flashes[0], 1);
Carl-Daniel Hailfingerb428e972012-02-16 20:31:25 +0000477 if (startchip != -1)
478 break;
479 }
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000480 if (startchip == -1) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000481 // FIXME: This should never happen! Ask for a bug report?
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000482 msg_cinfo("Probing for flash chip '%s' failed.\n", chip_to_probe);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000483 ret = 1;
484 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000485 }
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000486 if (map_flash(&flashes[0]) != 0) {
487 free(flashes[0].chip);
488 ret = 1;
489 goto out_shutdown;
490 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000491 msg_cinfo("Please note that forced reads most likely contain garbage.\n");
492 ret = read_flash_to_file(&flashes[0], filename);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000493 unmap_flash(&flashes[0]);
Stefan Taunerb8911d62012-12-26 07:55:00 +0000494 free(flashes[0].chip);
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000495 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000496 }
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000497 ret = 1;
498 goto out_shutdown;
Stefan Tauner1d947632011-09-11 22:08:58 +0000499 } else if (!chip_to_probe) {
500 /* repeat for convenience when looking at foreign logs */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000501 tempstr = flashbuses_to_text(flashes[0].chip->bustype);
Stefan Tauner1d947632011-09-11 22:08:58 +0000502 msg_gdbg("Found %s flash chip \"%s\" (%d kB, %s).\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000503 flashes[0].chip->vendor, flashes[0].chip->name, flashes[0].chip->total_size, tempstr);
Stefan Tauner1d947632011-09-11 22:08:58 +0000504 free(tempstr);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000505 }
506
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000507 fill_flash = &flashes[0];
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000508
Stefan Tauner9b32de92014-08-08 23:52:33 +0000509 print_chip_support_status(fill_flash->chip);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000510
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000511 unsigned int limitexceeded = count_max_decode_exceedings(fill_flash);
512 if (limitexceeded > 0 && !force) {
513 enum chipbustype commonbuses = fill_flash->mst->buses_supported & fill_flash->chip->bustype;
514
515 /* Sometimes chip and programmer have more than one bus in common,
516 * and the limit is not exceeded on all buses. Tell the user. */
517 if ((bitcount(commonbuses) > limitexceeded)) {
518 msg_pdbg("There is at least one interface available which could support the size of\n"
519 "the selected flash chip.\n");
520 }
521 msg_cerr("This flash chip is too big for this programmer (--verbose/-V gives details).\n"
522 "Use --force/-f to override at your own risk.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000523 ret = 1;
524 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000525 }
526
527 if (!(read_it | write_it | verify_it | erase_it)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000528 msg_ginfo("No operations were specified.\n");
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000529 goto out_shutdown;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000530 }
531
Nico Huber899e4ec2016-04-29 18:39:01 +0200532 if (layoutfile)
533 flashrom_layout_set(fill_flash, get_global_layout());
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000534
Nico Huber899e4ec2016-04-29 18:39:01 +0200535 flashrom_flag_set(fill_flash, FLASHROM_FLAG_FORCE, !!force);
536 flashrom_flag_set(fill_flash, FLASHROM_FLAG_FORCE_BOARDMISMATCH, !!force_boardmismatch);
537 flashrom_flag_set(fill_flash, FLASHROM_FLAG_VERIFY_AFTER_WRITE, !dont_verify_it);
Nico Huber99d15952016-05-02 16:54:24 +0200538 flashrom_flag_set(fill_flash, FLASHROM_FLAG_VERIFY_WHOLE_CHIP, !dont_verify_all);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000539
Carl-Daniel Hailfinger9ad42552010-09-15 10:20:16 +0000540 /* FIXME: We should issue an unconditional chip reset here. This can be
541 * done once we have a .reset function in struct flashchip.
542 * Give the chip time to settle.
543 */
544 programmer_delay(100000);
Nico Huber899e4ec2016-04-29 18:39:01 +0200545 if (read_it)
546 ret = do_read(fill_flash, filename);
547 else if (erase_it)
548 ret = do_erase(fill_flash);
549 else if (write_it)
550 ret = do_write(fill_flash, filename);
551 else if (verify_it)
552 ret = do_verify(fill_flash, filename);
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000553
554out_shutdown:
555 programmer_shutdown();
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +0000556out:
Stefan Taunerb8911d62012-12-26 07:55:00 +0000557 for (i = 0; i < chipcount; i++)
558 free(flashes[i].chip);
559
Stefan Tauner949ccc82013-09-15 14:01:06 +0000560 layout_cleanup();
Stefan Taunerb8911d62012-12-26 07:55:00 +0000561 free(filename);
562 free(layoutfile);
563 free(pparam);
564 /* clean up global variables */
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000565 free((char *)chip_to_probe); /* Silence! Freeing is not modifying contents. */
Stefan Taunerb8911d62012-12-26 07:55:00 +0000566 chip_to_probe = NULL;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000567#ifndef STANDALONE
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000568 free(logfile);
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +0000569 ret |= close_logfile();
570#endif /* !STANDALONE */
Carl-Daniel Hailfingerd5660142011-07-15 23:47:45 +0000571 return ret;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000572}