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.
 
 
 
 
 
 

948 lines
24 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 <string.h>
  31. #include <unistd.h>
  32. #include <stdlib.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <errno.h>
  36. #include <inttypes.h>
  37. /* command handlers */
  38. int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  39. int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  40. int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  41. int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  42. int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  43. int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  44. int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  45. int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  46. int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  47. int handle_flash_write_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  48. int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  49. int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  50. int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  51. flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr);
  52. /* flash drivers
  53. */
  54. extern flash_driver_t lpc2000_flash;
  55. extern flash_driver_t cfi_flash;
  56. extern flash_driver_t at91sam7_flash;
  57. extern flash_driver_t str7x_flash;
  58. extern flash_driver_t str9x_flash;
  59. extern flash_driver_t stellaris_flash;
  60. extern flash_driver_t str9xpec_flash;
  61. extern flash_driver_t stm32x_flash;
  62. extern flash_driver_t tms470_flash;
  63. extern flash_driver_t ecosflash_flash;
  64. flash_driver_t *flash_drivers[] =
  65. {
  66. &lpc2000_flash,
  67. &cfi_flash,
  68. &at91sam7_flash,
  69. &str7x_flash,
  70. &str9x_flash,
  71. &stellaris_flash,
  72. &str9xpec_flash,
  73. &stm32x_flash,
  74. &tms470_flash,
  75. &ecosflash_flash,
  76. NULL,
  77. };
  78. flash_bank_t *flash_banks;
  79. static command_t *flash_cmd;
  80. static int auto_erase = 0;
  81. /* wafer thin wrapper for invoking the flash driver */
  82. static int flash_driver_write(struct flash_bank_s *bank, u8 *buffer, u32 offset, u32 count)
  83. {
  84. int retval=ERROR_OK;
  85. if (bank->target->state != TARGET_HALTED)
  86. {
  87. ERROR("target not halted - aborting flash write");
  88. retval=ERROR_TARGET_NOT_HALTED;
  89. } else
  90. {
  91. retval=bank->driver->write(bank, buffer, offset, count);
  92. }
  93. if (retval!=ERROR_OK)
  94. {
  95. ERROR("Writing to flash bank at address 0x%08x at offset 0x%8.8x", bank->base, offset);
  96. }
  97. return retval;
  98. }
  99. static int flash_driver_erase(struct flash_bank_s *bank, int first, int last)
  100. {
  101. int retval=ERROR_OK;
  102. if (bank->target->state != TARGET_HALTED)
  103. {
  104. ERROR("target not halted - aborting flash erase");
  105. retval=ERROR_TARGET_NOT_HALTED;
  106. } else if ((first < 0) || (last < first) || (last >= bank->num_sectors))
  107. {
  108. ERROR("invalid flash sector");
  109. retval=ERROR_FLASH_SECTOR_INVALID;
  110. } else
  111. {
  112. retval=bank->driver->erase(bank, first, last);
  113. }
  114. if (retval!=ERROR_OK)
  115. {
  116. ERROR("Failed erasing banks %d to %d", first, last);
  117. }
  118. return retval;
  119. }
  120. int flash_driver_protect(struct flash_bank_s *bank, int set, int first, int last)
  121. {
  122. int retval;
  123. if (bank->target->state != TARGET_HALTED)
  124. {
  125. ERROR("target not halted - aborting flash erase");
  126. retval=ERROR_TARGET_NOT_HALTED;
  127. } else if ((first < 0) || (last < first) || (last >= bank->num_sectors))
  128. {
  129. ERROR("invalid flash sector");
  130. retval=ERROR_FLASH_SECTOR_INVALID;
  131. } else
  132. {
  133. retval=bank->driver->protect(bank, set, first, last);
  134. }
  135. if (retval!=ERROR_OK)
  136. {
  137. ERROR("Failed protecting banks %d to %d", first, last);
  138. }
  139. return retval;
  140. }
  141. int flash_register_commands(struct command_context_s *cmd_ctx)
  142. {
  143. flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
  144. 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 ...]");
  145. register_command(cmd_ctx, flash_cmd, "auto_erase", handle_flash_auto_erase_command, COMMAND_ANY,
  146. "auto erase flash sectors <on|off>");
  147. return ERROR_OK;
  148. }
  149. int flash_init_drivers(struct command_context_s *cmd_ctx)
  150. {
  151. if (flash_banks)
  152. {
  153. register_command(cmd_ctx, flash_cmd, "banks", handle_flash_banks_command, COMMAND_EXEC,
  154. "list configured flash banks ");
  155. register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
  156. "print info about flash bank <num>");
  157. register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
  158. "identify flash bank <num>");
  159. register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
  160. "check erase state of sectors in flash bank <num>");
  161. register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
  162. "check protection state of sectors in flash bank <num>");
  163. register_command(cmd_ctx, flash_cmd, "erase_sector", handle_flash_erase_command, COMMAND_EXEC,
  164. "erase sectors at <bank> <first> <last>");
  165. register_command(cmd_ctx, flash_cmd, "erase_address", handle_flash_erase_address_command, COMMAND_EXEC,
  166. "erase address range <address> <length>");
  167. register_command(cmd_ctx, flash_cmd, "write_bank", handle_flash_write_bank_command, COMMAND_EXEC,
  168. "write binary data to <bank> <file> <offset>");
  169. register_command(cmd_ctx, flash_cmd, "write_image", handle_flash_write_image_command, COMMAND_EXEC,
  170. "write_image <file> [offset] [type]");
  171. register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
  172. "set protection of sectors at <bank> <first> <last> <on|off>");
  173. }
  174. return ERROR_OK;
  175. }
  176. flash_bank_t *get_flash_bank_by_num_noprobe(int num)
  177. {
  178. flash_bank_t *p;
  179. int i = 0;
  180. for (p = flash_banks; p; p = p->next)
  181. {
  182. if (i++ == num)
  183. {
  184. return p;
  185. }
  186. }
  187. ERROR("Flash bank %d does not exist", num);
  188. return NULL;
  189. }
  190. flash_bank_t *get_flash_bank_by_num(int num)
  191. {
  192. flash_bank_t *p = get_flash_bank_by_num_noprobe(num);
  193. int retval;
  194. if (p == NULL)
  195. return NULL;
  196. retval = p->driver->auto_probe(p);
  197. if (retval != ERROR_OK)
  198. {
  199. ERROR("auto_probe failed %d\n", retval);
  200. return NULL;
  201. }
  202. return p;
  203. }
  204. int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  205. {
  206. int i;
  207. int found = 0;
  208. target_t *target;
  209. if (argc < 6)
  210. {
  211. return ERROR_COMMAND_SYNTAX_ERROR;
  212. }
  213. if ((target = get_target_by_num(strtoul(args[5], NULL, 0))) == NULL)
  214. {
  215. ERROR("target %lu not defined", strtoul(args[5], NULL, 0));
  216. return ERROR_OK;
  217. }
  218. for (i = 0; flash_drivers[i]; i++)
  219. {
  220. if (strcmp(args[0], flash_drivers[i]->name) == 0)
  221. {
  222. flash_bank_t *p, *c;
  223. /* register flash specific commands */
  224. if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
  225. {
  226. ERROR("couldn't register '%s' commands", args[0]);
  227. exit(-1);
  228. }
  229. c = malloc(sizeof(flash_bank_t));
  230. c->target = target;
  231. c->driver = flash_drivers[i];
  232. c->driver_priv = NULL;
  233. c->base = strtoul(args[1], NULL, 0);
  234. c->size = strtoul(args[2], NULL, 0);
  235. c->chip_width = strtoul(args[3], NULL, 0);
  236. c->bus_width = strtoul(args[4], NULL, 0);
  237. c->num_sectors = 0;
  238. c->sectors = NULL;
  239. c->next = NULL;
  240. if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
  241. {
  242. ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
  243. free(c);
  244. return ERROR_OK;
  245. }
  246. /* put flash bank in linked list */
  247. if (flash_banks)
  248. {
  249. /* find last flash bank */
  250. for (p = flash_banks; p && p->next; p = p->next);
  251. if (p)
  252. p->next = c;
  253. }
  254. else
  255. {
  256. flash_banks = c;
  257. }
  258. found = 1;
  259. }
  260. }
  261. /* no matching flash driver found */
  262. if (!found)
  263. {
  264. ERROR("flash driver '%s' not found", args[0]);
  265. exit(-1);
  266. }
  267. return ERROR_OK;
  268. }
  269. int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  270. {
  271. flash_bank_t *p;
  272. int i = 0;
  273. if (!flash_banks)
  274. {
  275. command_print(cmd_ctx, "no flash banks configured");
  276. return ERROR_OK;
  277. }
  278. for (p = flash_banks; p; p = p->next)
  279. {
  280. command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
  281. i++, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
  282. }
  283. return ERROR_OK;
  284. }
  285. int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  286. {
  287. flash_bank_t *p;
  288. int i = 0;
  289. int j = 0;
  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. p->driver->auto_probe(p);
  301. command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
  302. i, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
  303. for (j = 0; j < p->num_sectors; j++)
  304. {
  305. char *erase_state, *protect_state;
  306. if (p->sectors[j].is_erased == 0)
  307. erase_state = "not erased";
  308. else if (p->sectors[j].is_erased == 1)
  309. erase_state = "erased";
  310. else
  311. erase_state = "erase state unknown";
  312. if (p->sectors[j].is_protected == 0)
  313. protect_state = "not protected";
  314. else if (p->sectors[j].is_protected == 1)
  315. protect_state = "protected";
  316. else
  317. protect_state = "protection state unknown";
  318. command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%x %ikB) %s, %s",
  319. j, p->sectors[j].offset, p->sectors[j].size, p->sectors[j].size>>10,
  320. erase_state, protect_state);
  321. }
  322. p->driver->info(p, buf, 1024);
  323. command_print(cmd_ctx, "%s", buf);
  324. }
  325. }
  326. return ERROR_OK;
  327. }
  328. int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  329. {
  330. flash_bank_t *p;
  331. int retval;
  332. if (argc != 1)
  333. {
  334. return ERROR_COMMAND_SYNTAX_ERROR;
  335. }
  336. p = get_flash_bank_by_num_noprobe(strtoul(args[0], NULL, 0));
  337. if (p)
  338. {
  339. if ((retval = p->driver->probe(p)) == ERROR_OK)
  340. {
  341. command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
  342. }
  343. else if (retval == ERROR_FLASH_BANK_INVALID)
  344. {
  345. command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
  346. args[0], p->base);
  347. }
  348. else
  349. {
  350. command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
  351. args[0], p->base);
  352. }
  353. }
  354. else
  355. {
  356. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  357. }
  358. return ERROR_OK;
  359. }
  360. int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  361. {
  362. flash_bank_t *p;
  363. int retval;
  364. if (argc != 1)
  365. {
  366. return ERROR_COMMAND_SYNTAX_ERROR;
  367. }
  368. p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  369. if (p)
  370. {
  371. if ((retval = p->driver->erase_check(p)) == ERROR_OK)
  372. {
  373. command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
  374. }
  375. else
  376. {
  377. command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
  378. args[0], p->base);
  379. }
  380. }
  381. return ERROR_OK;
  382. }
  383. int handle_flash_erase_address_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  384. {
  385. flash_bank_t *p;
  386. int retval;
  387. int address;
  388. int length;
  389. duration_t duration;
  390. char *duration_text;
  391. target_t *target = get_current_target(cmd_ctx);
  392. if (argc != 2)
  393. {
  394. return ERROR_COMMAND_SYNTAX_ERROR;
  395. }
  396. address = strtoul(args[0], NULL, 0);
  397. length = strtoul(args[1], NULL, 0);
  398. if (length <= 0)
  399. {
  400. command_print(cmd_ctx, "Length must be >0");
  401. return ERROR_COMMAND_SYNTAX_ERROR;
  402. }
  403. p = get_flash_bank_by_addr(target, address);
  404. if (p == NULL)
  405. {
  406. return ERROR_COMMAND_SYNTAX_ERROR;
  407. }
  408. /* We can't know if we did a resume + halt, in which case we no longer know the erased state */
  409. flash_set_dirty();
  410. duration_start_measure(&duration);
  411. if ((retval = flash_erase_address_range(target, address, length)) == ERROR_OK)
  412. {
  413. duration_stop_measure(&duration, &duration_text);
  414. command_print(cmd_ctx, "erased address 0x%8.8x length %i in %s", address, length, duration_text);
  415. free(duration_text);
  416. }
  417. return retval;
  418. }
  419. int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  420. {
  421. flash_bank_t *p;
  422. int retval;
  423. if (argc != 1)
  424. {
  425. return ERROR_COMMAND_SYNTAX_ERROR;
  426. }
  427. p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  428. if (p)
  429. {
  430. if ((retval = p->driver->protect_check(p)) == ERROR_OK)
  431. {
  432. command_print(cmd_ctx, "successfully checked protect state");
  433. }
  434. else if (retval == ERROR_FLASH_OPERATION_FAILED)
  435. {
  436. command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
  437. }
  438. else
  439. {
  440. command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
  441. }
  442. }
  443. else
  444. {
  445. return ERROR_COMMAND_SYNTAX_ERROR;
  446. }
  447. return ERROR_OK;
  448. }
  449. int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  450. {
  451. if (argc > 2)
  452. {
  453. int first = strtoul(args[1], NULL, 0);
  454. int last = strtoul(args[2], NULL, 0);
  455. int retval;
  456. flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  457. duration_t duration;
  458. char *duration_text;
  459. duration_start_measure(&duration);
  460. if (!p)
  461. {
  462. return ERROR_COMMAND_SYNTAX_ERROR;
  463. }
  464. if ((retval = flash_driver_erase(p, first, last)) == ERROR_OK)
  465. {
  466. duration_stop_measure(&duration, &duration_text);
  467. command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %s", first, last, strtoul(args[0], 0, 0), duration_text);
  468. free(duration_text);
  469. }
  470. }
  471. else
  472. {
  473. return ERROR_COMMAND_SYNTAX_ERROR;
  474. }
  475. return ERROR_OK;
  476. }
  477. int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  478. {
  479. if (argc > 3)
  480. {
  481. int first = strtoul(args[1], NULL, 0);
  482. int last = strtoul(args[2], NULL, 0);
  483. int set;
  484. int retval;
  485. flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  486. if (!p)
  487. {
  488. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  489. return ERROR_OK;
  490. }
  491. if (strcmp(args[3], "on") == 0)
  492. set = 1;
  493. else if (strcmp(args[3], "off") == 0)
  494. set = 0;
  495. else
  496. {
  497. return ERROR_COMMAND_SYNTAX_ERROR;
  498. }
  499. retval = flash_driver_protect(p, set, first, last);
  500. if (retval == ERROR_OK)
  501. {
  502. 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));
  503. }
  504. }
  505. else
  506. {
  507. return ERROR_COMMAND_SYNTAX_ERROR;
  508. }
  509. return ERROR_OK;
  510. }
  511. int handle_flash_write_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  512. {
  513. target_t *target = get_current_target(cmd_ctx);
  514. image_t image;
  515. u32 written;
  516. duration_t duration;
  517. char *duration_text;
  518. int retval;
  519. if (argc < 1)
  520. {
  521. return ERROR_COMMAND_SYNTAX_ERROR;
  522. }
  523. if (!target)
  524. {
  525. ERROR("no target selected");
  526. return ERROR_OK;
  527. }
  528. duration_start_measure(&duration);
  529. if (argc >= 2)
  530. {
  531. image.base_address_set = 1;
  532. image.base_address = strtoul(args[1], NULL, 0);
  533. }
  534. else
  535. {
  536. image.base_address_set = 0;
  537. image.base_address = 0x0;
  538. }
  539. image.start_address_set = 0;
  540. retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL);
  541. if (retval != ERROR_OK)
  542. {
  543. command_print(cmd_ctx, "image_open error: %s", image.error_str);
  544. return retval;
  545. }
  546. retval = flash_write(target, &image, &written, auto_erase);
  547. if (retval != ERROR_OK)
  548. {
  549. image_close(&image);
  550. return retval;
  551. }
  552. duration_stop_measure(&duration, &duration_text);
  553. if (retval == ERROR_OK)
  554. {
  555. command_print(cmd_ctx, "wrote %u byte from file %s in %s (%f kb/s)",
  556. written, args[0], duration_text,
  557. (float)written / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
  558. }
  559. free(duration_text);
  560. image_close(&image);
  561. return retval;
  562. }
  563. int handle_flash_write_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  564. {
  565. u32 offset;
  566. u8 *buffer;
  567. u32 buf_cnt;
  568. fileio_t fileio;
  569. duration_t duration;
  570. char *duration_text;
  571. int retval;
  572. flash_bank_t *p;
  573. if (argc != 3)
  574. {
  575. return ERROR_COMMAND_SYNTAX_ERROR;
  576. }
  577. duration_start_measure(&duration);
  578. offset = strtoul(args[2], NULL, 0);
  579. p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  580. if (!p)
  581. {
  582. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  583. return ERROR_OK;
  584. }
  585. if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
  586. {
  587. command_print(cmd_ctx, "flash write_binary error: %s", fileio.error_str);
  588. return ERROR_OK;
  589. }
  590. buffer = malloc(fileio.size);
  591. if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
  592. {
  593. command_print(cmd_ctx, "flash write_binary error: %s", fileio.error_str);
  594. return ERROR_OK;
  595. }
  596. retval = flash_driver_write(p, buffer, offset, buf_cnt);
  597. free(buffer);
  598. duration_stop_measure(&duration, &duration_text);
  599. if (retval!=ERROR_OK)
  600. {
  601. command_print(cmd_ctx, "wrote %"PRIi64" byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
  602. fileio.size, args[1], strtoul(args[0], NULL, 0), offset, duration_text,
  603. (float)fileio.size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
  604. }
  605. free(duration_text);
  606. fileio_close(&fileio);
  607. return retval;
  608. }
  609. void flash_set_dirty(void)
  610. {
  611. flash_bank_t *c;
  612. int i;
  613. /* set all flash to require erasing */
  614. for (c = flash_banks; c; c = c->next)
  615. {
  616. for (i = 0; i < c->num_sectors; i++)
  617. {
  618. c->sectors[i].is_erased = 0;
  619. }
  620. }
  621. }
  622. /* lookup flash bank by address */
  623. flash_bank_t *get_flash_bank_by_addr(target_t *target, u32 addr)
  624. {
  625. flash_bank_t *c;
  626. /* cycle through bank list */
  627. for (c = flash_banks; c; c = c->next)
  628. {
  629. int retval;
  630. retval = c->driver->auto_probe(c);
  631. if (retval != ERROR_OK)
  632. {
  633. ERROR("auto_probe failed %d\n", retval);
  634. return NULL;
  635. }
  636. /* check whether address belongs to this flash bank */
  637. if ((addr >= c->base) && (addr < c->base + c->size) && target == c->target)
  638. return c;
  639. }
  640. ERROR("No flash at address 0x%08x\n", addr);
  641. return NULL;
  642. }
  643. /* erase given flash region, selects proper bank according to target and address */
  644. int flash_erase_address_range(target_t *target, u32 addr, u32 length)
  645. {
  646. flash_bank_t *c;
  647. int first = -1;
  648. int last = -1;
  649. int i;
  650. if ((c = get_flash_bank_by_addr(target, addr)) == NULL)
  651. return ERROR_FLASH_DST_OUT_OF_BANK; /* no corresponding bank found */
  652. if (c->size == 0 || c->num_sectors == 0)
  653. return ERROR_FLASH_BANK_INVALID;
  654. if (length == 0)
  655. {
  656. /* special case, erase whole bank when length is zero */
  657. if (addr != c->base)
  658. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  659. return flash_driver_erase(c, 0, c->num_sectors - 1);
  660. }
  661. /* check whether it fits */
  662. if (addr + length > c->base + c->size)
  663. return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
  664. addr -= c->base;
  665. for (i = 0; i < c->num_sectors; i++)
  666. {
  667. /* check whether sector overlaps with the given range and is not yet erased */
  668. if (addr < c->sectors[i].offset + c->sectors[i].size && addr + length > c->sectors[i].offset && c->sectors[i].is_erased != 1) {
  669. /* if first is not set yet then this is the first sector */
  670. if (first == -1)
  671. first = i;
  672. last = i; /* and it is the last one so far in any case */
  673. }
  674. }
  675. if( first == -1 || last == -1 )
  676. return ERROR_OK;
  677. return flash_driver_erase(c, first, last);
  678. }
  679. /* write (optional verify) an image to flash memory of the given target */
  680. int flash_write(target_t *target, image_t *image, u32 *written, int erase)
  681. {
  682. int retval;
  683. int section;
  684. u32 section_offset;
  685. flash_bank_t *c;
  686. section = 0;
  687. section_offset = 0;
  688. if (written)
  689. *written = 0;
  690. if (erase)
  691. {
  692. /* assume all sectors need erasing - stops any problems
  693. * when flash_write is called multiple times */
  694. flash_set_dirty();
  695. }
  696. /* loop until we reach end of the image */
  697. while (section < image->num_sections)
  698. {
  699. u32 buffer_size;
  700. u8 *buffer;
  701. int section_first;
  702. int section_last;
  703. u32 run_address = image->sections[section].base_address + section_offset;
  704. u32 run_size = image->sections[section].size - section_offset;
  705. if (image->sections[section].size == 0)
  706. {
  707. WARNING("empty section %d", section);
  708. section++;
  709. section_offset = 0;
  710. continue;
  711. }
  712. /* find the corresponding flash bank */
  713. if ((c = get_flash_bank_by_addr(target, run_address)) == NULL)
  714. {
  715. section++; /* and skip it */
  716. section_offset = 0;
  717. continue;
  718. }
  719. /* collect consecutive sections which fall into the same bank */
  720. section_first = section;
  721. section_last = section;
  722. while ((run_address + run_size < c->base + c->size)
  723. && (section_last + 1 < image->num_sections))
  724. {
  725. if (image->sections[section_last + 1].base_address < (run_address + run_size))
  726. {
  727. DEBUG("section %d out of order(very slightly surprising, but supported)", section_last + 1);
  728. break;
  729. }
  730. if (image->sections[section_last + 1].base_address != (run_address + run_size))
  731. break;
  732. run_size += image->sections[++section_last].size;
  733. }
  734. /* fit the run into bank constraints */
  735. if (run_address + run_size > c->base + c->size)
  736. run_size = c->base + c->size - run_address;
  737. /* allocate buffer */
  738. buffer = malloc(run_size);
  739. buffer_size = 0;
  740. /* read sections to the buffer */
  741. while (buffer_size < run_size)
  742. {
  743. u32 size_read;
  744. if (buffer_size - run_size <= image->sections[section].size - section_offset)
  745. size_read = buffer_size - run_size;
  746. else
  747. size_read = image->sections[section].size - section_offset;
  748. if ((retval = image_read_section(image, section, section_offset,
  749. size_read, buffer + buffer_size, &size_read)) != ERROR_OK || size_read == 0)
  750. {
  751. free(buffer);
  752. return retval;
  753. }
  754. buffer_size += size_read;
  755. section_offset += size_read;
  756. if (section_offset >= image->sections[section].size)
  757. {
  758. section++;
  759. section_offset = 0;
  760. }
  761. }
  762. retval = ERROR_OK;
  763. if (erase)
  764. {
  765. /* calculate and erase sectors */
  766. retval = flash_erase_address_range( target, run_address, run_size );
  767. }
  768. if (retval == ERROR_OK)
  769. {
  770. /* write flash sectors */
  771. retval = flash_driver_write(c, buffer, run_address - c->base, run_size);
  772. }
  773. free(buffer);
  774. if (retval != ERROR_OK)
  775. {
  776. return retval; /* abort operation */
  777. }
  778. if (written != NULL)
  779. *written += run_size; /* add run size to total written counter */
  780. }
  781. return ERROR_OK;
  782. }
  783. int handle_flash_auto_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  784. {
  785. if (argc != 1)
  786. {
  787. return ERROR_COMMAND_SYNTAX_ERROR;
  788. }
  789. if (strcmp(args[0], "on") == 0)
  790. auto_erase = 1;
  791. else if (strcmp(args[0], "off") == 0)
  792. auto_erase = 0;
  793. else
  794. return ERROR_COMMAND_SYNTAX_ERROR;
  795. return ERROR_OK;
  796. }