cli_classic.c 15 KB

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