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.
 
 
 
 
 
 

1242 lines
31 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 "image.h"
  31. #include "time_support.h"
  32. /* command handlers */
  33. static int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  34. static int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  35. static int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  36. static int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  37. static int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  38. static int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  39. static int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  40. static int handle_flash_write_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  41. static int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  42. static int handle_flash_fill_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  43. static int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  44. /* flash drivers
  45. */
  46. extern flash_driver_t lpc2000_flash;
  47. extern flash_driver_t cfi_flash;
  48. extern flash_driver_t at91sam7_flash;
  49. extern flash_driver_t str7x_flash;
  50. extern flash_driver_t str9x_flash;
  51. extern flash_driver_t aduc702x_flash;
  52. extern flash_driver_t stellaris_flash;
  53. extern flash_driver_t str9xpec_flash;
  54. extern flash_driver_t stm32x_flash;
  55. extern flash_driver_t tms470_flash;
  56. extern flash_driver_t ecosflash_flash;
  57. extern flash_driver_t lpc288x_flash;
  58. extern flash_driver_t ocl_flash;
  59. extern flash_driver_t pic32mx_flash;
  60. extern flash_driver_t avr_flash;
  61. flash_driver_t *flash_drivers[] = {
  62. &lpc2000_flash,
  63. &cfi_flash,
  64. &at91sam7_flash,
  65. &str7x_flash,
  66. &str9x_flash,
  67. &aduc702x_flash,
  68. &stellaris_flash,
  69. &str9xpec_flash,
  70. &stm32x_flash,
  71. &tms470_flash,
  72. &ecosflash_flash,
  73. &lpc288x_flash,
  74. &ocl_flash,
  75. &pic32mx_flash,
  76. &avr_flash,
  77. NULL,
  78. };
  79. flash_bank_t *flash_banks;
  80. static command_t *flash_cmd;
  81. /* wafer thin wrapper for invoking the flash driver */
  82. static int flash_driver_write(struct flash_bank_s *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
  83. {
  84. int retval;
  85. retval=bank->driver->write(bank, buffer, offset, count);
  86. if (retval!=ERROR_OK)
  87. {
  88. LOG_ERROR("error writing to flash at address 0x%08" PRIx32 " at offset 0x%8.8" PRIx32 " (%d)",
  89. bank->base, offset, retval);
  90. }
  91. return retval;
  92. }
  93. static int flash_driver_erase(struct flash_bank_s *bank, int first, int last)
  94. {
  95. int retval;
  96. retval=bank->driver->erase(bank, first, last);
  97. if (retval!=ERROR_OK)
  98. {
  99. LOG_ERROR("failed erasing sectors %d to %d (%d)", first, last, retval);
  100. }
  101. return retval;
  102. }
  103. int flash_driver_protect(struct flash_bank_s *bank, int set, int first, int last)
  104. {
  105. int retval;
  106. retval=bank->driver->protect(bank, set, first, last);
  107. if (retval!=ERROR_OK)
  108. {
  109. LOG_ERROR("failed setting protection for areas %d to %d (%d)", first, last, retval);
  110. }
  111. return retval;
  112. }
  113. int flash_register_commands(struct command_context_s *cmd_ctx)
  114. {
  115. flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
  116. register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, "flash bank <driver> <base> <size> <chip_width> <bus_width> <target> [driver_options ...]");
  117. return ERROR_OK;
  118. }
  119. static int jim_flash_banks(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  120. {
  121. flash_bank_t *p;
  122. if (argc != 1) {
  123. Jim_WrongNumArgs(interp, 1, argv, "no arguments to flash_banks command");
  124. return JIM_ERR;
  125. }
  126. Jim_Obj *list=Jim_NewListObj(interp, NULL, 0);
  127. for (p = flash_banks; p; p = p->next)
  128. {
  129. Jim_Obj *elem=Jim_NewListObj(interp, NULL, 0);
  130. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "name", -1));
  131. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, p->driver->name, -1));
  132. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "base", -1));
  133. Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->base));
  134. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "size", -1));
  135. Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->size));
  136. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "bus_width", -1));
  137. Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->bus_width));
  138. Jim_ListAppendElement(interp, elem, Jim_NewStringObj(interp, "chip_width", -1));
  139. Jim_ListAppendElement(interp, elem, Jim_NewIntObj(interp, p->chip_width));
  140. Jim_ListAppendElement(interp, list, elem);
  141. }
  142. Jim_SetResult(interp, list);
  143. return JIM_OK;
  144. }
  145. int flash_init_drivers(struct command_context_s *cmd_ctx)
  146. {
  147. register_jim(cmd_ctx, "ocd_flash_banks", jim_flash_banks, "return information about the flash banks");
  148. if (flash_banks)
  149. {
  150. register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
  151. "print info about flash bank <num>");
  152. register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
  153. "identify flash bank <num>");
  154. register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
  155. "check erase state of sectors in flash bank <num>");
  156. register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
  157. "check protection state of sectors in flash bank <num>");
  158. register_command(cmd_ctx, flash_cmd, "erase_sector", handle_flash_erase_command, COMMAND_EXEC,
  159. "erase sectors at <bank> <first> <last>");
  160. register_command(cmd_ctx, flash_cmd, "erase_address", handle_flash_erase_address_command, COMMAND_EXEC,
  161. "erase address range <address> <length>");
  162. register_command(cmd_ctx, flash_cmd, "fillw", handle_flash_fill_command, COMMAND_EXEC,
  163. "fill with pattern (no autoerase) <address> <word_pattern> <count>");
  164. register_command(cmd_ctx, flash_cmd, "fillh", handle_flash_fill_command, COMMAND_EXEC,
  165. "fill with pattern <address> <halfword_pattern> <count>");
  166. register_command(cmd_ctx, flash_cmd, "fillb", handle_flash_fill_command, COMMAND_EXEC,
  167. "fill with pattern <address> <byte_pattern> <count>");
  168. register_command(cmd_ctx, flash_cmd, "write_bank", handle_flash_write_bank_command, COMMAND_EXEC,
  169. "write binary data to <bank> <file> <offset>");
  170. register_command(cmd_ctx, flash_cmd, "write_image", handle_flash_write_image_command, COMMAND_EXEC,
  171. "write_image [erase] <file> [offset] [type]");
  172. register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
  173. "set protection of sectors at <bank> <first> <last> <on|off>");
  174. }
  175. return ERROR_OK;
  176. }
  177. flash_bank_t *get_flash_bank_by_num_noprobe(int num)
  178. {
  179. flash_bank_t *p;
  180. int i = 0;
  181. for (p = flash_banks; p; p = p->next)
  182. {
  183. if (i++ == num)
  184. {
  185. return p;
  186. }
  187. }
  188. LOG_ERROR("flash bank %d does not exist", num);
  189. return NULL;
  190. }
  191. int flash_get_bank_count(void)
  192. {
  193. flash_bank_t *p;
  194. int i = 0;
  195. for (p = flash_banks; p; p = p->next)
  196. {
  197. i++;
  198. }
  199. return i;
  200. }
  201. flash_bank_t *get_flash_bank_by_num(int num)
  202. {
  203. flash_bank_t *p = get_flash_bank_by_num_noprobe(num);
  204. int retval;
  205. if (p == NULL)
  206. return NULL;
  207. retval = p->driver->auto_probe(p);
  208. if (retval != ERROR_OK)
  209. {
  210. LOG_ERROR("auto_probe failed %d\n", retval);
  211. return NULL;
  212. }
  213. return p;
  214. }
  215. static int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  216. {
  217. int retval;
  218. int i;
  219. int found = 0;
  220. target_t *target;
  221. if (argc < 6)
  222. {
  223. return ERROR_COMMAND_SYNTAX_ERROR;
  224. }
  225. if ((target = get_target(args[5])) == NULL)
  226. {
  227. LOG_ERROR("target '%s' not defined", args[5]);
  228. return ERROR_FAIL;
  229. }
  230. for (i = 0; flash_drivers[i]; i++)
  231. {
  232. if (strcmp(args[0], flash_drivers[i]->name) == 0)
  233. {
  234. flash_bank_t *p, *c;
  235. /* register flash specific commands */
  236. if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
  237. {
  238. LOG_ERROR("couldn't register '%s' commands", args[0]);
  239. return ERROR_FAIL;
  240. }
  241. c = malloc(sizeof(flash_bank_t));
  242. c->target = target;
  243. c->driver = flash_drivers[i];
  244. c->driver_priv = NULL;
  245. c->base = strtoul(args[1], NULL, 0);
  246. c->size = strtoul(args[2], NULL, 0);
  247. c->chip_width = strtoul(args[3], NULL, 0);
  248. c->bus_width = strtoul(args[4], NULL, 0);
  249. c->num_sectors = 0;
  250. c->sectors = NULL;
  251. c->next = NULL;
  252. if ((retval=flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c)) != ERROR_OK)
  253. {
  254. LOG_ERROR("'%s' driver rejected flash bank at 0x%8.8" PRIx32 , args[0], c->base);
  255. free(c);
  256. return retval;
  257. }
  258. /* put flash bank in linked list */
  259. if (flash_banks)
  260. {
  261. int bank_num = 0;
  262. /* find last flash bank */
  263. for (p = flash_banks; p && p->next; p = p->next) bank_num++;
  264. if (p)
  265. p->next = c;
  266. c->bank_number = bank_num + 1;
  267. }
  268. else
  269. {
  270. flash_banks = c;
  271. c->bank_number = 0;
  272. }
  273. found = 1;
  274. }
  275. }
  276. /* no matching flash driver found */
  277. if (!found)
  278. {
  279. LOG_ERROR("flash driver '%s' not found", args[0]);
  280. return ERROR_FAIL;
  281. }
  282. return ERROR_OK;
  283. }
  284. static int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  285. {
  286. flash_bank_t *p;
  287. uint32_t i = 0;
  288. int j = 0;
  289. int retval;
  290. if (argc != 1)
  291. {
  292. return ERROR_COMMAND_SYNTAX_ERROR;
  293. }
  294. for (p = flash_banks; p; p = p->next, i++)
  295. {
  296. if (i == strtoul(args[0], NULL, 0))
  297. {
  298. char buf[1024];
  299. /* attempt auto probe */
  300. if ((retval = p->driver->auto_probe(p)) != ERROR_OK)
  301. return retval;
  302. command_print(cmd_ctx,
  303. "#%" PRIi32 " : %s at 0x%8.8" PRIx32 ", size 0x%8.8" PRIx32 ", buswidth %i, chipwidth %i",
  304. i,
  305. p->driver->name,
  306. p->base,
  307. p->size,
  308. p->bus_width,
  309. p->chip_width);
  310. for (j = 0; j < p->num_sectors; j++)
  311. {
  312. char *protect_state;
  313. if (p->sectors[j].is_protected == 0)
  314. protect_state = "not protected";
  315. else if (p->sectors[j].is_protected == 1)
  316. protect_state = "protected";
  317. else
  318. protect_state = "protection state unknown";
  319. command_print(cmd_ctx,
  320. "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
  321. j,
  322. p->sectors[j].offset,
  323. p->sectors[j].size,
  324. p->sectors[j].size>>10,
  325. protect_state);
  326. }
  327. *buf = '\0'; /* initialize buffer, otherwise it migh contain garbage if driver function fails */
  328. retval = p->driver->info(p, buf, sizeof(buf));
  329. command_print(cmd_ctx, "%s", buf);
  330. if (retval != ERROR_OK)
  331. LOG_ERROR("error retrieving flash info (%d)", retval);
  332. }
  333. }
  334. return ERROR_OK;
  335. }
  336. static int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  337. {
  338. flash_bank_t *p;
  339. int retval;
  340. if (argc != 1)
  341. {
  342. return ERROR_COMMAND_SYNTAX_ERROR;
  343. }
  344. p = get_flash_bank_by_num_noprobe(strtoul(args[0], NULL, 0));
  345. if (p)
  346. {
  347. if ((retval = p->driver->probe(p)) == ERROR_OK)
  348. {
  349. command_print(cmd_ctx, "flash '%s' found at 0x%8.8" PRIx32, p->driver->name, p->base);
  350. }
  351. else if (retval == ERROR_FLASH_BANK_INVALID)
  352. {
  353. command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8" PRIx32,
  354. args[0], p->base);
  355. }
  356. else
  357. {
  358. command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8" PRIx32,
  359. args[0], p->base);
  360. }
  361. }
  362. else
  363. {
  364. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  365. }
  366. return ERROR_OK;
  367. }
  368. static int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  369. {
  370. flash_bank_t *p;
  371. int retval;
  372. if (argc != 1)
  373. {
  374. return ERROR_COMMAND_SYNTAX_ERROR;
  375. }
  376. p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  377. if (p)
  378. {
  379. int j;
  380. if ((retval = p->driver->erase_check(p)) == ERROR_OK)
  381. {
  382. command_print(cmd_ctx, "successfully checked erase state");
  383. }
  384. else
  385. {
  386. command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8" PRIx32,
  387. args[0], p->base);
  388. }
  389. for (j = 0; j < p->num_sectors; j++)
  390. {
  391. char *erase_state;
  392. if (p->sectors[j].is_erased == 0)
  393. erase_state = "not erased";
  394. else if (p->sectors[j].is_erased == 1)
  395. erase_state = "erased";
  396. else
  397. erase_state = "erase state unknown";
  398. command_print(cmd_ctx,
  399. "\t#%3i: 0x%8.8" PRIx32 " (0x%" PRIx32 " %" PRIi32 "kB) %s",
  400. j,
  401. p->sectors[j].offset,
  402. p->sectors[j].size,
  403. p->sectors[j].size>>10,
  404. erase_state);
  405. }
  406. }
  407. return ERROR_OK;
  408. }
  409. static int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  410. {
  411. flash_bank_t *p;
  412. int retval;
  413. int address;
  414. int length;
  415. duration_t duration;
  416. char *duration_text;
  417. target_t *target = get_current_target(cmd_ctx);
  418. if (argc != 2)
  419. {
  420. return ERROR_COMMAND_SYNTAX_ERROR;
  421. }
  422. address = strtoul(args[0], NULL, 0);
  423. length = strtoul(args[1], NULL, 0);
  424. if (length <= 0)
  425. {
  426. command_print(cmd_ctx, "Length must be >0");
  427. return ERROR_COMMAND_SYNTAX_ERROR;
  428. }
  429. p = get_flash_bank_by_addr(target, address);
  430. if (p == NULL)
  431. {
  432. return ERROR_FAIL;
  433. }
  434. /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
  435. flash_set_dirty();
  436. duration_start_measure(&duration);
  437. if ((retval = flash_erase_address_range(target, address, length)) == ERROR_OK)
  438. {
  439. if ((retval = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
  440. {
  441. return retval;
  442. }
  443. command_print(cmd_ctx, "erased address 0x%8.8x length %i in %s", address, length, duration_text);
  444. free(duration_text);
  445. }
  446. return retval;
  447. }
  448. static int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  449. {
  450. flash_bank_t *p;
  451. int retval;
  452. if (argc != 1)
  453. {
  454. return ERROR_COMMAND_SYNTAX_ERROR;
  455. }
  456. p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  457. if (p)
  458. {
  459. if ((retval = p->driver->protect_check(p)) == ERROR_OK)
  460. {
  461. command_print(cmd_ctx, "successfully checked protect state");
  462. }
  463. else if (retval == ERROR_FLASH_OPERATION_FAILED)
  464. {
  465. command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8" PRIx32, args[0], p->base);
  466. }
  467. else
  468. {
  469. command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8" PRIx32, args[0], p->base);
  470. }
  471. }
  472. else
  473. {
  474. return ERROR_COMMAND_SYNTAX_ERROR;
  475. }
  476. return ERROR_OK;
  477. }
  478. static int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  479. {
  480. if (argc > 2)
  481. {
  482. int first = strtoul(args[1], NULL, 0);
  483. int last = strtoul(args[2], NULL, 0);
  484. int retval;
  485. flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  486. duration_t duration;
  487. char *duration_text;
  488. duration_start_measure(&duration);
  489. if (!p)
  490. {
  491. return ERROR_COMMAND_SYNTAX_ERROR;
  492. }
  493. if ((retval = flash_driver_erase(p, first, last)) == ERROR_OK)
  494. {
  495. if ((retval = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
  496. {
  497. return retval;
  498. }
  499. command_print(cmd_ctx, "erased sectors %i through %i on flash bank %li in %s",
  500. first, last, strtoul(args[0], 0, 0), duration_text);
  501. free(duration_text);
  502. }
  503. }
  504. else
  505. {
  506. return ERROR_COMMAND_SYNTAX_ERROR;
  507. }
  508. return ERROR_OK;
  509. }
  510. static int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  511. {
  512. if (argc > 3)
  513. {
  514. int first = strtoul(args[1], NULL, 0);
  515. int last = strtoul(args[2], NULL, 0);
  516. int set;
  517. int retval;
  518. flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  519. if (!p)
  520. {
  521. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  522. return ERROR_OK;
  523. }
  524. if (strcmp(args[3], "on") == 0)
  525. set = 1;
  526. else if (strcmp(args[3], "off") == 0)
  527. set = 0;
  528. else
  529. {
  530. return ERROR_COMMAND_SYNTAX_ERROR;
  531. }
  532. retval = flash_driver_protect(p, set, first, last);
  533. if (retval == ERROR_OK)
  534. {
  535. command_print(cmd_ctx, "%s protection for sectors %i through %i on flash bank %li",
  536. (set) ? "set" : "cleared", first,
  537. last, strtoul(args[0], 0, 0));
  538. }
  539. }
  540. else
  541. {
  542. return ERROR_COMMAND_SYNTAX_ERROR;
  543. }
  544. return ERROR_OK;
  545. }
  546. static int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  547. {
  548. target_t *target = get_current_target(cmd_ctx);
  549. image_t image;
  550. uint32_t written;
  551. duration_t duration;
  552. char *duration_text;
  553. int retval, retvaltemp;
  554. if (argc < 1)
  555. {
  556. return ERROR_COMMAND_SYNTAX_ERROR;
  557. }
  558. /* flash auto-erase is disabled by default*/
  559. int auto_erase = 0;
  560. if (strcmp(args[0], "erase")==0)
  561. {
  562. auto_erase = 1;
  563. args++;
  564. argc--;
  565. command_print(cmd_ctx, "auto erase enabled");
  566. }
  567. if (argc < 1)
  568. {
  569. return ERROR_COMMAND_SYNTAX_ERROR;
  570. }
  571. if (!target)
  572. {
  573. LOG_ERROR("no target selected");
  574. return ERROR_FAIL;
  575. }
  576. duration_start_measure(&duration);
  577. if (argc >= 2)
  578. {
  579. image.base_address_set = 1;
  580. image.base_address = strtoul(args[1], NULL, 0);
  581. }
  582. else
  583. {
  584. image.base_address_set = 0;
  585. image.base_address = 0x0;
  586. }
  587. image.start_address_set = 0;
  588. retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL);
  589. if (retval != ERROR_OK)
  590. {
  591. return retval;
  592. }
  593. retval = flash_write(target, &image, &written, auto_erase);
  594. if (retval != ERROR_OK)
  595. {
  596. image_close(&image);
  597. return retval;
  598. }
  599. if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
  600. {
  601. image_close(&image);
  602. return retvaltemp;
  603. }
  604. if (retval == ERROR_OK)
  605. {
  606. command_print(cmd_ctx,
  607. "wrote %" PRIu32 " byte from file %s in %s (%f kb/s)",
  608. written,
  609. args[0],
  610. duration_text,
  611. (float)written / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
  612. }
  613. free(duration_text);
  614. image_close(&image);
  615. return retval;
  616. }
  617. static int handle_flash_fill_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  618. {
  619. int err = ERROR_OK, retval;
  620. uint32_t address;
  621. uint32_t pattern;
  622. uint32_t count;
  623. uint8_t chunk[1024];
  624. uint8_t readback[1024];
  625. uint32_t wrote = 0;
  626. uint32_t cur_size = 0;
  627. uint32_t chunk_count;
  628. char *duration_text;
  629. duration_t duration;
  630. target_t *target = get_current_target(cmd_ctx);
  631. uint32_t i;
  632. uint32_t wordsize;
  633. if (argc != 3)
  634. {
  635. return ERROR_COMMAND_SYNTAX_ERROR;
  636. }
  637. address = strtoul(args[0], NULL, 0);
  638. pattern = strtoul(args[1], NULL, 0);
  639. count = strtoul(args[2], NULL, 0);
  640. if (count == 0)
  641. return ERROR_OK;
  642. switch (cmd[4])
  643. {
  644. case 'w':
  645. wordsize=4;
  646. break;
  647. case 'h':
  648. wordsize=2;
  649. break;
  650. case 'b':
  651. wordsize=1;
  652. break;
  653. default:
  654. return ERROR_COMMAND_SYNTAX_ERROR;
  655. }
  656. chunk_count = MIN(count, (1024 / wordsize));
  657. switch (wordsize)
  658. {
  659. case 4:
  660. for (i = 0; i < chunk_count; i++)
  661. {
  662. target_buffer_set_u32(target, chunk + i * wordsize, pattern);
  663. }
  664. break;
  665. case 2:
  666. for (i = 0; i < chunk_count; i++)
  667. {
  668. target_buffer_set_u16(target, chunk + i * wordsize, pattern);
  669. }
  670. break;
  671. case 1:
  672. memset(chunk, pattern, chunk_count);
  673. break;
  674. default:
  675. LOG_ERROR("BUG: can't happen");
  676. exit(-1);
  677. }
  678. duration_start_measure(&duration);
  679. for (wrote=0; wrote<(count*wordsize); wrote += cur_size)
  680. {
  681. cur_size = MIN( (count*wordsize - wrote), sizeof(chunk) );
  682. flash_bank_t *bank;
  683. bank = get_flash_bank_by_addr(target, address);
  684. if (bank == NULL)
  685. {
  686. return ERROR_FAIL;
  687. }
  688. err = flash_driver_write(bank, chunk, address - bank->base + wrote, cur_size);
  689. if (err!=ERROR_OK)
  690. return err;
  691. err = target_read_buffer(target, address + wrote, cur_size, readback);
  692. if (err!=ERROR_OK)
  693. return err;
  694. unsigned i;
  695. for (i=0; i<cur_size; i++)
  696. {
  697. if (readback[i]!=chunk[i])
  698. {
  699. LOG_ERROR("Verfication error address 0x%08" PRIx32 ", read back 0x%02x, expected 0x%02x",
  700. address + wrote + i, readback[i], chunk[i]);
  701. return ERROR_FAIL;
  702. }
  703. }
  704. }
  705. if ((retval = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
  706. {
  707. return retval;
  708. }
  709. if (err == ERROR_OK)
  710. {
  711. float speed;
  712. speed=wrote / 1024.0;
  713. speed/=((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0));
  714. command_print(cmd_ctx,
  715. "wrote %" PRId32 " bytes to 0x%8.8" PRIx32 " in %s (%f kb/s)",
  716. count*wordsize,
  717. address,
  718. duration_text,
  719. speed);
  720. }
  721. free(duration_text);
  722. return ERROR_OK;
  723. }
  724. static int handle_flash_write_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  725. {
  726. uint32_t offset;
  727. uint8_t *buffer;
  728. uint32_t buf_cnt;
  729. fileio_t fileio;
  730. duration_t duration;
  731. char *duration_text;
  732. int retval, retvaltemp;
  733. flash_bank_t *p;
  734. if (argc != 3)
  735. {
  736. return ERROR_COMMAND_SYNTAX_ERROR;
  737. }
  738. duration_start_measure(&duration);
  739. offset = strtoul(args[2], NULL, 0);
  740. p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  741. if (!p)
  742. {
  743. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  744. return ERROR_OK;
  745. }
  746. if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
  747. {
  748. return ERROR_OK;
  749. }
  750. buffer = malloc(fileio.size);
  751. if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
  752. {
  753. free(buffer);
  754. fileio_close(&fileio);
  755. return ERROR_OK;
  756. }
  757. retval = flash_driver_write(p, buffer, offset, buf_cnt);
  758. free(buffer);
  759. buffer = NULL;
  760. if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
  761. {
  762. fileio_close(&fileio);
  763. return retvaltemp;
  764. }
  765. if (retval==ERROR_OK)
  766. {
  767. command_print(cmd_ctx,
  768. "wrote %lld byte from file %s to flash bank %li at offset 0x%8.8" PRIx32 " in %s (%f kb/s)",
  769. fileio.size,
  770. args[1],
  771. strtoul(args[0], NULL, 0),
  772. offset,
  773. duration_text,
  774. (float)fileio.size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
  775. }
  776. free(duration_text);
  777. fileio_close(&fileio);
  778. return retval;
  779. }
  780. void flash_set_dirty(void)
  781. {
  782. flash_bank_t *c;
  783. int i;
  784. /* set all flash to require erasing */
  785. for (c = flash_banks; c; c = c->next)
  786. {
  787. for (i = 0; i < c->num_sectors; i++)
  788. {
  789. c->sectors[i].is_erased = 0;
  790. }
  791. }
  792. }
  793. /* lookup flash bank by address */
  794. flash_bank_t *get_flash_bank_by_addr(target_t *target, uint32_t addr)
  795. {
  796. flash_bank_t *c;
  797. /* cycle through bank list */
  798. for (c = flash_banks; c; c = c->next)
  799. {
  800. int retval;
  801. retval = c->driver->auto_probe(c);
  802. if (retval != ERROR_OK)
  803. {
  804. LOG_ERROR("auto_probe failed %d\n", retval);
  805. return NULL;
  806. }
  807. /* check whether address belongs to this flash bank */
  808. if ((addr >= c->base) && (addr <= c->base + (c->size - 1)) && target == c->target)
  809. return c;
  810. }
  811. LOG_ERROR("No flash at address 0x%08" PRIx32 "\n", addr);
  812. return NULL;
  813. }
  814. /* erase given flash region, selects proper bank according to target and address */
  815. int flash_erase_address_range(target_t *target, uint32_t addr, uint32_t length)
  816. {
  817. flash_bank_t *c;
  818. int first = -1;
  819. int last = -1;
  820. int i;
  821. if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
  822. return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
  823. if (c->size == 0 || c->num_sectors == 0)
  824. {
  825. LOG_ERROR("Bank is invalid");
  826. return ERROR_FLASH_BANK_INVALID;
  827. }
  828. if (length == 0)
  829. {
  830. /* special case, erase whole bank when length is zero */
  831. if (addr != c->base)
  832. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  833. return flash_driver_erase(c, 0, c->num_sectors - 1);
  834. }
  835. /* check whether it fits */
  836. if (addr + length - 1 > c->base + c->size - 1)
  837. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  838. addr -= c->base;
  839. for (i = 0; i < c->num_sectors; i++)
  840. {
  841. /* check whether sector overlaps with the given range and is not yet erased */
  842. if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
  843. /* if first is not set yet then this is the first sector */
  844. if (first == -1)
  845. first = i;
  846. last = i; /* and it is the last one so far in any case */
  847. }
  848. }
  849. if ( first == -1 || last == -1 )
  850. return ERROR_OK;
  851. return flash_driver_erase(c, first, last);
  852. }
  853. /* write (optional verify) an image to flash memory of the given target */
  854. int flash_write(target_t *target, image_t *image, uint32_t *written, int erase)
  855. {
  856. int retval=ERROR_OK;
  857. int section;
  858. uint32_t section_offset;
  859. flash_bank_t *c;
  860. int *padding;
  861. section = 0;
  862. section_offset = 0;
  863. if (written)
  864. *written = 0;
  865. if (erase)
  866. {
  867. /* assume all sectors need erasing - stops any problems
  868. * when flash_write is called multiple times */
  869. flash_set_dirty();
  870. }
  871. /* allocate padding array */
  872. padding = malloc(image->num_sections * sizeof(padding));
  873. /* loop until we reach end of the image */
  874. while (section < image->num_sections)
  875. {
  876. uint32_t buffer_size;
  877. uint8_t *buffer;
  878. int section_first;
  879. int section_last;
  880. uint32_t run_address = image->sections[section].base_address + section_offset;
  881. uint32_t run_size = image->sections[section].size - section_offset;
  882. int pad_bytes = 0;
  883. if (image->sections[section].size == 0)
  884. {
  885. LOG_WARNING("empty section %d", section);
  886. section++;
  887. section_offset = 0;
  888. continue;
  889. }
  890. /* find the corresponding flash bank */
  891. if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
  892. {
  893. section++; /* and skip it */
  894. section_offset = 0;
  895. continue;
  896. }
  897. /* collect consecutive sections which fall into the same bank */
  898. section_first = section;
  899. section_last = section;
  900. padding[section] = 0;
  901. while ((run_address + run_size - 1 < c->base + c->size - 1)
  902. && (section_last + 1 < image->num_sections))
  903. {
  904. if (image->sections[section_last + 1].base_address < (run_address + run_size))
  905. {
  906. LOG_DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
  907. break;
  908. }
  909. /* if we have multiple sections within our image, flash programming could fail due to alignment issues
  910. * attempt to rebuild a consecutive buffer for the flash loader */
  911. pad_bytes = (image->sections[section_last + 1].base_address) - (run_address + run_size);
  912. if ((run_address + run_size + pad_bytes) > (c->base + c->size))
  913. break;
  914. padding[section_last] = pad_bytes;
  915. run_size += image->sections[++section_last].size;
  916. run_size += pad_bytes;
  917. padding[section_last] = 0;
  918. LOG_INFO("Padding image section %d with %d bytes", section_last-1, pad_bytes );
  919. }
  920. /* fit the run into bank constraints */
  921. if (run_address + run_size - 1 > c->base + c->size - 1)
  922. {
  923. LOG_WARNING("writing %d bytes only - as image section is %d bytes and bank is only %d bytes", \
  924. (int)(c->base + c->size - run_address), (int)(run_size), (int)(c->size));
  925. run_size = c->base + c->size - run_address;
  926. }
  927. /* allocate buffer */
  928. buffer = malloc(run_size);
  929. buffer_size = 0;
  930. /* read sections to the buffer */
  931. while (buffer_size < run_size)
  932. {
  933. uint32_t size_read;
  934. size_read = run_size - buffer_size;
  935. if (size_read > image->sections[section].size - section_offset)
  936. size_read = image->sections[section].size - section_offset;
  937. if ((retval = image_read_section(image, section, section_offset,
  938. size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
  939. {
  940. free(buffer);
  941. free(padding);
  942. return retval;
  943. }
  944. /* see if we need to pad the section */
  945. while (padding[section]--)
  946. (buffer+buffer_size)[size_read++] = 0xff;
  947. buffer_size += size_read;
  948. section_offset += size_read;
  949. if (section_offset >= image->sections[section].size)
  950. {
  951. section++;
  952. section_offset = 0;
  953. }
  954. }
  955. retval = ERROR_OK;
  956. if (erase)
  957. {
  958. /* calculate and erase sectors */
  959. retval = flash_erase_address_range( target, run_address, run_size );
  960. }
  961. if (retval == ERROR_OK)
  962. {
  963. /* write flash sectors */
  964. retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
  965. }
  966. free(buffer);
  967. if (retval != ERROR_OK)
  968. {
  969. free(padding);
  970. return retval; /* abort operation */
  971. }
  972. if (written != NULL)
  973. *written += run_size; /* add run size to total written counter */
  974. }
  975. free(padding);
  976. return retval;
  977. }
  978. int default_flash_mem_blank_check(struct flash_bank_s *bank)
  979. {
  980. target_t *target = bank->target;
  981. uint8_t buffer[1024];
  982. int buffer_size = sizeof(buffer);
  983. int i;
  984. uint32_t nBytes;
  985. if (bank->target->state != TARGET_HALTED)
  986. {
  987. LOG_ERROR("Target not halted");
  988. return ERROR_TARGET_NOT_HALTED;
  989. }
  990. for (i = 0; i < bank->num_sectors; i++)
  991. {
  992. uint32_t j;
  993. bank->sectors[i].is_erased = 1;
  994. for (j = 0; j < bank->sectors[i].size; j += buffer_size)
  995. {
  996. uint32_t chunk;
  997. int retval;
  998. chunk = buffer_size;
  999. if (chunk > (j - bank->sectors[i].size))
  1000. {
  1001. chunk = (j - bank->sectors[i].size);
  1002. }
  1003. retval = target_read_memory(target, bank->base + bank->sectors[i].offset + j, 4, chunk/4, buffer);
  1004. if (retval != ERROR_OK)
  1005. return retval;
  1006. for (nBytes = 0; nBytes < chunk; nBytes++)
  1007. {
  1008. if (buffer[nBytes] != 0xFF)
  1009. {
  1010. bank->sectors[i].is_erased = 0;
  1011. break;
  1012. }
  1013. }
  1014. }
  1015. }
  1016. return ERROR_OK;
  1017. }
  1018. int default_flash_blank_check(struct flash_bank_s *bank)
  1019. {
  1020. target_t *target = bank->target;
  1021. int i;
  1022. int retval;
  1023. int fast_check = 0;
  1024. uint32_t blank;
  1025. if (bank->target->state != TARGET_HALTED)
  1026. {
  1027. LOG_ERROR("Target not halted");
  1028. return ERROR_TARGET_NOT_HALTED;
  1029. }
  1030. for (i = 0; i < bank->num_sectors; i++)
  1031. {
  1032. uint32_t address = bank->base + bank->sectors[i].offset;
  1033. uint32_t size = bank->sectors[i].size;
  1034. if ((retval = target_blank_check_memory(target, address, size, &blank)) != ERROR_OK)
  1035. {
  1036. fast_check = 0;
  1037. break;
  1038. }
  1039. if (blank == 0xFF)
  1040. bank->sectors[i].is_erased = 1;
  1041. else
  1042. bank->sectors[i].is_erased = 0;
  1043. fast_check = 1;
  1044. }
  1045. if (!fast_check)
  1046. {
  1047. LOG_USER("Running slow fallback erase check - add working memory");
  1048. return default_flash_mem_blank_check(bank);
  1049. }
  1050. return ERROR_OK;
  1051. }