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.
 
 
 
 
 
 

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