flashrom 

flashrom Svn Source Tree

Root/trunk/cli_classic.c

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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
34static void cli_classic_usage(const char *name)
35{
36printf("Please note that the command line interface for flashrom has changed between\n"
37 "0.9.5 and 0.9.6 and will change again before flashrom 1.0.\n\n");
38
39printf("Usage: %s [-h|-R|-L|"
40#if CONFIG_PRINT_WIKI == 1
41 "-z|"
42#endif
43 "-p <programmername>[:<parameters>] [-c <chipname>]\n"
44 "[-E|(-r|-w|-v) <file>] [-l <layoutfile> [-i <imagename>]...] [-n] [-f]]\n"
45 "[-V[V[V]]] [-o <logfile>]\n\n", name);
46
47printf(" -h | --help print this help text\n"
48 " -R | --version print version (release)\n"
49 " -r | --read <file> read flash and save to <file>\n"
50 " -w | --write <file> write <file> to flash\n"
51 " -v | --verify <file> verify flash against <file>\n"
52 " -E | --erase erase flash memory\n"
53 " -V | --verbose more verbose output\n"
54 " -c | --chip <chipname> probe only for specified flash chip\n"
55 " -f | --force force specific operations (see man page)\n"
56 " -n | --noverify don't auto-verify\n"
57 " -l | --layout <layoutfile> read ROM layout from <layoutfile>\n"
58 " -i | --image <name> only flash image <name> from flash layout\n"
59 " -o | --output <logfile> log output to <logfile>\n"
60 " -L | --list-supported print supported devices\n"
61#if CONFIG_PRINT_WIKI == 1
62 " -z | --list-supported-wiki print supported devices in wiki syntax\n"
63#endif
64 " -p | --programmer <name>[:<param>] specify the programmer device. One of\n");
65list_programmers_linebreak(4, 80, 0);
66printf(".\n\nYou can specify one of -h, -R, -L, "
67#if CONFIG_PRINT_WIKI == 1
68 "-z, "
69#endif
70 "-E, -r, -w, -v or no operation.\n"
71 "If no operation is specified, flashrom will only probe for flash chips.\n");
72}
73
74static void cli_classic_abort_usage(void)
75{
76printf("Please run \"flashrom --help\" for usage info.\n");
77exit(1);
78}
79
80static int check_filename(char *filename, char *type)
81{
82if (!filename || (filename[0] == '\0')) {
83fprintf(stderr, "Error: No %s file specified.\n", type);
84return 1;
85}
86/* Not an error, but maybe the user intended to specify a CLI option instead of a file name. */
87if (filename[0] == '-')
88fprintf(stderr, "Warning: Supplied %s file name starts with -\n", type);
89return 0;
90}
91
92int main(int argc, char *argv[])
93{
94const struct flashchip *chip = NULL;
95/* Probe for up to eight flash chips. */
96struct flashctx flashes[8] = {{0}};
97struct flashctx *fill_flash;
98const char *name;
99int namelen, opt, i, j;
100int startchip = -1, chipcount = 0, option_index = 0, force = 0;
101#if CONFIG_PRINT_WIKI == 1
102int list_supported_wiki = 0;
103#endif
104int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
105int dont_verify_it = 0, list_supported = 0, operation_specified = 0;
106enum programmer prog = PROGRAMMER_INVALID;
107int ret = 0;
108
109static const char optstring[] = "r:Rw:v:nVEfc:l:i:p:Lzho:";
110static const struct option long_options[] = {
111{"read",1, NULL, 'r'},
112{"write",1, NULL, 'w'},
113{"erase",0, NULL, 'E'},
114{"verify",1, NULL, 'v'},
115{"noverify",0, NULL, 'n'},
116{"chip",1, NULL, 'c'},
117{"verbose",0, NULL, 'V'},
118{"force",0, NULL, 'f'},
119{"layout",1, NULL, 'l'},
120{"image",1, NULL, 'i'},
121{"list-supported",0, NULL, 'L'},
122{"list-supported-wiki",0, NULL, 'z'},
123{"programmer",1, NULL, 'p'},
124{"help",0, NULL, 'h'},
125{"version",0, NULL, 'R'},
126{"output",1, NULL, 'o'},
127{NULL,0, NULL, 0},
128};
129
130char *filename = NULL;
131char *layoutfile = NULL;
132#ifndef STANDALONE
133char *logfile = NULL;
134#endif /* !STANDALONE */
135char *tempstr = NULL;
136char *pparam = NULL;
137
138print_version();
139print_banner();
140
141if (selfcheck())
142exit(1);
143
144setbuf(stdout, NULL);
145/* FIXME: Delay all operation_specified checks until after command
146 * line parsing to allow --help overriding everything else.
147 */
148while ((opt = getopt_long(argc, argv, optstring,
149 long_options, &option_index)) != EOF) {
150switch (opt) {
151case 'r':
152if (++operation_specified > 1) {
153fprintf(stderr, "More than one operation "
154"specified. Aborting.\n");
155cli_classic_abort_usage();
156}
157filename = strdup(optarg);
158read_it = 1;
159break;
160case 'w':
161if (++operation_specified > 1) {
162fprintf(stderr, "More than one operation "
163"specified. Aborting.\n");
164cli_classic_abort_usage();
165}
166filename = strdup(optarg);
167write_it = 1;
168break;
169case 'v':
170//FIXME: gracefully handle superfluous -v
171if (++operation_specified > 1) {
172fprintf(stderr, "More than one operation "
173"specified. Aborting.\n");
174cli_classic_abort_usage();
175}
176if (dont_verify_it) {
177fprintf(stderr, "--verify and --noverify are mutually exclusive. Aborting.\n");
178cli_classic_abort_usage();
179}
180filename = strdup(optarg);
181verify_it = 1;
182break;
183case 'n':
184if (verify_it) {
185fprintf(stderr, "--verify and --noverify are mutually exclusive. Aborting.\n");
186cli_classic_abort_usage();
187}
188dont_verify_it = 1;
189break;
190case 'c':
191chip_to_probe = strdup(optarg);
192break;
193case 'V':
194verbose_screen++;
195if (verbose_screen > MSG_DEBUG2)
196verbose_logfile = verbose_screen;
197break;
198case 'E':
199if (++operation_specified > 1) {
200fprintf(stderr, "More than one operation "
201"specified. Aborting.\n");
202cli_classic_abort_usage();
203}
204erase_it = 1;
205break;
206case 'f':
207force = 1;
208break;
209case 'l':
210if (layoutfile) {
211fprintf(stderr, "Error: --layout specified "
212"more than once. Aborting.\n");
213cli_classic_abort_usage();
214}
215layoutfile = strdup(optarg);
216break;
217case 'i':
218tempstr = strdup(optarg);
219if (register_include_arg(tempstr)) {
220free(tempstr);
221cli_classic_abort_usage();
222}
223break;
224case 'L':
225if (++operation_specified > 1) {
226fprintf(stderr, "More than one operation "
227"specified. Aborting.\n");
228cli_classic_abort_usage();
229}
230list_supported = 1;
231break;
232case 'z':
233#if CONFIG_PRINT_WIKI == 1
234if (++operation_specified > 1) {
235fprintf(stderr, "More than one operation "
236"specified. Aborting.\n");
237cli_classic_abort_usage();
238}
239list_supported_wiki = 1;
240#else
241fprintf(stderr, "Error: Wiki output was not compiled "
242"in. Aborting.\n");
243cli_classic_abort_usage();
244#endif
245break;
246case 'p':
247if (prog != PROGRAMMER_INVALID) {
248fprintf(stderr, "Error: --programmer specified "
249"more than once. You can separate "
250"multiple\nparameters for a programmer "
251"with \",\". Please see the man page "
252"for details.\n");
253cli_classic_abort_usage();
254}
255for (prog = 0; prog < PROGRAMMER_INVALID; prog++) {
256name = programmer_table[prog].name;
257namelen = strlen(name);
258if (strncmp(optarg, name, namelen) == 0) {
259switch (optarg[namelen]) {
260case ':':
261pparam = strdup(optarg + namelen + 1);
262if (!strlen(pparam)) {
263free(pparam);
264pparam = NULL;
265}
266break;
267case '\0':
268break;
269default:
270/* The continue refers to the
271 * for loop. It is here to be
272 * able to differentiate between
273 * foo and foobar.
274 */
275continue;
276}
277break;
278}
279}
280if (prog == PROGRAMMER_INVALID) {
281fprintf(stderr, "Error: Unknown programmer \"%s\". Valid choices are:\n",
282optarg);
283list_programmers_linebreak(0, 80, 0);
284msg_ginfo(".\n");
285cli_classic_abort_usage();
286}
287break;
288case 'R':
289/* print_version() is always called during startup. */
290if (++operation_specified > 1) {
291fprintf(stderr, "More than one operation "
292"specified. Aborting.\n");
293cli_classic_abort_usage();
294}
295exit(0);
296break;
297case 'h':
298if (++operation_specified > 1) {
299fprintf(stderr, "More than one operation "
300"specified. Aborting.\n");
301cli_classic_abort_usage();
302}
303cli_classic_usage(argv[0]);
304exit(0);
305break;
306case 'o':
307#ifdef STANDALONE
308fprintf(stderr, "Log file not supported in standalone mode. Aborting.\n");
309cli_classic_abort_usage();
310#else /* STANDALONE */
311logfile = strdup(optarg);
312if (logfile[0] == '\0') {
313fprintf(stderr, "No log filename specified.\n");
314cli_classic_abort_usage();
315}
316#endif /* STANDALONE */
317break;
318default:
319cli_classic_abort_usage();
320break;
321}
322}
323
324if (optind < argc) {
325fprintf(stderr, "Error: Extra parameter found.\n");
326cli_classic_abort_usage();
327}
328
329if ((read_it | write_it | verify_it) && check_filename(filename, "image")) {
330cli_classic_abort_usage();
331}
332if (layoutfile && check_filename(layoutfile, "layout")) {
333cli_classic_abort_usage();
334}
335
336#ifndef STANDALONE
337if (logfile && check_filename(logfile, "log"))
338cli_classic_abort_usage();
339if (logfile && open_logfile(logfile))
340cli_classic_abort_usage();
341#endif /* !STANDALONE */
342
343#if CONFIG_PRINT_WIKI == 1
344if (list_supported_wiki) {
345print_supported_wiki();
346goto out;
347}
348#endif
349
350if (list_supported) {
351if (print_supported())
352ret = 1;
353goto out;
354}
355
356#ifndef STANDALONE
357start_logging();
358#endif /* !STANDALONE */
359
360print_buildinfo();
361msg_gdbg("Command line (%i args):", argc - 1);
362for (i = 0; i < argc; i++) {
363msg_gdbg(" %s", argv[i]);
364}
365msg_gdbg("\n");
366
367if (layoutfile && read_romlayout(layoutfile)) {
368ret = 1;
369goto out;
370}
371if (layoutfile != NULL && !write_it) {
372msg_gerr("Layout files are currently supported for write operations only.\n");
373ret = 1;
374goto out;
375}
376
377if (process_include_args()) {
378ret = 1;
379goto out;
380}
381/* Does a chip with the requested name exist in the flashchips array? */
382if (chip_to_probe) {
383for (chip = flashchips; chip && chip->name; chip++)
384if (!strcmp(chip->name, chip_to_probe))
385break;
386if (!chip || !chip->name) {
387msg_cerr("Error: Unknown chip '%s' specified.\n", chip_to_probe);
388msg_gerr("Run flashrom -L to view the hardware supported in this flashrom version.\n");
389ret = 1;
390goto out;
391}
392/* Keep chip around for later usage in case a forced read is requested. */
393}
394
395if (prog == PROGRAMMER_INVALID) {
396if (CONFIG_DEFAULT_PROGRAMMER != PROGRAMMER_INVALID) {
397prog = CONFIG_DEFAULT_PROGRAMMER;
398/* We need to strdup here because we free(pparam) unconditionally later. */
399pparam = strdup(CONFIG_DEFAULT_PROGRAMMER_ARGS);
400msg_pinfo("Using default programmer \"%s\" with arguments \"%s\".\n",
401 programmer_table[CONFIG_DEFAULT_PROGRAMMER].name, pparam);
402} else {
403msg_perr("Please select a programmer with the --programmer parameter.\n"
404 "Previously this was not necessary because there was a default set.\n"
405#if CONFIG_INTERNAL == 1
406 "To choose the mainboard of this computer use 'internal'. "
407#endif
408 "Valid choices are:\n");
409list_programmers_linebreak(0, 80, 0);
410msg_ginfo(".\n");
411ret = 1;
412goto out;
413}
414}
415
416/* FIXME: Delay calibration should happen in programmer code. */
417myusec_calibrate_delay();
418
419if (programmer_init(prog, pparam)) {
420msg_perr("Error: Programmer initialization failed.\n");
421ret = 1;
422goto out_shutdown;
423}
424tempstr = flashbuses_to_text(get_buses_supported());
425msg_pdbg("The following protocols are supported: %s.\n", tempstr);
426free(tempstr);
427
428for (j = 0; j < registered_master_count; j++) {
429startchip = 0;
430while (chipcount < ARRAY_SIZE(flashes)) {
431startchip = probe_flash(&registered_masters[j], startchip, &flashes[chipcount], 0);
432if (startchip == -1)
433break;
434chipcount++;
435startchip++;
436}
437}
438
439if (chipcount > 1) {
440msg_cinfo("Multiple flash chip definitions match the detected chip(s): \"%s\"",
441 flashes[0].chip->name);
442for (i = 1; i < chipcount; i++)
443msg_cinfo(", \"%s\"", flashes[i].chip->name);
444msg_cinfo("\nPlease specify which chip definition to use with the -c <chipname> option.\n");
445ret = 1;
446goto out_shutdown;
447} else if (!chipcount) {
448msg_cinfo("No EEPROM/flash device found.\n");
449if (!force || !chip_to_probe) {
450msg_cinfo("Note: flashrom can never write if the flash chip isn't found "
451 "automatically.\n");
452}
453if (force && read_it && chip_to_probe) {
454struct registered_master *mst;
455int compatible_masters = 0;
456msg_cinfo("Force read (-f -r -c) requested, pretending the chip is there:\n");
457/* This loop just counts compatible controllers. */
458for (j = 0; j < registered_master_count; j++) {
459mst = &registered_masters[j];
460/* chip is still set from the chip_to_probe earlier in this function. */
461if (mst->buses_supported & chip->bustype)
462compatible_masters++;
463}
464if (!compatible_masters) {
465msg_cinfo("No compatible controller found for the requested flash chip.\n");
466ret = 1;
467goto out_shutdown;
468}
469if (compatible_masters > 1)
470msg_cinfo("More than one compatible controller found for the requested flash "
471 "chip, using the first one.\n");
472for (j = 0; j < registered_master_count; j++) {
473mst = &registered_masters[j];
474startchip = probe_flash(mst, 0, &flashes[0], 1);
475if (startchip != -1)
476break;
477}
478if (startchip == -1) {
479// FIXME: This should never happen! Ask for a bug report?
480msg_cinfo("Probing for flash chip '%s' failed.\n", chip_to_probe);
481ret = 1;
482goto out_shutdown;
483}
484if (map_flash(&flashes[0]) != 0) {
485free(flashes[0].chip);
486ret = 1;
487goto out_shutdown;
488}
489msg_cinfo("Please note that forced reads most likely contain garbage.\n");
490ret = read_flash_to_file(&flashes[0], filename);
491unmap_flash(&flashes[0]);
492free(flashes[0].chip);
493goto out_shutdown;
494}
495ret = 1;
496goto out_shutdown;
497} else if (!chip_to_probe) {
498/* repeat for convenience when looking at foreign logs */
499tempstr = flashbuses_to_text(flashes[0].chip->bustype);
500msg_gdbg("Found %s flash chip \"%s\" (%d kB, %s).\n",
501 flashes[0].chip->vendor, flashes[0].chip->name, flashes[0].chip->total_size, tempstr);
502free(tempstr);
503}
504
505fill_flash = &flashes[0];
506
507print_chip_support_status(fill_flash->chip);
508
509unsigned int limitexceeded = count_max_decode_exceedings(fill_flash);
510if (limitexceeded > 0 && !force) {
511enum chipbustype commonbuses = fill_flash->mst->buses_supported & fill_flash->chip->bustype;
512
513/* Sometimes chip and programmer have more than one bus in common,
514 * and the limit is not exceeded on all buses. Tell the user. */
515if ((bitcount(commonbuses) > limitexceeded)) {
516msg_pdbg("There is at least one interface available which could support the size of\n"
517 "the selected flash chip.\n");
518}
519msg_cerr("This flash chip is too big for this programmer (--verbose/-V gives details).\n"
520 "Use --force/-f to override at your own risk.\n");
521ret = 1;
522goto out_shutdown;
523}
524
525if (!(read_it | write_it | verify_it | erase_it)) {
526msg_ginfo("No operations were specified.\n");
527goto out_shutdown;
528}
529
530/* Always verify write operations unless -n is used. */
531if (write_it && !dont_verify_it)
532verify_it = 1;
533
534/* Map the selected flash chip again. */
535if (map_flash(fill_flash) != 0) {
536ret = 1;
537goto out_shutdown;
538}
539
540/* 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 */
544programmer_delay(100000);
545ret |= doit(fill_flash, force, filename, read_it, write_it, erase_it, verify_it);
546
547unmap_flash(fill_flash);
548out_shutdown:
549programmer_shutdown();
550out:
551for (i = 0; i < chipcount; i++)
552free(flashes[i].chip);
553
554layout_cleanup();
555free(filename);
556free(layoutfile);
557free(pparam);
558/* clean up global variables */
559free((char *)chip_to_probe); /* Silence! Freeing is not modifying contents. */
560chip_to_probe = NULL;
561#ifndef STANDALONE
562free(logfile);
563ret |= close_logfile();
564#endif /* !STANDALONE */
565return ret;
566}

Archive Download this file

Revision: HEAD