blob: 6cb0578f0be72e35ec39c531fee28800749e3aad [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
40 printf("Usage: %s [-VfLzhR] [-E|-r file|-w file|-v file] [-c chipname]\n"
41 " [-m [vendor:]part] [-l file] [-i image] [-p programmer]\n\n", name);
42
43 printf("Please note that the command line interface for flashrom will "
44 "change before\nflashrom 1.0. Do not use flashrom in scripts "
45 "or other automated tools without\nchecking that your flashrom"
46 " version won't interpret options in a different way.\n\n");
47
48 printf
49 (" -r | --read: read flash and save into file\n"
50 " -w | --write: write file into flash\n"
51 " -v | --verify: verify flash against file\n"
52 " -n | --noverify: don't verify flash against file\n"
53 " -E | --erase: erase flash device\n"
54 " -V | --verbose: more verbose output\n"
55 " -c | --chip <chipname>: probe only for specified flash chip\n"
56#if INTERNAL_SUPPORT == 1
57 " -m | --mainboard <[vendor:]part>: override mainboard settings\n"
58#endif
59 " -f | --force: force write without checking image\n"
60 " -l | --layout <file.layout>: read ROM layout from file\n"
61 " -i | --image <name>: only flash image name from flash layout\n"
62 " -L | --list-supported: print supported devices\n"
63#if PRINT_WIKI_SUPPORT == 1
64 " -z | --list-supported-wiki: print supported devices in wiki syntax\n"
65#endif
66 " -p | --programmer <name>: specify the programmer device");
67
68 for (p = 0; p < PROGRAMMER_INVALID; p++) {
69 pname = programmer_table[p].name;
70 pnamelen = strlen(pname);
71 if (remaining - pnamelen - 2 < 0) {
72 printf("\n ");
73 remaining = 43;
74 } else {
75 printf(" ");
76 remaining--;
77 }
78 if (p == 0) {
79 printf("(");
80 remaining--;
81 }
82 printf("%s", pname);
83 remaining -= pnamelen;
84 if (p < PROGRAMMER_INVALID - 1) {
85 printf(",");
86 remaining--;
87 } else {
88 printf(")\n");
89 }
90 }
91
92 printf(
93 " -h | --help: print this help text\n"
94 " -R | --version: print the version (release)\n"
95 "\nYou can specify one of -E, -r, -w, -v or no operation. If no operation is\n"
96 "specified, then all that happens is that flash info is dumped.\n\n");
97 exit(1);
98}
99
100int cli_classic(int argc, char *argv[])
101{
102 unsigned long size;
103 /* Probe for up to three flash chips. */
104 struct flashchip *flash, *flashes[3];
105 const char *name;
106 int namelen;
107 int opt;
108 int option_index = 0;
109 int force = 0;
110 int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
111 int dont_verify_it = 0, list_supported = 0;
112#if PRINT_WIKI_SUPPORT == 1
113 int list_supported_wiki = 0;
114#endif
115 int operation_specified = 0;
116 int i;
117
118#if PRINT_WIKI_SUPPORT == 1
119 const char *optstring = "rRwvnVEfc:m:l:i:p:Lzh";
120#else
121 const char *optstring = "rRwvnVEfc:m:l:i:p:Lh";
122#endif
123 static struct option long_options[] = {
124 {"read", 0, 0, 'r'},
125 {"write", 0, 0, 'w'},
126 {"erase", 0, 0, 'E'},
127 {"verify", 0, 0, 'v'},
128 {"noverify", 0, 0, 'n'},
129 {"chip", 1, 0, 'c'},
130 {"mainboard", 1, 0, 'm'},
131 {"verbose", 0, 0, 'V'},
132 {"force", 0, 0, 'f'},
133 {"layout", 1, 0, 'l'},
134 {"image", 1, 0, 'i'},
135 {"list-supported", 0, 0, 'L'},
136#if PRINT_WIKI_SUPPORT == 1
137 {"list-supported-wiki", 0, 0, 'z'},
138#endif
139 {"programmer", 1, 0, 'p'},
140 {"help", 0, 0, 'h'},
141 {"version", 0, 0, 'R'},
142 {0, 0, 0, 0}
143 };
144
145 char *filename = NULL;
146
147 char *tempstr = NULL;
148
149 print_version();
150
151 if (argc > 1) {
152 /* Yes, print them. */
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000153 printf_debug("The arguments are:\n");
154 for (i = 1; i < argc; ++i)
155 printf_debug("%s\n", argv[i]);
156 }
157
158 if (selfcheck())
159 exit(1);
160
161 setbuf(stdout, NULL);
162 while ((opt = getopt_long(argc, argv, optstring,
163 long_options, &option_index)) != EOF) {
164 switch (opt) {
165 case 'r':
166 if (++operation_specified > 1) {
167 fprintf(stderr, "More than one operation "
168 "specified. Aborting.\n");
169 exit(1);
170 }
171 read_it = 1;
172 break;
173 case 'w':
174 if (++operation_specified > 1) {
175 fprintf(stderr, "More than one operation "
176 "specified. Aborting.\n");
177 exit(1);
178 }
179 write_it = 1;
180 break;
181 case 'v':
182 //FIXME: gracefully handle superfluous -v
183 if (++operation_specified > 1) {
184 fprintf(stderr, "More than one operation "
185 "specified. Aborting.\n");
186 exit(1);
187 }
188 if (dont_verify_it) {
189 fprintf(stderr, "--verify and --noverify are"
190 "mutually exclusive. Aborting.\n");
191 exit(1);
192 }
193 verify_it = 1;
194 break;
195 case 'n':
196 if (verify_it) {
197 fprintf(stderr, "--verify and --noverify are"
198 "mutually exclusive. Aborting.\n");
199 exit(1);
200 }
201 dont_verify_it = 1;
202 break;
203 case 'c':
204 chip_to_probe = strdup(optarg);
205 break;
206 case 'V':
Sean Nelson51e97d72010-01-07 20:09:33 +0000207 verbose++;
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000208 break;
209 case 'E':
210 if (++operation_specified > 1) {
211 fprintf(stderr, "More than one operation "
212 "specified. Aborting.\n");
213 exit(1);
214 }
215 erase_it = 1;
216 break;
217#if INTERNAL_SUPPORT == 1
218 case 'm':
219 tempstr = strdup(optarg);
220 lb_vendor_dev_from_string(tempstr);
221 break;
222#endif
223 case 'f':
224 force = 1;
225 break;
226 case 'l':
227 tempstr = strdup(optarg);
228 if (read_romlayout(tempstr))
229 exit(1);
230 break;
231 case 'i':
232 tempstr = strdup(optarg);
233 find_romentry(tempstr);
234 break;
235 case 'L':
236 list_supported = 1;
237 break;
238#if PRINT_WIKI_SUPPORT == 1
239 case 'z':
240 list_supported_wiki = 1;
241 break;
242#endif
243 case 'p':
244 for (programmer = 0; programmer < PROGRAMMER_INVALID; programmer++) {
245 name = programmer_table[programmer].name;
246 namelen = strlen(name);
247 if (strncmp(optarg, name, namelen) == 0) {
248 switch (optarg[namelen]) {
249 case ':':
250 programmer_param = strdup(optarg + namelen + 1);
251 break;
252 case '\0':
253 break;
254 default:
255 /* The continue refers to the
256 * for loop. It is here to be
257 * able to differentiate between
258 * foo and foobar.
259 */
260 continue;
261 }
262 break;
263 }
264 }
265 if (programmer == PROGRAMMER_INVALID) {
266 printf("Error: Unknown programmer %s.\n", optarg);
267 exit(1);
268 }
269 break;
270 case 'R':
271 /* print_version() is always called during startup. */
272 exit(0);
273 break;
274 case 'h':
275 default:
276 cli_classic_usage(argv[0]);
277 break;
278 }
279 }
280
281 if (list_supported) {
282 print_supported();
283 exit(0);
284 }
285
286#if PRINT_WIKI_SUPPORT == 1
287 if (list_supported_wiki) {
288 print_supported_wiki();
289 exit(0);
290 }
291#endif
292
293 if (read_it && write_it) {
294 printf("Error: -r and -w are mutually exclusive.\n");
295 cli_classic_usage(argv[0]);
296 }
297
298 if (optind < argc)
299 filename = argv[optind++];
300
301 if (optind < argc) {
302 printf("Error: Extra parameter found.\n");
303 cli_classic_usage(argv[0]);
304 }
305
306 if (programmer_init()) {
307 fprintf(stderr, "Error: Programmer initialization failed.\n");
308 exit(1);
309 }
310
311 // FIXME: Delay calibration should happen in programmer code.
312 myusec_calibrate_delay();
313
314 for (i = 0; i < ARRAY_SIZE(flashes); i++) {
315 flashes[i] =
316 probe_flash(i ? flashes[i - 1] + 1 : flashchips, 0);
317 if (!flashes[i])
318 for (i++; i < ARRAY_SIZE(flashes); i++)
319 flashes[i] = NULL;
320 }
321
322 if (flashes[1]) {
323 printf("Multiple flash chips were detected:");
324 for (i = 0; i < ARRAY_SIZE(flashes) && flashes[i]; i++)
325 printf(" %s", flashes[i]->name);
326 printf("\nPlease specify which chip to use with the -c <chipname> option.\n");
327 programmer_shutdown();
328 exit(1);
329 } else if (!flashes[0]) {
330 printf("No EEPROM/flash device found.\n");
331 if (!force || !chip_to_probe) {
332 printf("If you know which flash chip you have, and if this version of flashrom\n");
333 printf("supports a similar flash chip, you can try to force read your chip. Run:\n");
334 printf("flashrom -f -r -c similar_supported_flash_chip filename\n");
335 printf("\n");
336 printf("Note: flashrom can never write when the flash chip isn't found automatically.\n");
337 }
338 if (force && read_it && chip_to_probe) {
339 printf("Force read (-f -r -c) requested, forcing chip probe success:\n");
340 flashes[0] = probe_flash(flashchips, 1);
341 if (!flashes[0]) {
342 printf("flashrom does not support a flash chip named '%s'.\n", chip_to_probe);
343 printf("Run flashrom -L to view the hardware supported in this flashrom version.\n");
344 exit(1);
345 }
346 printf("Please note that forced reads most likely contain garbage.\n");
347 return read_flash(flashes[0], filename);
348 }
349 // FIXME: flash writes stay enabled!
350 programmer_shutdown();
351 exit(1);
352 }
353
354 flash = flashes[0];
355
356 check_chip_supported(flash);
357
358 size = flash->total_size * 1024;
359 if (check_max_decode((buses_supported & flash->bustype), size) &&
360 (!force)) {
361 fprintf(stderr, "Chip is too big for this programmer "
362 "(-V gives details). Use --force to override.\n");
363 programmer_shutdown();
364 return 1;
365 }
366
367 if (!(read_it | write_it | verify_it | erase_it)) {
368 printf("No operations were specified.\n");
369 // FIXME: flash writes stay enabled!
370 programmer_shutdown();
371 exit(1);
372 }
373
374 if (!filename && !erase_it) {
375 printf("Error: No filename specified.\n");
376 // FIXME: flash writes stay enabled!
377 programmer_shutdown();
378 exit(1);
379 }
380
381 /* Always verify write operations unless -n is used. */
382 if (write_it && !dont_verify_it)
383 verify_it = 1;
384
385 return doit(flash, force, filename, read_it, write_it, erase_it, verify_it);
386}