blob: 24762fe4d7d3629d0244b89d8bf51e8cb898576c [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);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000251 if (!strlen(programmer_param)) {
252 free(programmer_param);
253 programmer_param = NULL;
254 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000255 break;
256 case '\0':
257 break;
258 default:
259 /* The continue refers to the
260 * for loop. It is here to be
261 * able to differentiate between
262 * foo and foobar.
263 */
264 continue;
265 }
266 break;
267 }
268 }
269 if (programmer == PROGRAMMER_INVALID) {
270 printf("Error: Unknown programmer %s.\n", optarg);
271 exit(1);
272 }
273 break;
274 case 'R':
275 /* print_version() is always called during startup. */
276 exit(0);
277 break;
278 case 'h':
279 default:
280 cli_classic_usage(argv[0]);
281 break;
282 }
283 }
284
285 if (list_supported) {
286 print_supported();
287 exit(0);
288 }
289
290#if PRINT_WIKI_SUPPORT == 1
291 if (list_supported_wiki) {
292 print_supported_wiki();
293 exit(0);
294 }
295#endif
296
297 if (read_it && write_it) {
298 printf("Error: -r and -w are mutually exclusive.\n");
299 cli_classic_usage(argv[0]);
300 }
301
302 if (optind < argc)
303 filename = argv[optind++];
304
305 if (optind < argc) {
306 printf("Error: Extra parameter found.\n");
307 cli_classic_usage(argv[0]);
308 }
309
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000310 if (chip_to_probe) {
311 for (flash = flashchips; flash && flash->name; flash++)
312 if (!strcmp(flash->name, chip_to_probe))
313 break;
314 if (!flash || !flash->name) {
315 fprintf(stderr, "Error: Unknown chip '%s' specified.\n",
316 chip_to_probe);
317 printf("Run flashrom -L to view the hardware supported "
318 "in this flashrom version.\n");
319 exit(1);
320 }
321 /* Clean up after the check. */
322 flash = NULL;
323 }
324
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000325 if (programmer_init()) {
326 fprintf(stderr, "Error: Programmer initialization failed.\n");
327 exit(1);
328 }
329
330 // FIXME: Delay calibration should happen in programmer code.
331 myusec_calibrate_delay();
332
333 for (i = 0; i < ARRAY_SIZE(flashes); i++) {
334 flashes[i] =
335 probe_flash(i ? flashes[i - 1] + 1 : flashchips, 0);
336 if (!flashes[i])
337 for (i++; i < ARRAY_SIZE(flashes); i++)
338 flashes[i] = NULL;
339 }
340
341 if (flashes[1]) {
342 printf("Multiple flash chips were detected:");
343 for (i = 0; i < ARRAY_SIZE(flashes) && flashes[i]; i++)
344 printf(" %s", flashes[i]->name);
345 printf("\nPlease specify which chip to use with the -c <chipname> option.\n");
346 programmer_shutdown();
347 exit(1);
348 } else if (!flashes[0]) {
349 printf("No EEPROM/flash device found.\n");
350 if (!force || !chip_to_probe) {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000351 printf("Note: flashrom can never write if the flash chip isn't found automatically.\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000352 }
353 if (force && read_it && chip_to_probe) {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000354 printf("Force read (-f -r -c) requested, pretending the chip is there:\n");
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000355 flashes[0] = probe_flash(flashchips, 1);
356 if (!flashes[0]) {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000357 printf("Probing for flash chip '%s' failed.\n", chip_to_probe);
358 programmer_shutdown();
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000359 exit(1);
360 }
361 printf("Please note that forced reads most likely contain garbage.\n");
362 return read_flash(flashes[0], filename);
363 }
364 // FIXME: flash writes stay enabled!
365 programmer_shutdown();
366 exit(1);
367 }
368
369 flash = flashes[0];
370
371 check_chip_supported(flash);
372
373 size = flash->total_size * 1024;
374 if (check_max_decode((buses_supported & flash->bustype), size) &&
375 (!force)) {
376 fprintf(stderr, "Chip is too big for this programmer "
377 "(-V gives details). Use --force to override.\n");
378 programmer_shutdown();
379 return 1;
380 }
381
382 if (!(read_it | write_it | verify_it | erase_it)) {
383 printf("No operations were specified.\n");
384 // FIXME: flash writes stay enabled!
385 programmer_shutdown();
386 exit(1);
387 }
388
389 if (!filename && !erase_it) {
390 printf("Error: No filename specified.\n");
391 // FIXME: flash writes stay enabled!
392 programmer_shutdown();
393 exit(1);
394 }
395
396 /* Always verify write operations unless -n is used. */
397 if (write_it && !dont_verify_it)
398 verify_it = 1;
399
400 return doit(flash, force, filename, read_it, write_it, erase_it, verify_it);
401}