You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

969 lines
23 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de> *
  3. * Copyright (C) 2007,2008 Øyvind Harboe <oyvind.harboe@zylin.com> *
  4. * Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk> *
  5. * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
  6. * *
  7. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation; either version 2 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with this program; if not, write to the *
  19. * Free Software Foundation, Inc., *
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  21. ***************************************************************************/
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include "imp.h"
  26. #include <helper/time_support.h>
  27. #include <target/image.h>
  28. /**
  29. * @file
  30. * Implements Tcl commands used to access NOR flash facilities.
  31. */
  32. COMMAND_HELPER(flash_command_get_bank, unsigned name_index,
  33. struct flash_bank **bank)
  34. {
  35. const char *name = CMD_ARGV[name_index];
  36. *bank = get_flash_bank_by_name(name);
  37. if (*bank)
  38. return ERROR_OK;
  39. unsigned bank_num;
  40. COMMAND_PARSE_NUMBER(uint, name, bank_num);
  41. *bank = get_flash_bank_by_num(bank_num);
  42. if (!*bank)
  43. {
  44. command_print(CMD_CTX, "flash bank '%s' not found", name);
  45. return ERROR_INVALID_ARGUMENTS;
  46. }
  47. return ERROR_OK;
  48. }
  49. COMMAND_HANDLER(handle_flash_info_command)
  50. {
  51. struct flash_bank *p;
  52. uint32_t i = 0;
  53. int j = 0;
  54. int retval;
  55. if (CMD_ARGC != 1)
  56. return ERROR_COMMAND_SYNTAX_ERROR;
  57. unsigned bank_nr;
  58. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], bank_nr);
  59. for (p = flash_bank_list(); p; p = p->next, i++)
  60. {
  61. if (i != bank_nr)
  62. continue;
  63. char buf[1024];
  64. /* attempt auto probe */
  65. if ((retval = p->driver->auto_probe(p)) != ERROR_OK)
  66. return retval;
  67. command_print(CMD_CTX,
  68. "#%" PRIi32 " : %s at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32 ", buswidth %i, chipwidth %i",
  69. i,
  70. p->driver->name,
  71. p->base,
  72. p->size,
  73. p->bus_width,
  74. p->chip_width);
  75. for (j = 0; j < p->num_sectors; j++)
  76. {
  77. char *protect_state;
  78. if (p->sectors[j].is_protected == 0)
  79. protect_state = "not protected";
  80. else if (p->sectors[j].is_protected == 1)
  81. protect_state = "protected";
  82. else
  83. protect_state = "protection state unknown";
  84. command_print(CMD_CTX,
  85. "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
  86. j,
  87. p->sectors[j].offset,
  88. p->sectors[j].size,
  89. p->sectors[j].size >> 10,
  90. protect_state);
  91. }
  92. *buf = '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
  93. retval = p->driver->info(p, buf, sizeof(buf));
  94. command_print(CMD_CTX, "%s", buf);
  95. if (retval != ERROR_OK)
  96. LOG_ERROR("error retrieving flash info (%d)", retval);
  97. }
  98. return ERROR_OK;
  99. }
  100. COMMAND_HANDLER(handle_flash_probe_command)
  101. {
  102. int retval;
  103. if (CMD_ARGC != 1)
  104. {
  105. return ERROR_COMMAND_SYNTAX_ERROR;
  106. }
  107. unsigned bank_nr;
  108. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], bank_nr);
  109. struct flash_bank *p = get_flash_bank_by_num_noprobe(bank_nr);
  110. if (p)
  111. {
  112. if ((retval = p->driver->probe(p)) == ERROR_OK)
  113. {
  114. command_print(CMD_CTX, "flash '%s' found at 0x%8.8" PRIx32, p->driver->name, p->base);
  115. }
  116. else if (retval == ERROR_FLASH_BANK_INVALID)
  117. {
  118. command_print(CMD_CTX, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32,
  119. CMD_ARGV[0], p->base);
  120. }
  121. else
  122. {
  123. command_print(CMD_CTX, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32,
  124. CMD_ARGV[0], p->base);
  125. }
  126. }
  127. else
  128. {
  129. command_print(CMD_CTX, "flash bank '#%s' is out of bounds", CMD_ARGV[0]);
  130. }
  131. return ERROR_OK;
  132. }
  133. COMMAND_HANDLER(handle_flash_erase_check_command)
  134. {
  135. if (CMD_ARGC != 1)
  136. {
  137. return ERROR_COMMAND_SYNTAX_ERROR;
  138. }
  139. struct flash_bank *p;
  140. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
  141. if (ERROR_OK != retval)
  142. return retval;
  143. int j;
  144. if ((retval = p->driver->erase_check(p)) == ERROR_OK)
  145. {
  146. command_print(CMD_CTX, "successfully checked erase state");
  147. }
  148. else
  149. {
  150. command_print(CMD_CTX, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32,
  151. CMD_ARGV[0], p->base);
  152. }
  153. for (j = 0; j < p->num_sectors; j++)
  154. {
  155. char *erase_state;
  156. if (p->sectors[j].is_erased == 0)
  157. erase_state = "not erased";
  158. else if (p->sectors[j].is_erased == 1)
  159. erase_state = "erased";
  160. else
  161. erase_state = "erase state unknown";
  162. command_print(CMD_CTX,
  163. "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
  164. j,
  165. p->sectors[j].offset,
  166. p->sectors[j].size,
  167. p->sectors[j].size >> 10,
  168. erase_state);
  169. }
  170. return ERROR_OK;
  171. }
  172. COMMAND_HANDLER(handle_flash_erase_address_command)
  173. {
  174. struct flash_bank *p;
  175. int retval;
  176. int address;
  177. int length;
  178. bool do_pad = false;
  179. struct target *target = get_current_target(CMD_CTX);
  180. switch (CMD_ARGC) {
  181. case 3:
  182. /* Optionally pad out the address range to block/sector
  183. * boundaries. We can't know if there's data in that part
  184. * of the flash; only do padding if we're told to.
  185. */
  186. if (strcmp("pad", CMD_ARGV[0]) != 0)
  187. return ERROR_COMMAND_SYNTAX_ERROR;
  188. do_pad = true;
  189. CMD_ARGC--;
  190. CMD_ARGV++;
  191. /* FALL THROUGH */
  192. case 2:
  193. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], address);
  194. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], length);
  195. break;
  196. default:
  197. return ERROR_COMMAND_SYNTAX_ERROR;
  198. }
  199. if (length <= 0)
  200. {
  201. command_print(CMD_CTX, "Length must be >0");
  202. return ERROR_COMMAND_SYNTAX_ERROR;
  203. }
  204. p = get_flash_bank_by_addr(target, address);
  205. if (p == NULL)
  206. {
  207. return ERROR_FAIL;
  208. }
  209. /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
  210. flash_set_dirty();
  211. struct duration bench;
  212. duration_start(&bench);
  213. retval = flash_erase_address_range(target, do_pad, address, length);
  214. if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
  215. {
  216. command_print(CMD_CTX, "erased address 0x%8.8x (length %i)"
  217. " in %fs (%0.3f kb/s)", address, length,
  218. duration_elapsed(&bench), duration_kbps(&bench, length));
  219. }
  220. return retval;
  221. }
  222. COMMAND_HANDLER(handle_flash_protect_check_command)
  223. {
  224. if (CMD_ARGC != 1)
  225. return ERROR_COMMAND_SYNTAX_ERROR;
  226. struct flash_bank *p;
  227. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
  228. if (ERROR_OK != retval)
  229. return retval;
  230. if ((retval = p->driver->protect_check(p)) == ERROR_OK)
  231. {
  232. command_print(CMD_CTX, "successfully checked protect state");
  233. }
  234. else if (retval == ERROR_FLASH_OPERATION_FAILED)
  235. {
  236. command_print(CMD_CTX, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32, CMD_ARGV[0], p->base);
  237. }
  238. else
  239. {
  240. command_print(CMD_CTX, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32, CMD_ARGV[0], p->base);
  241. }
  242. return ERROR_OK;
  243. }
  244. static int flash_check_sector_parameters(struct command_context *cmd_ctx,
  245. uint32_t first, uint32_t last, uint32_t num_sectors)
  246. {
  247. if (!(first <= last)) {
  248. command_print(cmd_ctx, "ERROR: "
  249. "first sector must be <= last sector");
  250. return ERROR_FAIL;
  251. }
  252. if (!(last <= (num_sectors - 1))) {
  253. command_print(cmd_ctx, "ERROR: last sector must be <= %d",
  254. (int) num_sectors - 1);
  255. return ERROR_FAIL;
  256. }
  257. return ERROR_OK;
  258. }
  259. COMMAND_HANDLER(handle_flash_erase_command)
  260. {
  261. if (CMD_ARGC != 3)
  262. return ERROR_COMMAND_SYNTAX_ERROR;
  263. uint32_t bank_nr;
  264. uint32_t first;
  265. uint32_t last;
  266. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bank_nr);
  267. struct flash_bank *p = get_flash_bank_by_num(bank_nr);
  268. if (!p)
  269. return ERROR_OK;
  270. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
  271. if (strcmp(CMD_ARGV[2], "last") == 0)
  272. last = p->num_sectors - 1;
  273. else
  274. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
  275. int retval;
  276. if ((retval = flash_check_sector_parameters(CMD_CTX,
  277. first, last, p->num_sectors)) != ERROR_OK)
  278. return retval;
  279. struct duration bench;
  280. duration_start(&bench);
  281. retval = flash_driver_erase(p, first, last);
  282. if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
  283. {
  284. command_print(CMD_CTX, "erased sectors %" PRIu32 " "
  285. "through %" PRIu32" on flash bank %" PRIu32 " "
  286. "in %fs", first, last, bank_nr, duration_elapsed(&bench));
  287. }
  288. return ERROR_OK;
  289. }
  290. COMMAND_HANDLER(handle_flash_protect_command)
  291. {
  292. if (CMD_ARGC != 4)
  293. return ERROR_COMMAND_SYNTAX_ERROR;
  294. uint32_t bank_nr;
  295. uint32_t first;
  296. uint32_t last;
  297. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bank_nr);
  298. struct flash_bank *p = get_flash_bank_by_num(bank_nr);
  299. if (!p)
  300. return ERROR_OK;
  301. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
  302. if (strcmp(CMD_ARGV[2], "last") == 0)
  303. last = p->num_sectors - 1;
  304. else
  305. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
  306. bool set;
  307. COMMAND_PARSE_ON_OFF(CMD_ARGV[3], set);
  308. int retval;
  309. if ((retval = flash_check_sector_parameters(CMD_CTX,
  310. first, last, p->num_sectors)) != ERROR_OK)
  311. return retval;
  312. retval = flash_driver_protect(p, set, first, last);
  313. if (retval == ERROR_OK) {
  314. command_print(CMD_CTX, "%s protection for sectors %i "
  315. "through %i on flash bank %i",
  316. (set) ? "set" : "cleared", (int) first,
  317. (int) last, (int) bank_nr);
  318. }
  319. return ERROR_OK;
  320. }
  321. COMMAND_HANDLER(handle_flash_write_image_command)
  322. {
  323. struct target *target = get_current_target(CMD_CTX);
  324. struct image image;
  325. uint32_t written;
  326. int retval;
  327. if (CMD_ARGC < 1)
  328. {
  329. return ERROR_COMMAND_SYNTAX_ERROR;
  330. }
  331. /* flash auto-erase is disabled by default*/
  332. int auto_erase = 0;
  333. bool auto_unlock = false;
  334. for (;;)
  335. {
  336. if (strcmp(CMD_ARGV[0], "erase") == 0)
  337. {
  338. auto_erase = 1;
  339. CMD_ARGV++;
  340. CMD_ARGC--;
  341. command_print(CMD_CTX, "auto erase enabled");
  342. } else if (strcmp(CMD_ARGV[0], "unlock") == 0)
  343. {
  344. auto_unlock = true;
  345. CMD_ARGV++;
  346. CMD_ARGC--;
  347. command_print(CMD_CTX, "auto unlock enabled");
  348. } else
  349. {
  350. break;
  351. }
  352. }
  353. if (CMD_ARGC < 1)
  354. {
  355. return ERROR_COMMAND_SYNTAX_ERROR;
  356. }
  357. if (!target)
  358. {
  359. LOG_ERROR("no target selected");
  360. return ERROR_FAIL;
  361. }
  362. struct duration bench;
  363. duration_start(&bench);
  364. if (CMD_ARGC >= 2)
  365. {
  366. image.base_address_set = 1;
  367. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], image.base_address);
  368. }
  369. else
  370. {
  371. image.base_address_set = 0;
  372. image.base_address = 0x0;
  373. }
  374. image.start_address_set = 0;
  375. retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
  376. if (retval != ERROR_OK)
  377. {
  378. return retval;
  379. }
  380. retval = flash_write_unlock(target, &image, &written, auto_erase, auto_unlock);
  381. if (retval != ERROR_OK)
  382. {
  383. image_close(&image);
  384. return retval;
  385. }
  386. if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
  387. {
  388. command_print(CMD_CTX, "wrote %" PRIu32 " bytes from file %s "
  389. "in %fs (%0.3f kb/s)", written, CMD_ARGV[0],
  390. duration_elapsed(&bench), duration_kbps(&bench, written));
  391. }
  392. image_close(&image);
  393. return retval;
  394. }
  395. COMMAND_HANDLER(handle_flash_fill_command)
  396. {
  397. int err = ERROR_OK;
  398. uint32_t address;
  399. uint32_t pattern;
  400. uint32_t count;
  401. uint32_t wrote = 0;
  402. uint32_t cur_size = 0;
  403. uint32_t chunk_count;
  404. struct target *target = get_current_target(CMD_CTX);
  405. uint32_t i;
  406. uint32_t wordsize;
  407. int retval = ERROR_OK;
  408. static size_t const chunksize = 1024;
  409. uint8_t *chunk = malloc(chunksize);
  410. if (chunk == NULL)
  411. return ERROR_FAIL;
  412. uint8_t *readback = malloc(chunksize);
  413. if (readback == NULL)
  414. {
  415. free(chunk);
  416. return ERROR_FAIL;
  417. }
  418. if (CMD_ARGC != 3)
  419. {
  420. retval = ERROR_COMMAND_SYNTAX_ERROR;
  421. goto done;
  422. }
  423. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
  424. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern);
  425. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
  426. if (count == 0)
  427. goto done;
  428. switch (CMD_NAME[4])
  429. {
  430. case 'w':
  431. wordsize = 4;
  432. break;
  433. case 'h':
  434. wordsize = 2;
  435. break;
  436. case 'b':
  437. wordsize = 1;
  438. break;
  439. default:
  440. retval = ERROR_COMMAND_SYNTAX_ERROR;
  441. goto done;
  442. }
  443. chunk_count = MIN(count, (chunksize / wordsize));
  444. switch (wordsize)
  445. {
  446. case 4:
  447. for (i = 0; i < chunk_count; i++)
  448. {
  449. target_buffer_set_u32(target, chunk + i * wordsize, pattern);
  450. }
  451. break;
  452. case 2:
  453. for (i = 0; i < chunk_count; i++)
  454. {
  455. target_buffer_set_u16(target, chunk + i * wordsize, pattern);
  456. }
  457. break;
  458. case 1:
  459. memset(chunk, pattern, chunk_count);
  460. break;
  461. default:
  462. LOG_ERROR("BUG: can't happen");
  463. exit(-1);
  464. }
  465. struct duration bench;
  466. duration_start(&bench);
  467. for (wrote = 0; wrote < (count*wordsize); wrote += cur_size)
  468. {
  469. struct flash_bank *bank;
  470. bank = get_flash_bank_by_addr(target, address);
  471. if (bank == NULL)
  472. {
  473. retval = ERROR_FAIL;
  474. goto done;
  475. }
  476. cur_size = MIN((count * wordsize - wrote), chunksize);
  477. err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
  478. if (err != ERROR_OK)
  479. {
  480. retval = err;
  481. goto done;
  482. }
  483. err = target_read_buffer(target, address + wrote, cur_size, readback);
  484. if (err != ERROR_OK)
  485. {
  486. retval = err;
  487. goto done;
  488. }
  489. unsigned i;
  490. for (i = 0; i < cur_size; i++)
  491. {
  492. if (readback[i]!=chunk[i])
  493. {
  494. LOG_ERROR("Verfication error address 0x%08" PRIx32 ", read back 0x%02x, expected 0x%02x",
  495. address + wrote + i, readback[i], chunk[i]);
  496. retval = ERROR_FAIL;
  497. goto done;
  498. }
  499. }
  500. }
  501. if (duration_measure(&bench) == ERROR_OK)
  502. {
  503. command_print(CMD_CTX, "wrote %" PRIu32 " bytes to 0x%8.8" PRIx32
  504. " in %fs (%0.3f kb/s)", wrote, address,
  505. duration_elapsed(&bench), duration_kbps(&bench, wrote));
  506. }
  507. done:
  508. free(readback);
  509. free(chunk);
  510. return retval;
  511. }
  512. COMMAND_HANDLER(handle_flash_write_bank_command)
  513. {
  514. uint32_t offset;
  515. uint8_t *buffer;
  516. struct fileio fileio;
  517. if (CMD_ARGC != 3)
  518. return ERROR_COMMAND_SYNTAX_ERROR;
  519. struct duration bench;
  520. duration_start(&bench);
  521. struct flash_bank *p;
  522. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
  523. if (ERROR_OK != retval)
  524. return retval;
  525. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], offset);
  526. if (fileio_open(&fileio, CMD_ARGV[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
  527. {
  528. return ERROR_OK;
  529. }
  530. buffer = malloc(fileio.size);
  531. size_t buf_cnt;
  532. if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
  533. {
  534. free(buffer);
  535. fileio_close(&fileio);
  536. return ERROR_OK;
  537. }
  538. retval = flash_driver_write(p, buffer, offset, buf_cnt);
  539. free(buffer);
  540. buffer = NULL;
  541. if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
  542. {
  543. command_print(CMD_CTX, "wrote %ld bytes from file %s to flash bank %u"
  544. " at offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)",
  545. (long)fileio.size, CMD_ARGV[1], p->bank_number, offset,
  546. duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
  547. }
  548. fileio_close(&fileio);
  549. return retval;
  550. }
  551. void flash_set_dirty(void)
  552. {
  553. struct flash_bank *c;
  554. int i;
  555. /* set all flash to require erasing */
  556. for (c = flash_bank_list(); c; c = c->next)
  557. {
  558. for (i = 0; i < c->num_sectors; i++)
  559. {
  560. c->sectors[i].is_erased = 0;
  561. }
  562. }
  563. }
  564. static const struct command_registration flash_exec_command_handlers[] = {
  565. {
  566. .name = "probe",
  567. .handler = handle_flash_probe_command,
  568. .mode = COMMAND_EXEC,
  569. .usage = "bank_id",
  570. .help = "Identify a flash bank.",
  571. },
  572. {
  573. .name = "info",
  574. .handler = handle_flash_info_command,
  575. .mode = COMMAND_EXEC,
  576. .usage = "bank_id",
  577. .help = "Print information about a flash bank.",
  578. },
  579. {
  580. .name = "erase_check",
  581. .handler = handle_flash_erase_check_command,
  582. .mode = COMMAND_EXEC,
  583. .usage = "bank_id",
  584. .help = "Check erase state of all blocks in a "
  585. "flash bank.",
  586. },
  587. {
  588. .name = "protect_check",
  589. .handler = handle_flash_protect_check_command,
  590. .mode = COMMAND_EXEC,
  591. .usage = "bank_id",
  592. .help = "Check protection state of all blocks in a "
  593. "flash bank.",
  594. },
  595. {
  596. .name = "erase_sector",
  597. .handler = handle_flash_erase_command,
  598. .mode = COMMAND_EXEC,
  599. .usage = "bank_id first_sector_num last_sector_num",
  600. .help = "Erase a range of sectors in a flash bank.",
  601. },
  602. {
  603. .name = "erase_address",
  604. .handler = handle_flash_erase_address_command,
  605. .mode = COMMAND_EXEC,
  606. .usage = "['pad'] address length",
  607. .help = "Erase flash sectors starting at address and "
  608. "continuing for length bytes. If 'pad' is specified, "
  609. "data outside that range may also be erased: the start "
  610. "address may be decreased, and length increased, so "
  611. "that all of the first and last sectors are erased.",
  612. },
  613. {
  614. .name = "fillw",
  615. .handler = handle_flash_fill_command,
  616. .mode = COMMAND_EXEC,
  617. .usage = "address value n",
  618. .help = "Fill n words with 32-bit value, starting at "
  619. "word address. (No autoerase.)",
  620. },
  621. {
  622. .name = "fillh",
  623. .handler = handle_flash_fill_command,
  624. .mode = COMMAND_EXEC,
  625. .usage = "address value n",
  626. .help = "Fill n halfwords with 16-bit value, starting at "
  627. "word address. (No autoerase.)",
  628. },
  629. {
  630. .name = "fillb",
  631. .handler = handle_flash_fill_command,
  632. .mode = COMMAND_EXEC,
  633. .usage = "address value n",
  634. .help = "Fill n bytes with 8-bit value, starting at "
  635. "word address. (No autoerase.)",
  636. },
  637. {
  638. .name = "write_bank",
  639. .handler = handle_flash_write_bank_command,
  640. .mode = COMMAND_EXEC,
  641. .usage = "bank_id filename offset",
  642. .help = "Write binary data from file to flash bank, "
  643. "starting at specified byte offset from the "
  644. "beginning of the bank.",
  645. },
  646. {
  647. .name = "write_image",
  648. .handler = handle_flash_write_image_command,
  649. .mode = COMMAND_EXEC,
  650. .usage = "[erase] [unlock] filename [offset [file_type]]",
  651. .help = "Write an image to flash. Optionally first unprotect "
  652. "and/or erase the region to be used. Allow optional "
  653. "offset from beginning of bank (defaults to zero)",
  654. },
  655. {
  656. .name = "protect",
  657. .handler = handle_flash_protect_command,
  658. .mode = COMMAND_EXEC,
  659. .usage = "bank_id first_sector [last_sector|'last'] "
  660. "('on'|'off')",
  661. .help = "Turn protection on or off for a range of sectors "
  662. "in a given flash bank.",
  663. },
  664. COMMAND_REGISTRATION_DONE
  665. };
  666. int flash_init_drivers(struct command_context *cmd_ctx)
  667. {
  668. if (!flash_bank_list())
  669. return ERROR_OK;
  670. struct command *parent = command_find_in_context(cmd_ctx, "flash");
  671. return register_commands(cmd_ctx, parent, flash_exec_command_handlers);
  672. }
  673. COMMAND_HANDLER(handle_flash_bank_command)
  674. {
  675. if (CMD_ARGC < 7)
  676. {
  677. LOG_ERROR("usage: flash bank <name> <driver> "
  678. "<base> <size> <chip_width> <bus_width>");
  679. return ERROR_COMMAND_SYNTAX_ERROR;
  680. }
  681. // save bank name and advance arguments for compatibility
  682. const char *bank_name = *CMD_ARGV++;
  683. CMD_ARGC--;
  684. struct target *target;
  685. if ((target = get_target(CMD_ARGV[5])) == NULL)
  686. {
  687. LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
  688. return ERROR_FAIL;
  689. }
  690. const char *driver_name = CMD_ARGV[0];
  691. struct flash_driver *driver = flash_driver_find_by_name(driver_name);
  692. if (NULL == driver)
  693. {
  694. /* no matching flash driver found */
  695. LOG_ERROR("flash driver '%s' not found", driver_name);
  696. return ERROR_FAIL;
  697. }
  698. /* register flash specific commands */
  699. if (NULL != driver->commands)
  700. {
  701. int retval = register_commands(CMD_CTX, NULL,
  702. driver->commands);
  703. if (ERROR_OK != retval)
  704. {
  705. LOG_ERROR("couldn't register '%s' commands",
  706. driver_name);
  707. return ERROR_FAIL;
  708. }
  709. }
  710. struct flash_bank *c = malloc(sizeof(*c));
  711. c->name = strdup(bank_name);
  712. c->target = target;
  713. c->driver = driver;
  714. c->driver_priv = NULL;
  715. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], c->base);
  716. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], c->size);
  717. COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], c->chip_width);
  718. COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], c->bus_width);
  719. c->num_sectors = 0;
  720. c->sectors = NULL;
  721. c->next = NULL;
  722. int retval;
  723. retval = CALL_COMMAND_HANDLER(driver->flash_bank_command, c);
  724. if (ERROR_OK != retval)
  725. {
  726. LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32,
  727. driver_name, c->base);
  728. free(c);
  729. return retval;
  730. }
  731. flash_bank_add(c);
  732. return ERROR_OK;
  733. }
  734. COMMAND_HANDLER(handle_flash_banks_command)
  735. {
  736. if (CMD_ARGC != 0)
  737. return ERROR_INVALID_ARGUMENTS;
  738. unsigned n = 0;
  739. for (struct flash_bank *p = flash_bank_list(); p; p = p->next, n++)
  740. {
  741. LOG_USER("#%u: %s at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32 ", "
  742. "buswidth %u, chipwidth %u", n,
  743. p->driver->name, p->base, p->size,
  744. p->bus_width, p->chip_width);
  745. }
  746. return ERROR_OK;
  747. }
  748. static int jim_flash_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  749. {
  750. if (argc != 1)
  751. {
  752. Jim_WrongNumArgs(interp, 1, argv,
  753. "no arguments to 'flash list' command");
  754. return JIM_ERR;
  755. }
  756. Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
  757. for (struct flash_bank *p = flash_bank_list(); p; p = p->next)
  758. {
  759. Jim_Obj *elem = Jim_NewListObj(interp, NULL, 0);
  760. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "name", -1));
  761. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, p->driver->name, -1));
  762. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "base", -1));
  763. Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->base));
  764. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "size", -1));
  765. Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->size));
  766. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "bus_width", -1));
  767. Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->bus_width));
  768. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "chip_width", -1));
  769. Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->chip_width));
  770. Jim_ListAppendElement(interp, list, elem);
  771. }
  772. Jim_SetResult(interp, list);
  773. return JIM_OK;
  774. }
  775. COMMAND_HANDLER(handle_flash_init_command)
  776. {
  777. if (CMD_ARGC != 0)
  778. return ERROR_COMMAND_SYNTAX_ERROR;
  779. static bool flash_initialized = false;
  780. if (flash_initialized)
  781. {
  782. LOG_INFO("'flash init' has already been called");
  783. return ERROR_OK;
  784. }
  785. flash_initialized = true;
  786. LOG_DEBUG("Initializing flash devices...");
  787. return flash_init_drivers(CMD_CTX);
  788. }
  789. static const struct command_registration flash_config_command_handlers[] = {
  790. {
  791. .name = "bank",
  792. .handler = &handle_flash_bank_command,
  793. .mode = COMMAND_CONFIG,
  794. .usage = "bank_id driver_name base_address size_bytes "
  795. "chip_width_bytes bus_width_bytes target "
  796. "[driver_options ...]",
  797. .help = "Define a new bank with the given name, "
  798. "using the specified NOR flash driver.",
  799. },
  800. {
  801. .name = "init",
  802. .mode = COMMAND_CONFIG,
  803. .handler = &handle_flash_init_command,
  804. .help = "Initialize flash devices.",
  805. },
  806. {
  807. .name = "banks",
  808. .mode = COMMAND_ANY,
  809. .handler = &handle_flash_banks_command,
  810. .help = "Display table with information about flash banks.",
  811. },
  812. {
  813. .name = "list",
  814. .mode = COMMAND_ANY,
  815. .jim_handler = &jim_flash_list,
  816. .help = "Returns a list of details about the flash banks.",
  817. },
  818. COMMAND_REGISTRATION_DONE
  819. };
  820. static const struct command_registration flash_command_handlers[] = {
  821. {
  822. .name = "flash",
  823. .mode = COMMAND_ANY,
  824. .help = "NOR flash command group",
  825. .chain = flash_config_command_handlers,
  826. },
  827. COMMAND_REGISTRATION_DONE
  828. };
  829. int flash_register_commands(struct command_context *cmd_ctx)
  830. {
  831. return register_commands(cmd_ctx, NULL, flash_command_handlers);
  832. }