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.
 
 
 
 
 
 

1329 lines
33 KiB

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