Flashrom

Flashrom Svn Source Tree

Root/trunk/cli_classic.c

  • Property svn:keywords set to Author Date Id Revision
  • Property svn:eol-style set to native
1/*
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 <stdio.h>
25#include <fcntl.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#include "programmer.h"
33
34#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
49PROGRAMMER_NIC3COM
50#endif
51#if CONFIG_NICREALTEK == 1
52PROGRAMMER_NICREALTEK
53#endif
54#if CONFIG_NICNATSEMI == 1
55PROGRAMMER_NICNATSEMI
56#endif
57#if CONFIG_GFXNVIDIA == 1
58PROGRAMMER_GFXNVIDIA
59#endif
60#if CONFIG_DRKAISER == 1
61PROGRAMMER_DRKAISER
62#endif
63#if CONFIG_SATASII == 1
64PROGRAMMER_SATASII
65#endif
66#if CONFIG_ATAHPT == 1
67PROGRAMMER_ATAHPT
68#endif
69#if CONFIG_FT2232_SPI == 1
70PROGRAMMER_FT2232_SPI
71#endif
72#if CONFIG_SERPROG == 1
73PROGRAMMER_SERPROG
74#endif
75#if CONFIG_BUSPIRATE_SPI == 1
76PROGRAMMER_BUSPIRATE_SPI
77#endif
78#if CONFIG_DEDIPROG == 1
79PROGRAMMER_DEDIPROG
80#endif
81#if CONFIG_RAYER_SPI == 1
82PROGRAMMER_RAYER_SPI
83#endif
84#if CONFIG_NICINTEL == 1
85PROGRAMMER_NICINTEL
86#endif
87#if CONFIG_NICINTEL_SPI == 1
88PROGRAMMER_NICINTEL_SPI
89#endif
90#if CONFIG_OGP_SPI == 1
91PROGRAMMER_OGP_SPI
92#endif
93#if CONFIG_SATAMV == 1
94PROGRAMMER_SATAMV
95#endif
96#if CONFIG_LINUX_SPI == 1
97PROGRAMMER_LINUX_SPI
98#endif
99;
100#endif
101
102static void cli_classic_usage(const char *name)
103{
104printf("Usage: flashrom [-n] [-V] [-f] [-h|-R|-L|"
105#if CONFIG_PRINT_WIKI == 1
106 "-z|"
107#endif
108 "-E|-r <file>|-w <file>|-v <file>]\n"
109 " [-c <chipname>] [-l <file>]\n"
110 " [-i <image>] [-p <programmername>[:<parameters>]]\n\n");
111
112printf("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
120printf(" -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"
131 " -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"
139#if CONFIG_PRINT_WIKI == 1
140 " -z | --list-supported-wiki print supported devices "
141 "in wiki syntax\n"
142#endif
143 " -p | --programmer <name>[:<param>] specify the programmer "
144 "device\n");
145
146list_programmers_linebreak(37, 80, 1);
147printf("\nYou can specify one of -h, -R, -L, "
148#if CONFIG_PRINT_WIKI == 1
149 "-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
156static void cli_classic_abort_usage(void)
157{
158printf("Please run \"flashrom --help\" for usage info.\n");
159exit(1);
160}
161
162int main(int argc, char *argv[])
163{
164unsigned long size;
165/* Probe for up to three flash chips. */
166const struct flashchip *flash;
167struct flashctx flashes[3];
168struct flashctx *fill_flash;
169const char *name;
170int namelen, opt, i, j;
171int startchip = 0, chipcount = 0, option_index = 0, force = 0;
172#if CONFIG_PRINT_WIKI == 1
173int list_supported_wiki = 0;
174#endif
175int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
176int dont_verify_it = 0, list_supported = 0, operation_specified = 0;
177enum programmer prog = PROGRAMMER_INVALID;
178int ret = 0;
179
180static const char optstring[] = "r:Rw:v:nVEfc:l:i:p:Lzh";
181static const struct option long_options[] = {
182{"read",1, NULL, 'r'},
183{"write",1, NULL, 'w'},
184{"erase",0, NULL, 'E'},
185{"verify",1, NULL, 'v'},
186{"noverify",0, NULL, 'n'},
187{"chip",1, NULL, 'c'},
188{"verbose",0, NULL, 'V'},
189{"force",0, NULL, 'f'},
190{"layout",1, NULL, 'l'},
191{"image",1, NULL, 'i'},
192{"list-supported",0, NULL, 'L'},
193{"list-supported-wiki",0, NULL, 'z'},
194{"programmer",1, NULL, 'p'},
195{"help",0, NULL, 'h'},
196{"version",0, NULL, 'R'},
197{NULL,0, NULL, 0},
198};
199
200char *filename = NULL;
201char *layoutfile = NULL;
202char *tempstr = NULL;
203char *pparam = NULL;
204
205print_version();
206print_banner();
207
208if (selfcheck())
209exit(1);
210
211setbuf(stdout, NULL);
212/* FIXME: Delay all operation_specified checks until after command
213 * line parsing to allow --help overriding everything else.
214 */
215while ((opt = getopt_long(argc, argv, optstring,
216 long_options, &option_index)) != EOF) {
217switch (opt) {
218case 'r':
219if (++operation_specified > 1) {
220fprintf(stderr, "More than one operation "
221"specified. Aborting.\n");
222cli_classic_abort_usage();
223}
224filename = strdup(optarg);
225read_it = 1;
226break;
227case 'w':
228if (++operation_specified > 1) {
229fprintf(stderr, "More than one operation "
230"specified. Aborting.\n");
231cli_classic_abort_usage();
232}
233filename = strdup(optarg);
234write_it = 1;
235break;
236case 'v':
237//FIXME: gracefully handle superfluous -v
238if (++operation_specified > 1) {
239fprintf(stderr, "More than one operation "
240"specified. Aborting.\n");
241cli_classic_abort_usage();
242}
243if (dont_verify_it) {
244fprintf(stderr, "--verify and --noverify are"
245"mutually exclusive. Aborting.\n");
246cli_classic_abort_usage();
247}
248filename = strdup(optarg);
249verify_it = 1;
250break;
251case 'n':
252if (verify_it) {
253fprintf(stderr, "--verify and --noverify are"
254"mutually exclusive. Aborting.\n");
255cli_classic_abort_usage();
256}
257dont_verify_it = 1;
258break;
259case 'c':
260chip_to_probe = strdup(optarg);
261break;
262case 'V':
263verbose++;
264break;
265case 'E':
266if (++operation_specified > 1) {
267fprintf(stderr, "More than one operation "
268"specified. Aborting.\n");
269cli_classic_abort_usage();
270}
271erase_it = 1;
272break;
273case 'f':
274force = 1;
275break;
276case 'l':
277if (layoutfile) {
278fprintf(stderr, "Error: --layout specified "
279"more than once. Aborting.\n");
280cli_classic_abort_usage();
281}
282layoutfile = strdup(optarg);
283break;
284case 'i':
285tempstr = strdup(optarg);
286if (register_include_arg(tempstr))
287cli_classic_abort_usage();
288break;
289case 'L':
290if (++operation_specified > 1) {
291fprintf(stderr, "More than one operation "
292"specified. Aborting.\n");
293cli_classic_abort_usage();
294}
295list_supported = 1;
296break;
297case 'z':
298#if CONFIG_PRINT_WIKI == 1
299if (++operation_specified > 1) {
300fprintf(stderr, "More than one operation "
301"specified. Aborting.\n");
302cli_classic_abort_usage();
303}
304list_supported_wiki = 1;
305#else
306fprintf(stderr, "Error: Wiki output was not compiled "
307"in. Aborting.\n");
308cli_classic_abort_usage();
309#endif
310break;
311case 'p':
312if (prog != PROGRAMMER_INVALID) {
313fprintf(stderr, "Error: --programmer specified "
314"more than once. You can separate "
315"multiple\nparameters for a programmer "
316"with \",\". Please see the man page "
317"for details.\n");
318cli_classic_abort_usage();
319}
320for (prog = 0; prog < PROGRAMMER_INVALID; prog++) {
321name = programmer_table[prog].name;
322namelen = strlen(name);
323if (strncmp(optarg, name, namelen) == 0) {
324switch (optarg[namelen]) {
325case ':':
326pparam = strdup(optarg + namelen + 1);
327if (!strlen(pparam)) {
328free(pparam);
329pparam = NULL;
330}
331break;
332case '\0':
333break;
334default:
335/* The continue refers to the
336 * for loop. It is here to be
337 * able to differentiate between
338 * foo and foobar.
339 */
340continue;
341}
342break;
343}
344}
345if (prog == PROGRAMMER_INVALID) {
346fprintf(stderr, "Error: Unknown programmer "
347"%s.\n", optarg);
348cli_classic_abort_usage();
349}
350break;
351case 'R':
352/* print_version() is always called during startup. */
353if (++operation_specified > 1) {
354fprintf(stderr, "More than one operation "
355"specified. Aborting.\n");
356cli_classic_abort_usage();
357}
358exit(0);
359break;
360case 'h':
361if (++operation_specified > 1) {
362fprintf(stderr, "More than one operation "
363"specified. Aborting.\n");
364cli_classic_abort_usage();
365}
366cli_classic_usage(argv[0]);
367exit(0);
368break;
369default:
370cli_classic_abort_usage();
371break;
372}
373}
374
375if (optind < argc) {
376fprintf(stderr, "Error: Extra parameter found.\n");
377cli_classic_abort_usage();
378}
379
380/* FIXME: Print the actions flashrom will take. */
381
382if (list_supported) {
383print_supported();
384exit(0);
385}
386
387#if CONFIG_PRINT_WIKI == 1
388if (list_supported_wiki) {
389print_supported_wiki();
390exit(0);
391}
392#endif
393
394if (layoutfile && read_romlayout(layoutfile))
395cli_classic_abort_usage();
396if (process_include_args())
397cli_classic_abort_usage();
398
399/* Does a chip with the requested name exist in the flashchips array? */
400if (chip_to_probe) {
401for (flash = flashchips; flash && flash->name; flash++)
402if (!strcmp(flash->name, chip_to_probe))
403break;
404if (!flash || !flash->name) {
405fprintf(stderr, "Error: Unknown chip '%s' specified.\n",
406chip_to_probe);
407printf("Run flashrom -L to view the hardware supported "
408 "in this flashrom version.\n");
409exit(1);
410}
411/* Clean up after the check. */
412flash = NULL;
413}
414
415if (prog == PROGRAMMER_INVALID)
416prog = default_programmer;
417
418/* FIXME: Delay calibration should happen in programmer code. */
419myusec_calibrate_delay();
420
421if (programmer_init(prog, pparam)) {
422fprintf(stderr, "Error: Programmer initialization failed.\n");
423ret = 1;
424goto out_shutdown;
425}
426tempstr = flashbuses_to_text(get_buses_supported());
427msg_pdbg("The following protocols are supported: %s.\n",
428 tempstr);
429free(tempstr);
430
431for (j = 0; j < registered_programmer_count; j++) {
432startchip = 0;
433while (chipcount < ARRAY_SIZE(flashes)) {
434startchip = probe_flash(&registered_programmers[j],
435startchip,
436&flashes[chipcount], 0);
437if (startchip == -1)
438break;
439chipcount++;
440startchip++;
441}
442}
443
444if (chipcount > 1) {
445printf("Multiple flash chips were detected: \"%s\"",
446flashes[0].name);
447for (i = 1; i < chipcount; i++)
448printf(", \"%s\"", flashes[i].name);
449printf("\nPlease specify which chip to use with the "
450 "-c <chipname> option.\n");
451ret = 1;
452goto out_shutdown;
453} else if (!chipcount) {
454printf("No EEPROM/flash device found.\n");
455if (!force || !chip_to_probe) {
456printf("Note: flashrom can never write if the flash "
457 "chip isn't found automatically.\n");
458}
459#if 0 // FIXME: What happens for a forced chip read if multiple compatible programmers are registered?
460if (force && read_it && chip_to_probe) {
461printf("Force read (-f -r -c) requested, pretending "
462 "the chip is there:\n");
463startchip = probe_flash(0, &flashes[0], 1);
464if (startchip == -1) {
465printf("Probing for flash chip '%s' failed.\n",
466 chip_to_probe);
467ret = 1;
468goto out_shutdown;
469}
470printf("Please note that forced reads most likely "
471 "contain garbage.\n");
472return read_flash_to_file(&flashes[0], filename);
473}
474#endif
475ret = 1;
476goto out_shutdown;
477} else if (!chip_to_probe) {
478/* repeat for convenience when looking at foreign logs */
479tempstr = flashbuses_to_text(flashes[0].bustype);
480msg_gdbg("Found %s flash chip \"%s\" (%d kB, %s).\n",
481 flashes[0].vendor, flashes[0].name,
482 flashes[0].total_size, tempstr);
483free(tempstr);
484}
485
486fill_flash = &flashes[0];
487
488check_chip_supported(fill_flash);
489
490size = fill_flash->total_size * 1024;
491if (check_max_decode(fill_flash->pgm->buses_supported & fill_flash->bustype, size) &&
492 (!force)) {
493fprintf(stderr, "Chip is too big for this programmer "
494"(-V gives details). Use --force to override.\n");
495ret = 1;
496goto out_shutdown;
497}
498
499if (!(read_it | write_it | verify_it | erase_it)) {
500printf("No operations were specified.\n");
501goto out_shutdown;
502}
503
504if (!filename && !erase_it) {
505printf("Error: No filename specified.\n");
506ret = 1;
507goto out_shutdown;
508}
509
510/* Always verify write operations unless -n is used. */
511if (write_it && !dont_verify_it)
512verify_it = 1;
513
514/* FIXME: We should issue an unconditional chip reset here. This can be
515 * done once we have a .reset function in struct flashchip.
516 * Give the chip time to settle.
517 */
518programmer_delay(100000);
519return doit(fill_flash, force, filename, read_it, write_it, erase_it, verify_it);
520
521out_shutdown:
522programmer_shutdown();
523return ret;
524}
525

Archive Download this file

Revision: HEAD