blob: baca90096345862742027f8c34b772d1e168e858 [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
24#include <fcntl.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <string.h>
28#include <stdlib.h>
29#include <getopt.h>
30#include "flash.h"
31#include "flashchips.h"
32
33void cli_classic_usage(const char *name)
34{
35 const char *pname;
36 int pnamelen;
37 int remaining = 0;
38 enum programmer p;
39
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000040 printf("Usage: %s [-n] [-V] [-f] [-h|-R|-L|"
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000041#if PRINT_WIKI_SUPPORT == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000042 "-z|"
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000043#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000044 "-E|-r <file>|-w <file>|-v <file>]\n"
45 " [-c <chipname>] [-m [<vendor>:]<part>] [-l <file>]\n"
46 " [-i <image>] [-p <programmername>[:<parameters>]]\n",
47 name);
48
49 printf("Please note that the command line interface for flashrom has "
50 "changed between\n"
51 "0.9.1 and 0.9.2 and will change again before flashrom 1.0.\n"
52 "Do not use flashrom in scripts or other automated tools "
53 "without checking\n"
54 "that your flashrom version won't interpret options in a "
55 "different way.\n\n");
56
57 printf(" -h | --help print this help text\n"
58 " -R | --version print version (release)\n"
59 " -r | --read <file> read flash and save to "
60 "<file>\n"
61 " -w | --write <file> write <file> to flash\n"
62 " -v | --verify <file> verify flash against "
63 "<file>\n"
64 " -E | --erase erase flash device\n"
65 " -V | --verbose more verbose output\n"
66 " -c | --chip <chipname> probe only for specified "
67 "flash chip\n"
68#if INTERNAL_SUPPORT == 1
69 /* FIXME: --mainboard should be a programmer parameter */
70 " -m | --mainboard <[vendor:]part> override mainboard "
71 "detection\n"
72#endif
73 " -f | --force force specific operations "
74 "(see man page)\n"
75 " -n | --noverify don't auto-verify\n"
76 " -l | --layout <file> read ROM layout from "
77 "<file>\n"
78 " -i | --image <name> only flash image <name> "
79 "from flash layout\n"
80 " -L | --list-supported print supported devices\n"
81#if PRINT_WIKI_SUPPORT == 1
82 " -z | --list-supported-wiki print supported devices "
83 "in wiki syntax\n"
84#endif
85 " -p | --programmer <name>[:<param>] specify the programmer "
86 "device");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000087
88 for (p = 0; p < PROGRAMMER_INVALID; p++) {
89 pname = programmer_table[p].name;
90 pnamelen = strlen(pname);
91 if (remaining - pnamelen - 2 < 0) {
92 printf("\n ");
93 remaining = 43;
94 } else {
95 printf(" ");
96 remaining--;
97 }
98 if (p == 0) {
99 printf("(");
100 remaining--;
101 }
102 printf("%s", pname);
103 remaining -= pnamelen;
104 if (p < PROGRAMMER_INVALID - 1) {
105 printf(",");
106 remaining--;
107 } else {
108 printf(")\n");
109 }
110 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000111
112 printf("\nYou can specify one of -h, -R, -L, "
113#if PRINT_WIKI_SUPPORT == 1
114 "-z, "
115#endif
116 "-E, -r, -w, -v or no operation.\n"
117 "If no operation is specified, flashrom will only probe for "
118 "flash chips.\n\n");
119}
120
121void cli_classic_abort_usage(const char *name)
122{
123 printf("Please run \"%s --help\" for usage info.\n", name);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000124 exit(1);
125}
126
127int cli_classic(int argc, char *argv[])
128{
129 unsigned long size;
130 /* Probe for up to three flash chips. */
131 struct flashchip *flash, *flashes[3];
132 const char *name;
133 int namelen;
134 int opt;
135 int option_index = 0;
136 int force = 0;
137 int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
138 int dont_verify_it = 0, list_supported = 0;
139#if PRINT_WIKI_SUPPORT == 1
140 int list_supported_wiki = 0;
141#endif
142 int operation_specified = 0;
143 int i;
144
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000145 const char *optstring = "r:Rw:v:nVEfc:m:l:i:p:Lzh";
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000146 static struct option long_options[] = {
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000147 {"read", 1, 0, 'r'},
148 {"write", 1, 0, 'w'},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000149 {"erase", 0, 0, 'E'},
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000150 {"verify", 1, 0, 'v'},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000151 {"noverify", 0, 0, 'n'},
152 {"chip", 1, 0, 'c'},
153 {"mainboard", 1, 0, 'm'},
154 {"verbose", 0, 0, 'V'},
155 {"force", 0, 0, 'f'},
156 {"layout", 1, 0, 'l'},
157 {"image", 1, 0, 'i'},
158 {"list-supported", 0, 0, 'L'},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000159 {"list-supported-wiki", 0, 0, 'z'},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000160 {"programmer", 1, 0, 'p'},
161 {"help", 0, 0, 'h'},
162 {"version", 0, 0, 'R'},
163 {0, 0, 0, 0}
164 };
165
166 char *filename = NULL;
167
168 char *tempstr = NULL;
169
170 print_version();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000171 print_banner();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000172
173 if (selfcheck())
174 exit(1);
175
176 setbuf(stdout, NULL);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000177 /* FIXME: Delay all operation_specified checks until after command
178 * line parsing to allow --help overriding everything else.
179 */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000180 while ((opt = getopt_long(argc, argv, optstring,
181 long_options, &option_index)) != EOF) {
182 switch (opt) {
183 case 'r':
184 if (++operation_specified > 1) {
185 fprintf(stderr, "More than one operation "
186 "specified. Aborting.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000187 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000188 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000189 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000190 read_it = 1;
191 break;
192 case 'w':
193 if (++operation_specified > 1) {
194 fprintf(stderr, "More than one operation "
195 "specified. Aborting.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000196 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000197 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000198 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000199 write_it = 1;
200 break;
201 case 'v':
202 //FIXME: gracefully handle superfluous -v
203 if (++operation_specified > 1) {
204 fprintf(stderr, "More than one operation "
205 "specified. Aborting.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000206 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000207 }
208 if (dont_verify_it) {
209 fprintf(stderr, "--verify and --noverify are"
210 "mutually exclusive. Aborting.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000211 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000212 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000213 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000214 verify_it = 1;
215 break;
216 case 'n':
217 if (verify_it) {
218 fprintf(stderr, "--verify and --noverify are"
219 "mutually exclusive. Aborting.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000220 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000221 }
222 dont_verify_it = 1;
223 break;
224 case 'c':
225 chip_to_probe = strdup(optarg);
226 break;
227 case 'V':
Sean Nelson51e97d72010-01-07 20:09:33 +0000228 verbose++;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000229 break;
230 case 'E':
231 if (++operation_specified > 1) {
232 fprintf(stderr, "More than one operation "
233 "specified. Aborting.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000234 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000235 }
236 erase_it = 1;
237 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000238 case 'm':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000239#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000240 tempstr = strdup(optarg);
241 lb_vendor_dev_from_string(tempstr);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000242#else
243 fprintf(stderr, "Error: Internal programmer support "
244 "was not compiled in and --mainboard only\n"
245 "applies to the internal programmer. Aborting.\n");
246 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000247#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000248 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000249 case 'f':
250 force = 1;
251 break;
252 case 'l':
253 tempstr = strdup(optarg);
254 if (read_romlayout(tempstr))
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000255 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000256 break;
257 case 'i':
258 tempstr = strdup(optarg);
259 find_romentry(tempstr);
260 break;
261 case 'L':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000262 if (++operation_specified > 1) {
263 fprintf(stderr, "More than one operation "
264 "specified. Aborting.\n");
265 cli_classic_abort_usage(argv[0]);
266 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000267 list_supported = 1;
268 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000269 case 'z':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000270#if PRINT_WIKI_SUPPORT == 1
271 if (++operation_specified > 1) {
272 fprintf(stderr, "More than one operation "
273 "specified. Aborting.\n");
274 cli_classic_abort_usage(argv[0]);
275 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000276 list_supported_wiki = 1;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000277#else
278 fprintf(stderr, "Error: Wiki output was not compiled "
279 "in. Aborting.\n");
280 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000281#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000282 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000283 case 'p':
284 for (programmer = 0; programmer < PROGRAMMER_INVALID; programmer++) {
285 name = programmer_table[programmer].name;
286 namelen = strlen(name);
287 if (strncmp(optarg, name, namelen) == 0) {
288 switch (optarg[namelen]) {
289 case ':':
290 programmer_param = strdup(optarg + namelen + 1);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000291 if (!strlen(programmer_param)) {
292 free(programmer_param);
293 programmer_param = NULL;
294 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000295 break;
296 case '\0':
297 break;
298 default:
299 /* The continue refers to the
300 * for loop. It is here to be
301 * able to differentiate between
302 * foo and foobar.
303 */
304 continue;
305 }
306 break;
307 }
308 }
309 if (programmer == PROGRAMMER_INVALID) {
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000310 fprintf(stderr, "Error: Unknown programmer "
311 "%s.\n", optarg);
312 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000313 }
314 break;
315 case 'R':
316 /* print_version() is always called during startup. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000317 if (++operation_specified > 1) {
318 fprintf(stderr, "More than one operation "
319 "specified. Aborting.\n");
320 cli_classic_abort_usage(argv[0]);
321 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000322 exit(0);
323 break;
324 case 'h':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000325 if (++operation_specified > 1) {
326 fprintf(stderr, "More than one operation "
327 "specified. Aborting.\n");
328 cli_classic_abort_usage(argv[0]);
329 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000330 cli_classic_usage(argv[0]);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000331 exit(0);
332 break;
333 default:
334 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000335 break;
336 }
337 }
338
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000339 /* FIXME: Print the actions flashrom will take. */
340
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000341 if (list_supported) {
342 print_supported();
343 exit(0);
344 }
345
346#if PRINT_WIKI_SUPPORT == 1
347 if (list_supported_wiki) {
348 print_supported_wiki();
349 exit(0);
350 }
351#endif
352
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000353 if (optind < argc) {
354 fprintf(stderr, "Error: Extra parameter found.\n");
355 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000356 }
357
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000358#if INTERNAL_SUPPORT == 1
359 if ((programmer != PROGRAMMER_INTERNAL) && (lb_part || lb_vendor)) {
360 fprintf(stderr, "Error: --mainboard requires the internal "
361 "programmer. Aborting.\n");
362 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000363 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000364#endif
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000365
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000366 if (chip_to_probe) {
367 for (flash = flashchips; flash && flash->name; flash++)
368 if (!strcmp(flash->name, chip_to_probe))
369 break;
370 if (!flash || !flash->name) {
371 fprintf(stderr, "Error: Unknown chip '%s' specified.\n",
372 chip_to_probe);
373 printf("Run flashrom -L to view the hardware supported "
374 "in this flashrom version.\n");
375 exit(1);
376 }
377 /* Clean up after the check. */
378 flash = NULL;
379 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000380
Carl-Daniel Hailfinger49884202010-05-22 07:10:46 +0000381 /* FIXME: Delay calibration should happen in programmer code. */
382 myusec_calibrate_delay();
383
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000384 msg_pdbg("Initializing %s programmer\n",
385 programmer_table[programmer].name);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000386 if (programmer_init()) {
387 fprintf(stderr, "Error: Programmer initialization failed.\n");
388 exit(1);
389 }
390
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000391 /* FIXME: Delay calibration should happen in programmer code. */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000392 for (i = 0; i < ARRAY_SIZE(flashes); i++) {
393 flashes[i] =
394 probe_flash(i ? flashes[i - 1] + 1 : flashchips, 0);
395 if (!flashes[i])
396 for (i++; i < ARRAY_SIZE(flashes); i++)
397 flashes[i] = NULL;
398 }
399
400 if (flashes[1]) {
401 printf("Multiple flash chips were detected:");
402 for (i = 0; i < ARRAY_SIZE(flashes) && flashes[i]; i++)
403 printf(" %s", flashes[i]->name);
404 printf("\nPlease specify which chip to use with the -c <chipname> option.\n");
405 programmer_shutdown();
406 exit(1);
407 } else if (!flashes[0]) {
408 printf("No EEPROM/flash device found.\n");
409 if (!force || !chip_to_probe) {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000410 printf("Note: flashrom can never write if the flash chip isn't found automatically.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000411 }
412 if (force && read_it && chip_to_probe) {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000413 printf("Force read (-f -r -c) requested, pretending the chip is there:\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000414 flashes[0] = probe_flash(flashchips, 1);
415 if (!flashes[0]) {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000416 printf("Probing for flash chip '%s' failed.\n", chip_to_probe);
417 programmer_shutdown();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000418 exit(1);
419 }
420 printf("Please note that forced reads most likely contain garbage.\n");
421 return read_flash(flashes[0], filename);
422 }
423 // FIXME: flash writes stay enabled!
424 programmer_shutdown();
425 exit(1);
426 }
427
428 flash = flashes[0];
429
430 check_chip_supported(flash);
431
432 size = flash->total_size * 1024;
433 if (check_max_decode((buses_supported & flash->bustype), size) &&
434 (!force)) {
435 fprintf(stderr, "Chip is too big for this programmer "
436 "(-V gives details). Use --force to override.\n");
437 programmer_shutdown();
438 return 1;
439 }
440
441 if (!(read_it | write_it | verify_it | erase_it)) {
442 printf("No operations were specified.\n");
443 // FIXME: flash writes stay enabled!
444 programmer_shutdown();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000445 exit(0);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000446 }
447
448 if (!filename && !erase_it) {
449 printf("Error: No filename specified.\n");
450 // FIXME: flash writes stay enabled!
451 programmer_shutdown();
452 exit(1);
453 }
454
455 /* Always verify write operations unless -n is used. */
456 if (write_it && !dont_verify_it)
457 verify_it = 1;
458
459 return doit(flash, force, filename, read_it, write_it, erase_it, verify_it);
460}