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.
 
 
 
 
 
 

1434 lines
34 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2008 by Spencer Oliver *
  9. * spen@spen-soft.co.uk *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "flash.h"
  30. #include "common.h"
  31. #include "image.h"
  32. #include "time_support.h"
  33. static int flash_write_unlock(struct target *target, struct image *image, uint32_t *written, int erase, bool unlock);
  34. /* flash drivers
  35. */
  36. extern struct flash_driver lpc2000_flash;
  37. extern struct flash_driver lpc288x_flash;
  38. extern struct flash_driver lpc2900_flash;
  39. extern struct flash_driver cfi_flash;
  40. extern struct flash_driver at91sam3_flash;
  41. extern struct flash_driver at91sam7_flash;
  42. extern struct flash_driver str7x_flash;
  43. extern struct flash_driver str9x_flash;
  44. extern struct flash_driver aduc702x_flash;
  45. extern struct flash_driver stellaris_flash;
  46. extern struct flash_driver str9xpec_flash;
  47. extern struct flash_driver stm32x_flash;
  48. extern struct flash_driver tms470_flash;
  49. extern struct flash_driver ecosflash_flash;
  50. extern struct flash_driver ocl_flash;
  51. extern struct flash_driver pic32mx_flash;
  52. extern struct flash_driver avr_flash;
  53. extern struct flash_driver faux_flash;
  54. struct flash_driver *flash_drivers[] = {
  55. &lpc2000_flash,
  56. &lpc288x_flash,
  57. &lpc2900_flash,
  58. &cfi_flash,
  59. &at91sam7_flash,
  60. &at91sam3_flash,
  61. &str7x_flash,
  62. &str9x_flash,
  63. &aduc702x_flash,
  64. &stellaris_flash,
  65. &str9xpec_flash,
  66. &stm32x_flash,
  67. &tms470_flash,
  68. &ecosflash_flash,
  69. &ocl_flash,
  70. &pic32mx_flash,
  71. &avr_flash,
  72. &faux_flash,
  73. NULL,
  74. };
  75. struct flash_bank *flash_banks;
  76. /* wafer thin wrapper for invoking the flash driver */
  77. static int flash_driver_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
  78. {
  79. int retval;
  80. retval = bank->driver->write(bank, buffer, offset, count);
  81. if (retval != ERROR_OK)
  82. {
  83. LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
  84. bank->base, offset, retval);
  85. }
  86. return retval;
  87. }
  88. static int flash_driver_erase(struct flash_bank *bank, int first, int last)
  89. {
  90. int retval;
  91. retval = bank->driver->erase(bank, first, last);
  92. if (retval != ERROR_OK)
  93. {
  94. LOG_ERROR("failed erasing sectors %d to %d (%d)", first, last, retval);
  95. }
  96. return retval;
  97. }
  98. int flash_driver_protect(struct flash_bank *bank, int set, int first, int last)
  99. {
  100. int retval;
  101. retval = bank->driver->protect(bank, set, first, last);
  102. if (retval != ERROR_OK)
  103. {
  104. LOG_ERROR("failed setting protection for areas %d to %d (%d)", first, last, retval);
  105. }
  106. return retval;
  107. }
  108. static int jim_flash_banks(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  109. {
  110. struct flash_bank *p;
  111. if (argc != 1) {
  112. Jim_WrongNumArgs(interp, 1, argv, "no arguments to flash_banks command");
  113. return JIM_ERR;
  114. }
  115. Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
  116. for (p = flash_banks; p; p = p->next)
  117. {
  118. Jim_Obj *elem = Jim_NewListObj(interp, NULL, 0);
  119. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "name", -1));
  120. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, p->driver->name, -1));
  121. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "base", -1));
  122. Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->base));
  123. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "size", -1));
  124. Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->size));
  125. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "bus_width", -1));
  126. Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->bus_width));
  127. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "chip_width", -1));
  128. Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->chip_width));
  129. Jim_ListAppendElement(interp, list, elem);
  130. }
  131. Jim_SetResult(interp, list);
  132. return JIM_OK;
  133. }
  134. struct flash_bank *get_flash_bank_by_num_noprobe(int num)
  135. {
  136. struct flash_bank *p;
  137. int i = 0;
  138. for (p = flash_banks; p; p = p->next)
  139. {
  140. if (i++ == num)
  141. {
  142. return p;
  143. }
  144. }
  145. LOG_ERROR("flash bank %d does not exist", num);
  146. return NULL;
  147. }
  148. int flash_get_bank_count(void)
  149. {
  150. struct flash_bank *p;
  151. int i = 0;
  152. for (p = flash_banks; p; p = p->next)
  153. {
  154. i++;
  155. }
  156. return i;
  157. }
  158. struct flash_bank *get_flash_bank_by_name(const char *name)
  159. {
  160. unsigned requested = get_flash_name_index(name);
  161. unsigned found = 0;
  162. struct flash_bank *bank;
  163. for (bank = flash_banks; NULL != bank; bank = bank->next)
  164. {
  165. if (strcmp(bank->name, name) == 0)
  166. return bank;
  167. if (!flash_driver_name_matches(bank->driver->name, name))
  168. continue;
  169. if (++found < requested)
  170. continue;
  171. return bank;
  172. }
  173. return NULL;
  174. }
  175. struct flash_bank *get_flash_bank_by_num(int num)
  176. {
  177. struct flash_bank *p = get_flash_bank_by_num_noprobe(num);
  178. int retval;
  179. if (p == NULL)
  180. return NULL;
  181. retval = p->driver->auto_probe(p);
  182. if (retval != ERROR_OK)
  183. {
  184. LOG_ERROR("auto_probe failed %d\n", retval);
  185. return NULL;
  186. }
  187. return p;
  188. }
  189. COMMAND_HELPER(flash_command_get_bank, unsigned name_index,
  190. struct flash_bank **bank)
  191. {
  192. const char *name = CMD_ARGV[name_index];
  193. *bank = get_flash_bank_by_name(name);
  194. if (*bank)
  195. return ERROR_OK;
  196. unsigned bank_num;
  197. COMMAND_PARSE_NUMBER(uint, name, bank_num);
  198. *bank = get_flash_bank_by_num(bank_num);
  199. if (!*bank)
  200. {
  201. command_print(CMD_CTX, "flash bank '%s' not found", name);
  202. return ERROR_INVALID_ARGUMENTS;
  203. }
  204. return ERROR_OK;
  205. }
  206. COMMAND_HANDLER(handle_flash_bank_command)
  207. {
  208. if (CMD_ARGC < 7)
  209. {
  210. LOG_ERROR("usage: flash bank <name> <driver> "
  211. "<base> <size> <chip_width> <bus_width>");
  212. return ERROR_COMMAND_SYNTAX_ERROR;
  213. }
  214. // save bank name and advance arguments for compatibility
  215. const char *bank_name = *CMD_ARGV++;
  216. CMD_ARGC--;
  217. struct target *target;
  218. if ((target = get_target(CMD_ARGV[5])) == NULL)
  219. {
  220. LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
  221. return ERROR_FAIL;
  222. }
  223. const char *driver_name = CMD_ARGV[0];
  224. for (unsigned i = 0; flash_drivers[i]; i++)
  225. {
  226. if (strcmp(driver_name, flash_drivers[i]->name) != 0)
  227. continue;
  228. /* register flash specific commands */
  229. if (NULL != flash_drivers[i]->commands)
  230. {
  231. int retval = register_commands(CMD_CTX, NULL,
  232. flash_drivers[i]->commands);
  233. if (ERROR_OK != retval)
  234. {
  235. LOG_ERROR("couldn't register '%s' commands",
  236. driver_name);
  237. return ERROR_FAIL;
  238. }
  239. }
  240. struct flash_bank *p, *c;
  241. c = malloc(sizeof(struct flash_bank));
  242. c->name = strdup(bank_name);
  243. c->target = target;
  244. c->driver = flash_drivers[i];
  245. c->driver_priv = NULL;
  246. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], c->base);
  247. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], c->size);
  248. COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], c->chip_width);
  249. COMMAND_PARSE_NUMBER(int, CMD_ARGV[4], c->bus_width);
  250. c->num_sectors = 0;
  251. c->sectors = NULL;
  252. c->next = NULL;
  253. int retval;
  254. retval = CALL_COMMAND_HANDLER(flash_drivers[i]->flash_bank_command, c);
  255. if (ERROR_OK != retval)
  256. {
  257. LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32,
  258. driver_name, c->base);
  259. free(c);
  260. return retval;
  261. }
  262. /* put flash bank in linked list */
  263. if (flash_banks)
  264. {
  265. int bank_num = 0;
  266. /* find last flash bank */
  267. for (p = flash_banks; p && p->next; p = p->next) bank_num++;
  268. if (p)
  269. p->next = c;
  270. c->bank_number = bank_num + 1;
  271. }
  272. else
  273. {
  274. flash_banks = c;
  275. c->bank_number = 0;
  276. }
  277. return ERROR_OK;
  278. }
  279. /* no matching flash driver found */
  280. LOG_ERROR("flash driver '%s' not found", driver_name);
  281. return ERROR_FAIL;
  282. }
  283. COMMAND_HANDLER(handle_flash_info_command)
  284. {
  285. struct flash_bank *p;
  286. uint32_t i = 0;
  287. int j = 0;
  288. int retval;
  289. if (CMD_ARGC != 1)
  290. return ERROR_COMMAND_SYNTAX_ERROR;
  291. unsigned bank_nr;
  292. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], bank_nr);
  293. for (p = flash_banks; p; p = p->next, i++)
  294. {
  295. if (i != bank_nr)
  296. continue;
  297. char buf[1024];
  298. /* attempt auto probe */
  299. if ((retval = p->driver->auto_probe(p)) != ERROR_OK)
  300. return retval;
  301. command_print(CMD_CTX,
  302. "#%" PRIi32 " : %s at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32 ", buswidth %i, chipwidth %i",
  303. i,
  304. p->driver->name,
  305. p->base,
  306. p->size,
  307. p->bus_width,
  308. p->chip_width);
  309. for (j = 0; j < p->num_sectors; j++)
  310. {
  311. char *protect_state;
  312. if (p->sectors[j].is_protected == 0)
  313. protect_state = "not protected";
  314. else if (p->sectors[j].is_protected == 1)
  315. protect_state = "protected";
  316. else
  317. protect_state = "protection state unknown";
  318. command_print(CMD_CTX,
  319. "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
  320. j,
  321. p->sectors[j].offset,
  322. p->sectors[j].size,
  323. p->sectors[j].size >> 10,
  324. protect_state);
  325. }
  326. *buf = '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
  327. retval = p->driver->info(p, buf, sizeof(buf));
  328. command_print(CMD_CTX, "%s", buf);
  329. if (retval != ERROR_OK)
  330. LOG_ERROR("error retrieving flash info (%d)", retval);
  331. }
  332. return ERROR_OK;
  333. }
  334. COMMAND_HANDLER(handle_flash_probe_command)
  335. {
  336. int retval;
  337. if (CMD_ARGC != 1)
  338. {
  339. return ERROR_COMMAND_SYNTAX_ERROR;
  340. }
  341. unsigned bank_nr;
  342. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], bank_nr);
  343. struct flash_bank *p = get_flash_bank_by_num_noprobe(bank_nr);
  344. if (p)
  345. {
  346. if ((retval = p->driver->probe(p)) == ERROR_OK)
  347. {
  348. command_print(CMD_CTX, "flash '%s' found at 0x%8.8" PRIx32, p->driver->name, p->base);
  349. }
  350. else if (retval == ERROR_FLASH_BANK_INVALID)
  351. {
  352. command_print(CMD_CTX, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32,
  353. CMD_ARGV[0], p->base);
  354. }
  355. else
  356. {
  357. command_print(CMD_CTX, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32,
  358. CMD_ARGV[0], p->base);
  359. }
  360. }
  361. else
  362. {
  363. command_print(CMD_CTX, "flash bank '#%s' is out of bounds", CMD_ARGV[0]);
  364. }
  365. return ERROR_OK;
  366. }
  367. COMMAND_HANDLER(handle_flash_erase_check_command)
  368. {
  369. if (CMD_ARGC != 1)
  370. {
  371. return ERROR_COMMAND_SYNTAX_ERROR;
  372. }
  373. struct flash_bank *p;
  374. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
  375. if (ERROR_OK != retval)
  376. return retval;
  377. int j;
  378. if ((retval = p->driver->erase_check(p)) == ERROR_OK)
  379. {
  380. command_print(CMD_CTX, "successfully checked erase state");
  381. }
  382. else
  383. {
  384. command_print(CMD_CTX, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32,
  385. CMD_ARGV[0], p->base);
  386. }
  387. for (j = 0; j < p->num_sectors; j++)
  388. {
  389. char *erase_state;
  390. if (p->sectors[j].is_erased == 0)
  391. erase_state = "not erased";
  392. else if (p->sectors[j].is_erased == 1)
  393. erase_state = "erased";
  394. else
  395. erase_state = "erase state unknown";
  396. command_print(CMD_CTX,
  397. "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
  398. j,
  399. p->sectors[j].offset,
  400. p->sectors[j].size,
  401. p->sectors[j].size >> 10,
  402. erase_state);
  403. }
  404. return ERROR_OK;
  405. }
  406. COMMAND_HANDLER(handle_flash_erase_address_command)
  407. {
  408. struct flash_bank *p;
  409. int retval;
  410. int address;
  411. int length;
  412. struct target *target = get_current_target(CMD_CTX);
  413. if (CMD_ARGC != 2)
  414. return ERROR_COMMAND_SYNTAX_ERROR;
  415. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], address);
  416. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], length);
  417. if (length <= 0)
  418. {
  419. command_print(CMD_CTX, "Length must be >0");
  420. return ERROR_COMMAND_SYNTAX_ERROR;
  421. }
  422. p = get_flash_bank_by_addr(target, address);
  423. if (p == NULL)
  424. {
  425. return ERROR_FAIL;
  426. }
  427. /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
  428. flash_set_dirty();
  429. struct duration bench;
  430. duration_start(&bench);
  431. retval = flash_erase_address_range(target, address, length);
  432. if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
  433. {
  434. command_print(CMD_CTX, "erased address 0x%8.8x (length %i)"
  435. " in %fs (%0.3f kb/s)", address, length,
  436. duration_elapsed(&bench), duration_kbps(&bench, length));
  437. }
  438. return retval;
  439. }
  440. COMMAND_HANDLER(handle_flash_protect_check_command)
  441. {
  442. if (CMD_ARGC != 1)
  443. return ERROR_COMMAND_SYNTAX_ERROR;
  444. struct flash_bank *p;
  445. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
  446. if (ERROR_OK != retval)
  447. return retval;
  448. if ((retval = p->driver->protect_check(p)) == ERROR_OK)
  449. {
  450. command_print(CMD_CTX, "successfully checked protect state");
  451. }
  452. else if (retval == ERROR_FLASH_OPERATION_FAILED)
  453. {
  454. command_print(CMD_CTX, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32, CMD_ARGV[0], p->base);
  455. }
  456. else
  457. {
  458. command_print(CMD_CTX, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32, CMD_ARGV[0], p->base);
  459. }
  460. return ERROR_OK;
  461. }
  462. static int flash_check_sector_parameters(struct command_context *cmd_ctx,
  463. uint32_t first, uint32_t last, uint32_t num_sectors)
  464. {
  465. if (!(first <= last)) {
  466. command_print(cmd_ctx, "ERROR: "
  467. "first sector must be <= last sector");
  468. return ERROR_FAIL;
  469. }
  470. if (!(last <= (num_sectors - 1))) {
  471. command_print(cmd_ctx, "ERROR: last sector must be <= %d",
  472. (int) num_sectors - 1);
  473. return ERROR_FAIL;
  474. }
  475. return ERROR_OK;
  476. }
  477. COMMAND_HANDLER(handle_flash_erase_command)
  478. {
  479. if (CMD_ARGC != 3)
  480. return ERROR_COMMAND_SYNTAX_ERROR;
  481. uint32_t bank_nr;
  482. uint32_t first;
  483. uint32_t last;
  484. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bank_nr);
  485. struct flash_bank *p = get_flash_bank_by_num(bank_nr);
  486. if (!p)
  487. return ERROR_OK;
  488. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
  489. if (strcmp(CMD_ARGV[2], "last") == 0)
  490. last = p->num_sectors - 1;
  491. else
  492. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
  493. int retval;
  494. if ((retval = flash_check_sector_parameters(CMD_CTX,
  495. first, last, p->num_sectors)) != ERROR_OK)
  496. return retval;
  497. struct duration bench;
  498. duration_start(&bench);
  499. retval = flash_driver_erase(p, first, last);
  500. if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
  501. {
  502. command_print(CMD_CTX, "erased sectors %" PRIu32 " "
  503. "through %" PRIu32" on flash bank %" PRIu32 " "
  504. "in %fs", first, last, bank_nr, duration_elapsed(&bench));
  505. }
  506. return ERROR_OK;
  507. }
  508. COMMAND_HANDLER(handle_flash_protect_command)
  509. {
  510. if (CMD_ARGC != 4)
  511. return ERROR_COMMAND_SYNTAX_ERROR;
  512. uint32_t bank_nr;
  513. uint32_t first;
  514. uint32_t last;
  515. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], bank_nr);
  516. struct flash_bank *p = get_flash_bank_by_num(bank_nr);
  517. if (!p)
  518. return ERROR_OK;
  519. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], first);
  520. if (strcmp(CMD_ARGV[2], "last") == 0)
  521. last = p->num_sectors - 1;
  522. else
  523. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], last);
  524. bool set;
  525. COMMAND_PARSE_ON_OFF(CMD_ARGV[3], set);
  526. int retval;
  527. if ((retval = flash_check_sector_parameters(CMD_CTX,
  528. first, last, p->num_sectors)) != ERROR_OK)
  529. return retval;
  530. retval = flash_driver_protect(p, set, first, last);
  531. if (retval == ERROR_OK) {
  532. command_print(CMD_CTX, "%s protection for sectors %i "
  533. "through %i on flash bank %i",
  534. (set) ? "set" : "cleared", (int) first,
  535. (int) last, (int) bank_nr);
  536. }
  537. return ERROR_OK;
  538. }
  539. COMMAND_HANDLER(handle_flash_write_image_command)
  540. {
  541. struct target *target = get_current_target(CMD_CTX);
  542. struct image image;
  543. uint32_t written;
  544. int retval;
  545. if (CMD_ARGC < 1)
  546. {
  547. return ERROR_COMMAND_SYNTAX_ERROR;
  548. }
  549. /* flash auto-erase is disabled by default*/
  550. int auto_erase = 0;
  551. bool auto_unlock = false;
  552. for (;;)
  553. {
  554. if (strcmp(CMD_ARGV[0], "erase") == 0)
  555. {
  556. auto_erase = 1;
  557. CMD_ARGV++;
  558. CMD_ARGC--;
  559. command_print(CMD_CTX, "auto erase enabled");
  560. } else if (strcmp(CMD_ARGV[0], "unlock") == 0)
  561. {
  562. auto_unlock = true;
  563. CMD_ARGV++;
  564. CMD_ARGC--;
  565. command_print(CMD_CTX, "auto unlock enabled");
  566. } else
  567. {
  568. break;
  569. }
  570. }
  571. if (CMD_ARGC < 1)
  572. {
  573. return ERROR_COMMAND_SYNTAX_ERROR;
  574. }
  575. if (!target)
  576. {
  577. LOG_ERROR("no target selected");
  578. return ERROR_FAIL;
  579. }
  580. struct duration bench;
  581. duration_start(&bench);
  582. if (CMD_ARGC >= 2)
  583. {
  584. image.base_address_set = 1;
  585. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], image.base_address);
  586. }
  587. else
  588. {
  589. image.base_address_set = 0;
  590. image.base_address = 0x0;
  591. }
  592. image.start_address_set = 0;
  593. retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
  594. if (retval != ERROR_OK)
  595. {
  596. return retval;
  597. }
  598. retval = flash_write_unlock(target, &image, &written, auto_erase, auto_unlock);
  599. if (retval != ERROR_OK)
  600. {
  601. image_close(&image);
  602. return retval;
  603. }
  604. if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
  605. {
  606. command_print(CMD_CTX, "wrote %" PRIu32 " byte from file %s "
  607. "in %fs (%0.3f kb/s)", written, CMD_ARGV[0],
  608. duration_elapsed(&bench), duration_kbps(&bench, written));
  609. }
  610. image_close(&image);
  611. return retval;
  612. }
  613. COMMAND_HANDLER(handle_flash_fill_command)
  614. {
  615. int err = ERROR_OK;
  616. uint32_t address;
  617. uint32_t pattern;
  618. uint32_t count;
  619. uint32_t wrote = 0;
  620. uint32_t cur_size = 0;
  621. uint32_t chunk_count;
  622. struct target *target = get_current_target(CMD_CTX);
  623. uint32_t i;
  624. uint32_t wordsize;
  625. int retval = ERROR_OK;
  626. static size_t const chunksize = 1024;
  627. uint8_t *chunk = malloc(chunksize);
  628. if (chunk == NULL)
  629. return ERROR_FAIL;
  630. uint8_t *readback = malloc(chunksize);
  631. if (readback == NULL)
  632. {
  633. free(chunk);
  634. return ERROR_FAIL;
  635. }
  636. if (CMD_ARGC != 3)
  637. {
  638. retval = ERROR_COMMAND_SYNTAX_ERROR;
  639. goto done;
  640. }
  641. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
  642. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern);
  643. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
  644. if (count == 0)
  645. goto done;
  646. switch (CMD_NAME[4])
  647. {
  648. case 'w':
  649. wordsize = 4;
  650. break;
  651. case 'h':
  652. wordsize = 2;
  653. break;
  654. case 'b':
  655. wordsize = 1;
  656. break;
  657. default:
  658. retval = ERROR_COMMAND_SYNTAX_ERROR;
  659. goto done;
  660. }
  661. chunk_count = MIN(count, (chunksize / wordsize));
  662. switch (wordsize)
  663. {
  664. case 4:
  665. for (i = 0; i < chunk_count; i++)
  666. {
  667. target_buffer_set_u32(target, chunk + i * wordsize, pattern);
  668. }
  669. break;
  670. case 2:
  671. for (i = 0; i < chunk_count; i++)
  672. {
  673. target_buffer_set_u16(target, chunk + i * wordsize, pattern);
  674. }
  675. break;
  676. case 1:
  677. memset(chunk, pattern, chunk_count);
  678. break;
  679. default:
  680. LOG_ERROR("BUG: can't happen");
  681. exit(-1);
  682. }
  683. struct duration bench;
  684. duration_start(&bench);
  685. for (wrote = 0; wrote < (count*wordsize); wrote += cur_size)
  686. {
  687. cur_size = MIN((count*wordsize - wrote), sizeof(chunk));
  688. struct flash_bank *bank;
  689. bank = get_flash_bank_by_addr(target, address);
  690. if (bank == NULL)
  691. {
  692. retval = ERROR_FAIL;
  693. goto done;
  694. }
  695. err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
  696. if (err != ERROR_OK)
  697. {
  698. retval = err;
  699. goto done;
  700. }
  701. err = target_read_buffer(target, address + wrote, cur_size, readback);
  702. if (err != ERROR_OK)
  703. {
  704. retval = err;
  705. goto done;
  706. }
  707. unsigned i;
  708. for (i = 0; i < cur_size; i++)
  709. {
  710. if (readback[i]!=chunk[i])
  711. {
  712. LOG_ERROR("Verfication error address 0x%08" PRIx32 ", read back 0x%02x, expected 0x%02x",
  713. address + wrote + i, readback[i], chunk[i]);
  714. retval = ERROR_FAIL;
  715. goto done;
  716. }
  717. }
  718. }
  719. if (duration_measure(&bench) == ERROR_OK)
  720. {
  721. command_print(CMD_CTX, "wrote %" PRIu32 " bytes to 0x%8.8" PRIx32
  722. " in %fs (%0.3f kb/s)", wrote, address,
  723. duration_elapsed(&bench), duration_kbps(&bench, wrote));
  724. }
  725. done:
  726. free(readback);
  727. free(chunk);
  728. return retval;
  729. }
  730. COMMAND_HANDLER(handle_flash_write_bank_command)
  731. {
  732. uint32_t offset;
  733. uint8_t *buffer;
  734. struct fileio fileio;
  735. if (CMD_ARGC != 3)
  736. return ERROR_COMMAND_SYNTAX_ERROR;
  737. struct duration bench;
  738. duration_start(&bench);
  739. struct flash_bank *p;
  740. int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &p);
  741. if (ERROR_OK != retval)
  742. return retval;
  743. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], offset);
  744. if (fileio_open(&fileio, CMD_ARGV[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
  745. {
  746. return ERROR_OK;
  747. }
  748. buffer = malloc(fileio.size);
  749. size_t buf_cnt;
  750. if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
  751. {
  752. free(buffer);
  753. fileio_close(&fileio);
  754. return ERROR_OK;
  755. }
  756. retval = flash_driver_write(p, buffer, offset, buf_cnt);
  757. free(buffer);
  758. buffer = NULL;
  759. if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
  760. {
  761. command_print(CMD_CTX, "wrote %zu byte from file %s to flash bank %u"
  762. " at offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)",
  763. fileio.size, CMD_ARGV[1], p->bank_number, offset,
  764. duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
  765. }
  766. fileio_close(&fileio);
  767. return retval;
  768. }
  769. void flash_set_dirty(void)
  770. {
  771. struct flash_bank *c;
  772. int i;
  773. /* set all flash to require erasing */
  774. for (c = flash_banks; c; c = c->next)
  775. {
  776. for (i = 0; i < c->num_sectors; i++)
  777. {
  778. c->sectors[i].is_erased = 0;
  779. }
  780. }
  781. }
  782. /* lookup flash bank by address */
  783. struct flash_bank *get_flash_bank_by_addr(struct target *target, uint32_t addr)
  784. {
  785. struct flash_bank *c;
  786. /* cycle through bank list */
  787. for (c = flash_banks; c; c = c->next)
  788. {
  789. int retval;
  790. retval = c->driver->auto_probe(c);
  791. if (retval != ERROR_OK)
  792. {
  793. LOG_ERROR("auto_probe failed %d\n", retval);
  794. return NULL;
  795. }
  796. /* check whether address belongs to this flash bank */
  797. if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
  798. return c;
  799. }
  800. LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
  801. return NULL;
  802. }
  803. /* erase given flash region, selects proper bank according to target and address */
  804. static int flash_iterate_address_range(struct target *target, uint32_t addr, uint32_t length,
  805. int (*callback)(struct flash_bank *bank, int first, int last))
  806. {
  807. struct flash_bank *c;
  808. int first = -1;
  809. int last = -1;
  810. int i;
  811. if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
  812. return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
  813. if (c->size == 0 || c->num_sectors == 0)
  814. {
  815. LOG_ERROR("Bank is invalid");
  816. return ERROR_FLASH_BANK_INVALID;
  817. }
  818. if (length == 0)
  819. {
  820. /* special case, erase whole bank when length is zero */
  821. if (addr != c->base)
  822. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  823. return callback(c, 0, c->num_sectors - 1);
  824. }
  825. /* check whether it fits */
  826. if (addr + length - 1 > c->base + c->size - 1)
  827. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  828. addr -= c->base;
  829. for (i = 0; i < c->num_sectors; i++)
  830. {
  831. /* check whether sector overlaps with the given range and is not yet erased */
  832. if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
  833. /* if first is not set yet then this is the first sector */
  834. if (first == -1)
  835. first = i;
  836. last = i; /* and it is the last one so far in any case */
  837. }
  838. }
  839. if (first == -1 || last == -1)
  840. return ERROR_OK;
  841. return callback(c, first, last);
  842. }
  843. int flash_erase_address_range(struct target *target, uint32_t addr, uint32_t length)
  844. {
  845. return flash_iterate_address_range(target, addr, length, &flash_driver_erase);
  846. }
  847. static int flash_driver_unprotect(struct flash_bank *bank, int first, int last)
  848. {
  849. return flash_driver_protect(bank, 0, first, last);
  850. }
  851. static int flash_unlock_address_range(struct target *target, uint32_t addr, uint32_t length)
  852. {
  853. return flash_iterate_address_range(target, addr, length, &flash_driver_unprotect);
  854. }
  855. /* write (optional verify) an image to flash memory of the given target */
  856. static int flash_write_unlock(struct target *target, struct image *image, uint32_t *written, int erase, bool unlock)
  857. {
  858. int retval = ERROR_OK;
  859. int section;
  860. uint32_t section_offset;
  861. struct flash_bank *c;
  862. int *padding;
  863. section = 0;
  864. section_offset = 0;
  865. if (written)
  866. *written = 0;
  867. if (erase)
  868. {
  869. /* assume all sectors need erasing - stops any problems
  870. * when flash_write is called multiple times */
  871. flash_set_dirty();
  872. }
  873. /* allocate padding array */
  874. padding = malloc(image->num_sections * sizeof(padding));
  875. /* loop until we reach end of the image */
  876. while (section < image->num_sections)
  877. {
  878. uint32_t buffer_size;
  879. uint8_t *buffer;
  880. int section_first;
  881. int section_last;
  882. uint32_t run_address = image->sections[section].base_address + section_offset;
  883. uint32_t run_size = image->sections[section].size - section_offset;
  884. int pad_bytes = 0;
  885. if (image->sections[section].size == 0)
  886. {
  887. LOG_WARNING("empty section %d", section);
  888. section++;
  889. section_offset = 0;
  890. continue;
  891. }
  892. /* find the corresponding flash bank */
  893. if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
  894. {
  895. section++; /* and skip it */
  896. section_offset = 0;
  897. continue;
  898. }
  899. /* collect consecutive sections which fall into the same bank */
  900. section_first = section;
  901. section_last = section;
  902. padding[section] = 0;
  903. while ((run_address + run_size - 1 < c->base + c->size - 1)
  904. && (section_last + 1 < image->num_sections))
  905. {
  906. if (image->sections[section_last + 1].base_address < (run_address + run_size))
  907. {
  908. LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
  909. break;
  910. }
  911. /* if we have multiple sections within our image, flash programming could fail due to alignment issues
  912. * attempt to rebuild a consecutive buffer for the flash loader */
  913. pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
  914. if ((run_address + run_size + pad_bytes) > (c->base + c->size))
  915. break;
  916. padding[section_last] = pad_bytes;
  917. run_size += image->sections[++section_last].size;
  918. run_size += pad_bytes;
  919. padding[section_last] = 0;
  920. LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes);
  921. }
  922. /* fit the run into bank constraints */
  923. if (run_address + run_size - 1 > c->base + c->size - 1)
  924. {
  925. LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
  926. (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size));
  927. run_size = c->base + c->size - run_address;
  928. }
  929. /* allocate buffer */
  930. buffer = malloc(run_size);
  931. buffer_size = 0;
  932. /* read sections to the buffer */
  933. while (buffer_size < run_size)
  934. {
  935. size_t size_read;
  936. size_read = run_size - buffer_size;
  937. if (size_read > image->sections[section].size - section_offset)
  938. size_read = image->sections[section].size - section_offset;
  939. if ((retval = image_read_section(image, section, section_offset,
  940. size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
  941. {
  942. free(buffer);
  943. free(padding);
  944. return retval;
  945. }
  946. /* see if we need to pad the section */
  947. while (padding[section]--)
  948. (buffer + buffer_size)[size_read++] = 0xff;
  949. buffer_size += size_read;
  950. section_offset += size_read;
  951. if (section_offset >= image->sections[section].size)
  952. {
  953. section++;
  954. section_offset = 0;
  955. }
  956. }
  957. retval = ERROR_OK;
  958. if (unlock)
  959. {
  960. retval = flash_unlock_address_range(target, run_address, run_size);
  961. }
  962. if (retval == ERROR_OK)
  963. {
  964. if (erase)
  965. {
  966. /* calculate and erase sectors */
  967. retval = flash_erase_address_range(target, run_address, run_size);
  968. }
  969. }
  970. if (retval == ERROR_OK)
  971. {
  972. /* write flash sectors */
  973. retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
  974. }
  975. free(buffer);
  976. if (retval != ERROR_OK)
  977. {
  978. free(padding);
  979. return retval; /* abort operation */
  980. }
  981. if (written != NULL)
  982. *written += run_size; /* add run size to total written counter */
  983. }
  984. free(padding);
  985. return retval;
  986. }
  987. int flash_write(struct target *target, struct image *image, uint32_t *written, int erase)
  988. {
  989. return flash_write_unlock(target, image, written, erase, false);
  990. }
  991. int default_flash_mem_blank_check(struct flash_bank *bank)
  992. {
  993. struct target *target = bank->target;
  994. const int buffer_size = 1024;
  995. int i;
  996. uint32_t nBytes;
  997. int retval = ERROR_OK;
  998. if (bank->target->state != TARGET_HALTED)
  999. {
  1000. LOG_ERROR("Target not halted");
  1001. return ERROR_TARGET_NOT_HALTED;
  1002. }
  1003. uint8_t *buffer = malloc(buffer_size);
  1004. for (i = 0; i < bank->num_sectors; i++)
  1005. {
  1006. uint32_t j;
  1007. bank->sectors[i].is_erased = 1;
  1008. for (j = 0; j < bank->sectors[i].size; j += buffer_size)
  1009. {
  1010. uint32_t chunk;
  1011. chunk = buffer_size;
  1012. if (chunk > (j - bank->sectors[i].size))
  1013. {
  1014. chunk = (j - bank->sectors[i].size);
  1015. }
  1016. retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
  1017. if (retval != ERROR_OK)
  1018. {
  1019. goto done;
  1020. }
  1021. for (nBytes = 0; nBytes < chunk; nBytes++)
  1022. {
  1023. if (buffer[nBytes] != 0xFF)
  1024. {
  1025. bank->sectors[i].is_erased = 0;
  1026. break;
  1027. }
  1028. }
  1029. }
  1030. }
  1031. done:
  1032. free(buffer);
  1033. return retval;
  1034. }
  1035. int default_flash_blank_check(struct flash_bank *bank)
  1036. {
  1037. struct target *target = bank->target;
  1038. int i;
  1039. int retval;
  1040. int fast_check = 0;
  1041. uint32_t blank;
  1042. if (bank->target->state != TARGET_HALTED)
  1043. {
  1044. LOG_ERROR("Target not halted");
  1045. return ERROR_TARGET_NOT_HALTED;
  1046. }
  1047. for (i = 0; i < bank->num_sectors; i++)
  1048. {
  1049. uint32_t address = bank->base + bank->sectors[i].offset;
  1050. uint32_t size = bank->sectors[i].size;
  1051. if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
  1052. {
  1053. fast_check = 0;
  1054. break;
  1055. }
  1056. if (blank == 0xFF)
  1057. bank->sectors[i].is_erased = 1;
  1058. else
  1059. bank->sectors[i].is_erased = 0;
  1060. fast_check = 1;
  1061. }
  1062. if (!fast_check)
  1063. {
  1064. LOG_USER("Running slow fallback erase check - add working memory");
  1065. return default_flash_mem_blank_check(bank);
  1066. }
  1067. return ERROR_OK;
  1068. }
  1069. static const struct command_registration flash_exec_command_handlers[] = {
  1070. {
  1071. .name = "probe",
  1072. .handler = &handle_flash_probe_command,
  1073. .mode = COMMAND_EXEC,
  1074. .usage = "<bank>",
  1075. .help = "identify flash bank",
  1076. },
  1077. {
  1078. .name = "info",
  1079. .handler = &handle_flash_info_command,
  1080. .mode = COMMAND_EXEC,
  1081. .usage = "<bank>",
  1082. .help = "print bank information",
  1083. },
  1084. {
  1085. .name = "erase_check",
  1086. .handler = &handle_flash_erase_check_command,
  1087. .mode = COMMAND_EXEC,
  1088. .usage = "<bank>",
  1089. .help = "check erase state of sectors",
  1090. },
  1091. {
  1092. .name = "protect_check",
  1093. .handler = &handle_flash_protect_check_command,
  1094. .mode = COMMAND_EXEC,
  1095. .usage = "<bank>",
  1096. .help = "check protection state of sectors",
  1097. },
  1098. {
  1099. .name = "erase_sector",
  1100. .handler = &handle_flash_erase_command,
  1101. .mode = COMMAND_EXEC,
  1102. .usage = "<bank> <first> <last>",
  1103. .help = "erase sectors",
  1104. },
  1105. {
  1106. .name = "erase_address",
  1107. .handler = &handle_flash_erase_address_command,
  1108. .mode = COMMAND_EXEC,
  1109. .usage = "<address> <length>",
  1110. .help = "erase address range",
  1111. },
  1112. {
  1113. .name = "fillw",
  1114. .handler = &handle_flash_fill_command,
  1115. .mode = COMMAND_EXEC,
  1116. .usage = "<bank> <address> <word_pattern> <count>",
  1117. .help = "fill with pattern (no autoerase)",
  1118. },
  1119. {
  1120. .name = "fillh",
  1121. .handler = &handle_flash_fill_command,
  1122. .mode = COMMAND_EXEC,
  1123. .usage = "<bank> <address> <halfword_pattern> <count>",
  1124. .help = "fill with pattern",
  1125. },
  1126. {
  1127. .name = "fillb",
  1128. .handler = &handle_flash_fill_command,
  1129. .mode = COMMAND_EXEC,
  1130. .usage = "<bank> <address> <byte_pattern> <count>",
  1131. .help = "fill with pattern",
  1132. },
  1133. {
  1134. .name = "write_bank",
  1135. .handler = &handle_flash_write_bank_command,
  1136. .mode = COMMAND_EXEC,
  1137. .usage = "<bank> <file> <offset>",
  1138. .help = "write binary data",
  1139. },
  1140. {
  1141. .name = "write_image",
  1142. .handler = &handle_flash_write_image_command,
  1143. .mode = COMMAND_EXEC,
  1144. .usage = "<bank> [erase] [unlock] <file> [offset] [type]",
  1145. .help = "write an image to flash"
  1146. },
  1147. {
  1148. .name = "protect",
  1149. .handler = &handle_flash_protect_command,
  1150. .mode = COMMAND_EXEC,
  1151. .usage = "<bank> <first> <last> <on | off>",
  1152. .help = "set protection of sectors",
  1153. },
  1154. COMMAND_REGISTRATION_DONE
  1155. };
  1156. int flash_init_drivers(struct command_context *cmd_ctx)
  1157. {
  1158. if (!flash_banks)
  1159. return ERROR_OK;
  1160. struct command *parent = command_find_in_context(cmd_ctx, "flash");
  1161. return register_commands(cmd_ctx, parent, flash_exec_command_handlers);
  1162. }
  1163. COMMAND_HANDLER(handle_flash_init_command)
  1164. {
  1165. if (CMD_ARGC != 0)
  1166. return ERROR_COMMAND_SYNTAX_ERROR;
  1167. static bool flash_initialized = false;
  1168. if (flash_initialized)
  1169. {
  1170. LOG_INFO("'flash init' has already been called");
  1171. return ERROR_OK;
  1172. }
  1173. flash_initialized = true;
  1174. LOG_DEBUG("Initializing flash devices...");
  1175. return flash_init_drivers(CMD_CTX);
  1176. }
  1177. static const struct command_registration flash_config_command_handlers[] = {
  1178. {
  1179. .name = "bank",
  1180. .handler = &handle_flash_bank_command,
  1181. .mode = COMMAND_CONFIG,
  1182. .usage = "<name> <driver> <base> <size> "
  1183. "<chip_width> <bus_width> <target> "
  1184. "[driver_options ...]",
  1185. .help = "Define a new bank with the given name, "
  1186. "using the specified NOR flash driver.",
  1187. },
  1188. {
  1189. .name = "init",
  1190. .mode = COMMAND_CONFIG,
  1191. .handler = &handle_flash_init_command,
  1192. .help = "initialize flash devices",
  1193. },
  1194. {
  1195. .name = "banks",
  1196. .mode = COMMAND_ANY,
  1197. .jim_handler = &jim_flash_banks,
  1198. .help = "return information about the flash banks",
  1199. },
  1200. COMMAND_REGISTRATION_DONE
  1201. };
  1202. static const struct command_registration flash_command_handlers[] = {
  1203. {
  1204. .name = "flash",
  1205. .mode = COMMAND_ANY,
  1206. .help = "NOR flash command group",
  1207. .chain = flash_config_command_handlers,
  1208. },
  1209. COMMAND_REGISTRATION_DONE
  1210. };
  1211. int flash_register_commands(struct command_context *cmd_ctx)
  1212. {
  1213. return register_commands(cmd_ctx, NULL, flash_command_handlers);
  1214. }