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.
 
 
 
 
 
 

1306 lines
33 KiB

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