blob: 4899b3a2b20bb62c36c0c96b2ac58b302b6cc71b [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>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <string.h>
29#include <stdlib.h>
30#include <getopt.h>
31#include "flash.h"
32#include "flashchips.h"
33
34void cli_classic_usage(const char *name)
35{
36 const char *pname;
37 int pnamelen;
38 int remaining = 0;
39 enum programmer p;
40
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000041 printf("Usage: %s [-n] [-V] [-f] [-h|-R|-L|"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000042#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000043 "-z|"
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000044#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000045 "-E|-r <file>|-w <file>|-v <file>]\n"
46 " [-c <chipname>] [-m [<vendor>:]<part>] [-l <file>]\n"
47 " [-i <image>] [-p <programmername>[:<parameters>]]\n",
48 name);
49
50 printf("Please note that the command line interface for flashrom has "
51 "changed between\n"
52 "0.9.1 and 0.9.2 and will change again before flashrom 1.0.\n"
53 "Do not use flashrom in scripts or other automated tools "
54 "without checking\n"
55 "that your flashrom version won't interpret options in a "
56 "different way.\n\n");
57
58 printf(" -h | --help print this help text\n"
59 " -R | --version print version (release)\n"
60 " -r | --read <file> read flash and save to "
61 "<file>\n"
62 " -w | --write <file> write <file> to flash\n"
63 " -v | --verify <file> verify flash against "
64 "<file>\n"
65 " -E | --erase erase flash device\n"
66 " -V | --verbose more verbose output\n"
67 " -c | --chip <chipname> probe only for specified "
68 "flash chip\n"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000069#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000070 /* FIXME: --mainboard should be a programmer parameter */
71 " -m | --mainboard <[vendor:]part> override mainboard "
72 "detection\n"
73#endif
74 " -f | --force force specific operations "
75 "(see man page)\n"
76 " -n | --noverify don't auto-verify\n"
77 " -l | --layout <file> read ROM layout from "
78 "<file>\n"
79 " -i | --image <name> only flash image <name> "
80 "from flash layout\n"
81 " -L | --list-supported print supported devices\n"
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000082#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +000083 " -z | --list-supported-wiki print supported devices "
84 "in wiki syntax\n"
85#endif
86 " -p | --programmer <name>[:<param>] specify the programmer "
87 "device");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +000088
89 for (p = 0; p < PROGRAMMER_INVALID; p++) {
90 pname = programmer_table[p].name;
91 pnamelen = strlen(pname);
92 if (remaining - pnamelen - 2 < 0) {
93 printf("\n ");
94 remaining = 43;
95 } else {
96 printf(" ");
97 remaining--;
98 }
99 if (p == 0) {
100 printf("(");
101 remaining--;
102 }
103 printf("%s", pname);
104 remaining -= pnamelen;
105 if (p < PROGRAMMER_INVALID - 1) {
106 printf(",");
107 remaining--;
108 } else {
109 printf(")\n");
110 }
111 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000112
113 printf("\nYou can specify one of -h, -R, -L, "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000114#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000115 "-z, "
116#endif
117 "-E, -r, -w, -v or no operation.\n"
118 "If no operation is specified, flashrom will only probe for "
119 "flash chips.\n\n");
120}
121
122void cli_classic_abort_usage(const char *name)
123{
124 printf("Please run \"%s --help\" for usage info.\n", name);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000125 exit(1);
126}
127
128int cli_classic(int argc, char *argv[])
129{
130 unsigned long size;
131 /* Probe for up to three flash chips. */
132 struct flashchip *flash, *flashes[3];
133 const char *name;
134 int namelen;
135 int opt;
136 int option_index = 0;
137 int force = 0;
138 int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
139 int dont_verify_it = 0, list_supported = 0;
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000140#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000141 int list_supported_wiki = 0;
142#endif
143 int operation_specified = 0;
144 int i;
145
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000146 const char *optstring = "r:Rw:v:nVEfc:m:l:i:p:Lzh";
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000147 static struct option long_options[] = {
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000148 {"read", 1, 0, 'r'},
149 {"write", 1, 0, 'w'},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000150 {"erase", 0, 0, 'E'},
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000151 {"verify", 1, 0, 'v'},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000152 {"noverify", 0, 0, 'n'},
153 {"chip", 1, 0, 'c'},
154 {"mainboard", 1, 0, 'm'},
155 {"verbose", 0, 0, 'V'},
156 {"force", 0, 0, 'f'},
157 {"layout", 1, 0, 'l'},
158 {"image", 1, 0, 'i'},
159 {"list-supported", 0, 0, 'L'},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000160 {"list-supported-wiki", 0, 0, 'z'},
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000161 {"programmer", 1, 0, 'p'},
162 {"help", 0, 0, 'h'},
163 {"version", 0, 0, 'R'},
164 {0, 0, 0, 0}
165 };
166
167 char *filename = NULL;
168
169 char *tempstr = NULL;
170
171 print_version();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000172 print_banner();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000173
174 if (selfcheck())
175 exit(1);
176
177 setbuf(stdout, NULL);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000178 /* FIXME: Delay all operation_specified checks until after command
179 * line parsing to allow --help overriding everything else.
180 */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000181 while ((opt = getopt_long(argc, argv, optstring,
182 long_options, &option_index)) != EOF) {
183 switch (opt) {
184 case 'r':
185 if (++operation_specified > 1) {
186 fprintf(stderr, "More than one operation "
187 "specified. Aborting.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000188 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000189 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000190 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000191 read_it = 1;
192 break;
193 case 'w':
194 if (++operation_specified > 1) {
195 fprintf(stderr, "More than one operation "
196 "specified. Aborting.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000197 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000198 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000199 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000200 write_it = 1;
201 break;
202 case 'v':
203 //FIXME: gracefully handle superfluous -v
204 if (++operation_specified > 1) {
205 fprintf(stderr, "More than one operation "
206 "specified. Aborting.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000207 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000208 }
209 if (dont_verify_it) {
210 fprintf(stderr, "--verify and --noverify are"
211 "mutually exclusive. Aborting.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000212 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000213 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000214 filename = strdup(optarg);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000215 verify_it = 1;
216 break;
217 case 'n':
218 if (verify_it) {
219 fprintf(stderr, "--verify and --noverify are"
220 "mutually exclusive. Aborting.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000221 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000222 }
223 dont_verify_it = 1;
224 break;
225 case 'c':
226 chip_to_probe = strdup(optarg);
227 break;
228 case 'V':
Sean Nelson51e97d72010-01-07 20:09:33 +0000229 verbose++;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000230 break;
231 case 'E':
232 if (++operation_specified > 1) {
233 fprintf(stderr, "More than one operation "
234 "specified. Aborting.\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000235 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000236 }
237 erase_it = 1;
238 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000239 case 'm':
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000240#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000241 tempstr = strdup(optarg);
242 lb_vendor_dev_from_string(tempstr);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000243#else
244 fprintf(stderr, "Error: Internal programmer support "
245 "was not compiled in and --mainboard only\n"
246 "applies to the internal programmer. Aborting.\n");
247 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000248#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000249 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000250 case 'f':
251 force = 1;
252 break;
253 case 'l':
254 tempstr = strdup(optarg);
255 if (read_romlayout(tempstr))
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000256 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000257 break;
258 case 'i':
259 tempstr = strdup(optarg);
260 find_romentry(tempstr);
261 break;
262 case 'L':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000263 if (++operation_specified > 1) {
264 fprintf(stderr, "More than one operation "
265 "specified. Aborting.\n");
266 cli_classic_abort_usage(argv[0]);
267 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000268 list_supported = 1;
269 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000270 case 'z':
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000271#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000272 if (++operation_specified > 1) {
273 fprintf(stderr, "More than one operation "
274 "specified. Aborting.\n");
275 cli_classic_abort_usage(argv[0]);
276 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000277 list_supported_wiki = 1;
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000278#else
279 fprintf(stderr, "Error: Wiki output was not compiled "
280 "in. Aborting.\n");
281 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000282#endif
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000283 break;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000284 case 'p':
285 for (programmer = 0; programmer < PROGRAMMER_INVALID; programmer++) {
286 name = programmer_table[programmer].name;
287 namelen = strlen(name);
288 if (strncmp(optarg, name, namelen) == 0) {
289 switch (optarg[namelen]) {
290 case ':':
291 programmer_param = strdup(optarg + namelen + 1);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000292 if (!strlen(programmer_param)) {
293 free(programmer_param);
294 programmer_param = NULL;
295 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000296 break;
297 case '\0':
298 break;
299 default:
300 /* The continue refers to the
301 * for loop. It is here to be
302 * able to differentiate between
303 * foo and foobar.
304 */
305 continue;
306 }
307 break;
308 }
309 }
310 if (programmer == PROGRAMMER_INVALID) {
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000311 fprintf(stderr, "Error: Unknown programmer "
312 "%s.\n", optarg);
313 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000314 }
315 break;
316 case 'R':
317 /* print_version() is always called during startup. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000318 if (++operation_specified > 1) {
319 fprintf(stderr, "More than one operation "
320 "specified. Aborting.\n");
321 cli_classic_abort_usage(argv[0]);
322 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000323 exit(0);
324 break;
325 case 'h':
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000326 if (++operation_specified > 1) {
327 fprintf(stderr, "More than one operation "
328 "specified. Aborting.\n");
329 cli_classic_abort_usage(argv[0]);
330 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000331 cli_classic_usage(argv[0]);
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000332 exit(0);
333 break;
334 default:
335 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000336 break;
337 }
338 }
339
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000340 /* FIXME: Print the actions flashrom will take. */
341
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000342 if (list_supported) {
343 print_supported();
344 exit(0);
345 }
346
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000347#if CONFIG_PRINT_WIKI == 1
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000348 if (list_supported_wiki) {
349 print_supported_wiki();
350 exit(0);
351 }
352#endif
353
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000354 if (optind < argc) {
355 fprintf(stderr, "Error: Extra parameter found.\n");
356 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000357 }
358
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000359#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000360 if ((programmer != PROGRAMMER_INTERNAL) && (lb_part || lb_vendor)) {
361 fprintf(stderr, "Error: --mainboard requires the internal "
362 "programmer. Aborting.\n");
363 cli_classic_abort_usage(argv[0]);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000364 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000365#endif
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000366
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000367 if (chip_to_probe) {
368 for (flash = flashchips; flash && flash->name; flash++)
369 if (!strcmp(flash->name, chip_to_probe))
370 break;
371 if (!flash || !flash->name) {
372 fprintf(stderr, "Error: Unknown chip '%s' specified.\n",
373 chip_to_probe);
374 printf("Run flashrom -L to view the hardware supported "
375 "in this flashrom version.\n");
376 exit(1);
377 }
378 /* Clean up after the check. */
379 flash = NULL;
380 }
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000381
Carl-Daniel Hailfinger49884202010-05-22 07:10:46 +0000382 /* FIXME: Delay calibration should happen in programmer code. */
383 myusec_calibrate_delay();
384
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000385 msg_pdbg("Initializing %s programmer\n",
386 programmer_table[programmer].name);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000387 if (programmer_init()) {
388 fprintf(stderr, "Error: Programmer initialization failed.\n");
389 exit(1);
390 }
391
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000392 /* FIXME: Delay calibration should happen in programmer code. */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000393 for (i = 0; i < ARRAY_SIZE(flashes); i++) {
394 flashes[i] =
395 probe_flash(i ? flashes[i - 1] + 1 : flashchips, 0);
396 if (!flashes[i])
397 for (i++; i < ARRAY_SIZE(flashes); i++)
398 flashes[i] = NULL;
399 }
400
401 if (flashes[1]) {
402 printf("Multiple flash chips were detected:");
403 for (i = 0; i < ARRAY_SIZE(flashes) && flashes[i]; i++)
404 printf(" %s", flashes[i]->name);
405 printf("\nPlease specify which chip to use with the -c <chipname> option.\n");
406 programmer_shutdown();
407 exit(1);
408 } else if (!flashes[0]) {
409 printf("No EEPROM/flash device found.\n");
410 if (!force || !chip_to_probe) {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000411 printf("Note: flashrom can never write if the flash chip isn't found automatically.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000412 }
413 if (force && read_it && chip_to_probe) {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000414 printf("Force read (-f -r -c) requested, pretending the chip is there:\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000415 flashes[0] = probe_flash(flashchips, 1);
416 if (!flashes[0]) {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000417 printf("Probing for flash chip '%s' failed.\n", chip_to_probe);
418 programmer_shutdown();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000419 exit(1);
420 }
421 printf("Please note that forced reads most likely contain garbage.\n");
422 return read_flash(flashes[0], filename);
423 }
424 // FIXME: flash writes stay enabled!
425 programmer_shutdown();
426 exit(1);
427 }
428
429 flash = flashes[0];
430
431 check_chip_supported(flash);
432
433 size = flash->total_size * 1024;
434 if (check_max_decode((buses_supported & flash->bustype), size) &&
435 (!force)) {
436 fprintf(stderr, "Chip is too big for this programmer "
437 "(-V gives details). Use --force to override.\n");
438 programmer_shutdown();
439 return 1;
440 }
441
442 if (!(read_it | write_it | verify_it | erase_it)) {
443 printf("No operations were specified.\n");
444 // FIXME: flash writes stay enabled!
445 programmer_shutdown();
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +0000446 exit(0);
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000447 }
448
449 if (!filename && !erase_it) {
450 printf("Error: No filename specified.\n");
451 // FIXME: flash writes stay enabled!
452 programmer_shutdown();
453 exit(1);
454 }
455
456 /* Always verify write operations unless -n is used. */
457 if (write_it && !dont_verify_it)
458 verify_it = 1;
459
460 return doit(flash, force, filename, read_it, write_it, erase_it, verify_it);
461}