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.
 
 
 
 
 
 

600 lines
17 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 <string.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <errno.h>
  33. #include <fileio.h>
  34. #include <image.h>
  35. #include "log.h"
  36. /* command handlers */
  37. int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  38. int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  39. int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  40. int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  41. int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  42. int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  43. int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  44. int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  45. int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  46. /* flash drivers
  47. */
  48. extern flash_driver_t lpc2000_flash;
  49. extern flash_driver_t cfi_flash;
  50. extern flash_driver_t at91sam7_flash;
  51. extern flash_driver_t str7x_flash;
  52. extern flash_driver_t str9x_flash;
  53. extern flash_driver_t stellaris_flash;
  54. extern flash_driver_t str9xpec_flash;
  55. extern flash_driver_t stm32x_flash;
  56. flash_driver_t *flash_drivers[] =
  57. {
  58. &lpc2000_flash,
  59. &cfi_flash,
  60. &at91sam7_flash,
  61. &str7x_flash,
  62. &str9x_flash,
  63. &stellaris_flash,
  64. &str9xpec_flash,
  65. &stm32x_flash,
  66. NULL,
  67. };
  68. flash_bank_t *flash_banks;
  69. static command_t *flash_cmd;
  70. int flash_register_commands(struct command_context_s *cmd_ctx)
  71. {
  72. flash_cmd = register_command(cmd_ctx, NULL, "flash", NULL, COMMAND_ANY, NULL);
  73. register_command(cmd_ctx, flash_cmd, "bank", handle_flash_bank_command, COMMAND_CONFIG, NULL);
  74. return ERROR_OK;
  75. }
  76. int flash_init(struct command_context_s *cmd_ctx)
  77. {
  78. if (flash_banks)
  79. {
  80. register_command(cmd_ctx, flash_cmd, "banks", handle_flash_banks_command, COMMAND_EXEC,
  81. "list configured flash banks ");
  82. register_command(cmd_ctx, flash_cmd, "info", handle_flash_info_command, COMMAND_EXEC,
  83. "print info about flash bank <num>");
  84. register_command(cmd_ctx, flash_cmd, "probe", handle_flash_probe_command, COMMAND_EXEC,
  85. "identify flash bank <num>");
  86. register_command(cmd_ctx, flash_cmd, "erase_check", handle_flash_erase_check_command, COMMAND_EXEC,
  87. "check erase state of sectors in flash bank <num>");
  88. register_command(cmd_ctx, flash_cmd, "protect_check", handle_flash_protect_check_command, COMMAND_EXEC,
  89. "check protection state of sectors in flash bank <num>");
  90. register_command(cmd_ctx, flash_cmd, "erase", handle_flash_erase_command, COMMAND_EXEC,
  91. "erase sectors at <bank> <first> <last>");
  92. register_command(cmd_ctx, flash_cmd, "write", handle_flash_write_command, COMMAND_EXEC,
  93. "write binary <bank> <file> <offset>");
  94. register_command(cmd_ctx, flash_cmd, "protect", handle_flash_protect_command, COMMAND_EXEC,
  95. "set protection of sectors at <bank> <first> <last> <on|off>");
  96. }
  97. return ERROR_OK;
  98. }
  99. flash_bank_t *get_flash_bank_by_num(int num)
  100. {
  101. flash_bank_t *p;
  102. int i = 0;
  103. for (p = flash_banks; p; p = p->next)
  104. {
  105. if (i++ == num)
  106. {
  107. return p;
  108. }
  109. }
  110. return NULL;
  111. }
  112. /* flash_bank <driver> <base> <size> <chip_width> <bus_width> [driver_options ...]
  113. */
  114. int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  115. {
  116. int i;
  117. int found = 0;
  118. if (argc < 5)
  119. {
  120. WARNING("incomplete flash_bank configuration");
  121. return ERROR_OK;
  122. }
  123. for (i = 0; flash_drivers[i]; i++)
  124. {
  125. if (strcmp(args[0], flash_drivers[i]->name) == 0)
  126. {
  127. flash_bank_t *p, *c;
  128. /* register flash specific commands */
  129. if (flash_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
  130. {
  131. ERROR("couldn't register '%s' commands", args[0]);
  132. exit(-1);
  133. }
  134. c = malloc(sizeof(flash_bank_t));
  135. c->driver = flash_drivers[i];
  136. c->driver_priv = NULL;
  137. c->base = strtoul(args[1], NULL, 0);
  138. c->size = strtoul(args[2], NULL, 0);
  139. c->chip_width = strtoul(args[3], NULL, 0);
  140. c->bus_width = strtoul(args[4], NULL, 0);
  141. c->next = NULL;
  142. if (flash_drivers[i]->flash_bank_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
  143. {
  144. ERROR("'%s' driver rejected flash bank at 0x%8.8x", args[0], c->base);
  145. free(c);
  146. return ERROR_OK;
  147. }
  148. /* put flash bank in linked list */
  149. if (flash_banks)
  150. {
  151. /* find last flash bank */
  152. for (p = flash_banks; p && p->next; p = p->next);
  153. if (p)
  154. p->next = c;
  155. }
  156. else
  157. {
  158. flash_banks = c;
  159. }
  160. found = 1;
  161. }
  162. }
  163. /* no matching flash driver found */
  164. if (!found)
  165. {
  166. ERROR("flash driver '%s' not found", args[0]);
  167. exit(-1);
  168. }
  169. return ERROR_OK;
  170. }
  171. int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  172. {
  173. flash_bank_t *p;
  174. int i = 0;
  175. if (!flash_banks)
  176. {
  177. command_print(cmd_ctx, "no flash banks configured");
  178. return ERROR_OK;
  179. }
  180. for (p = flash_banks; p; p = p->next)
  181. {
  182. command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
  183. i++, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
  184. }
  185. return ERROR_OK;
  186. }
  187. int handle_flash_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  188. {
  189. flash_bank_t *p;
  190. int i = 0;
  191. int j = 0;
  192. if (argc != 1)
  193. {
  194. command_print(cmd_ctx, "usage: flash info <num>");
  195. return ERROR_OK;
  196. }
  197. for (p = flash_banks; p; p = p->next)
  198. {
  199. if (i++ == strtoul(args[0], NULL, 0))
  200. {
  201. char buf[1024];
  202. command_print(cmd_ctx, "#%i: %s at 0x%8.8x, size 0x%8.8x, buswidth %i, chipwidth %i",
  203. i, p->driver->name, p->base, p->size, p->bus_width, p->chip_width);
  204. for (j = 0; j < p->num_sectors; j++)
  205. {
  206. char *erase_state, *protect_state;
  207. if (p->sectors[j].is_erased == 0)
  208. erase_state = "not erased";
  209. else if (p->sectors[j].is_erased == 1)
  210. erase_state = "erased";
  211. else
  212. erase_state = "erase state unknown";
  213. if (p->sectors[j].is_protected == 0)
  214. protect_state = "not protected";
  215. else if (p->sectors[j].is_protected == 1)
  216. protect_state = "protected";
  217. else
  218. protect_state = "protection state unknown";
  219. command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%xkB) %s, %s",
  220. j, p->sectors[j].offset, p->sectors[j].size,
  221. erase_state, protect_state);
  222. }
  223. p->driver->info(p, buf, 1024);
  224. command_print(cmd_ctx, "%s", buf);
  225. }
  226. }
  227. return ERROR_OK;
  228. }
  229. int handle_flash_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  230. {
  231. flash_bank_t *p;
  232. int retval;
  233. if (argc != 1)
  234. {
  235. command_print(cmd_ctx, "usage: flash probe <num>");
  236. return ERROR_OK;
  237. }
  238. p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  239. if (p)
  240. {
  241. if ((retval = p->driver->probe(p)) == ERROR_OK)
  242. {
  243. command_print(cmd_ctx, "flash '%s' found at 0x%8.8x", p->driver->name, p->base);
  244. }
  245. else if (retval == ERROR_FLASH_BANK_INVALID)
  246. {
  247. command_print(cmd_ctx, "probing failed for flash bank '#%s' at 0x%8.8x",
  248. args[0], p->base);
  249. }
  250. else
  251. {
  252. command_print(cmd_ctx, "unknown error when probing flash bank '#%s' at 0x%8.8x",
  253. args[0], p->base);
  254. }
  255. }
  256. else
  257. {
  258. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  259. }
  260. return ERROR_OK;
  261. }
  262. int handle_flash_erase_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  263. {
  264. flash_bank_t *p;
  265. int retval;
  266. if (argc != 1)
  267. {
  268. command_print(cmd_ctx, "usage: flash erase_check <num>");
  269. return ERROR_OK;
  270. }
  271. p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  272. if (p)
  273. {
  274. if ((retval = p->driver->erase_check(p)) == ERROR_OK)
  275. {
  276. command_print(cmd_ctx, "successfully checked erase state", p->driver->name, p->base);
  277. }
  278. else
  279. {
  280. command_print(cmd_ctx, "unknown error when checking erase state of flash bank #%s at 0x%8.8x",
  281. args[0], p->base);
  282. }
  283. }
  284. else
  285. {
  286. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  287. }
  288. return ERROR_OK;
  289. }
  290. int handle_flash_protect_check_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  291. {
  292. flash_bank_t *p;
  293. int retval;
  294. if (argc != 1)
  295. {
  296. command_print(cmd_ctx, "usage: flash protect_check <num>");
  297. return ERROR_OK;
  298. }
  299. p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  300. if (p)
  301. {
  302. if ((retval = p->driver->protect_check(p)) == ERROR_OK)
  303. {
  304. command_print(cmd_ctx, "successfully checked protect state");
  305. }
  306. else if (retval == ERROR_FLASH_OPERATION_FAILED)
  307. {
  308. command_print(cmd_ctx, "checking protection state failed (possibly unsupported) by flash #%s at 0x%8.8x", args[0], p->base);
  309. }
  310. else
  311. {
  312. command_print(cmd_ctx, "unknown error when checking protection state of flash bank '#%s' at 0x%8.8x", args[0], p->base);
  313. }
  314. }
  315. else
  316. {
  317. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  318. }
  319. return ERROR_OK;
  320. }
  321. int handle_flash_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  322. {
  323. if (argc > 2)
  324. {
  325. int first = strtoul(args[1], NULL, 0);
  326. int last = strtoul(args[2], NULL, 0);
  327. int retval;
  328. flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  329. duration_t duration;
  330. char *duration_text;
  331. duration_start_measure(&duration);
  332. if (!p)
  333. {
  334. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  335. return ERROR_OK;
  336. }
  337. if ((retval = p->driver->erase(p, first, last)) != ERROR_OK)
  338. {
  339. switch (retval)
  340. {
  341. case ERROR_TARGET_NOT_HALTED:
  342. command_print(cmd_ctx, "can't work with this flash while target is running");
  343. break;
  344. case ERROR_INVALID_ARGUMENTS:
  345. command_print(cmd_ctx, "usage: flash_erase <bank> <first> <last>");
  346. break;
  347. case ERROR_FLASH_BANK_INVALID:
  348. command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
  349. break;
  350. case ERROR_FLASH_OPERATION_FAILED:
  351. command_print(cmd_ctx, "flash erase error");
  352. break;
  353. case ERROR_FLASH_SECTOR_INVALID:
  354. command_print(cmd_ctx, "sector number(s) invalid");
  355. break;
  356. case ERROR_OK:
  357. command_print(cmd_ctx, "erased flash sectors %i to %i", first, last);
  358. break;
  359. default:
  360. command_print(cmd_ctx, "unknown error");
  361. }
  362. }
  363. else
  364. {
  365. duration_stop_measure(&duration, &duration_text);
  366. command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %s", first, last, strtoul(args[0], 0, 0), duration_text);
  367. free(duration_text);
  368. }
  369. }
  370. else
  371. {
  372. command_print(cmd_ctx, "usage: flash erase <bank> <first> <last>");
  373. }
  374. return ERROR_OK;
  375. }
  376. int handle_flash_protect_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  377. {
  378. if (argc > 3)
  379. {
  380. int first = strtoul(args[1], NULL, 0);
  381. int last = strtoul(args[2], NULL, 0);
  382. int set;
  383. int retval;
  384. flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  385. if (!p)
  386. {
  387. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  388. return ERROR_OK;
  389. }
  390. if (strcmp(args[3], "on") == 0)
  391. set = 1;
  392. else if (strcmp(args[3], "off") == 0)
  393. set = 0;
  394. else
  395. {
  396. command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
  397. return ERROR_OK;
  398. }
  399. if ((retval = p->driver->protect(p, set, first, last)) != ERROR_OK)
  400. {
  401. switch (retval)
  402. {
  403. case ERROR_TARGET_NOT_HALTED:
  404. command_print(cmd_ctx, "can't work with this flash while target is running");
  405. break;
  406. case ERROR_INVALID_ARGUMENTS:
  407. command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
  408. break;
  409. case ERROR_FLASH_BANK_INVALID:
  410. command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
  411. break;
  412. case ERROR_FLASH_OPERATION_FAILED:
  413. command_print(cmd_ctx, "flash program error");
  414. break;
  415. case ERROR_FLASH_SECTOR_INVALID:
  416. command_print(cmd_ctx, "sector number(s) invalid");
  417. break;
  418. case ERROR_OK:
  419. command_print(cmd_ctx, "protection of flash sectors %i to %i turned %s", first, last, args[3]);
  420. break;
  421. default:
  422. command_print(cmd_ctx, "unknown error");
  423. }
  424. }
  425. else
  426. {
  427. 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));
  428. }
  429. }
  430. else
  431. {
  432. command_print(cmd_ctx, "usage: flash protect <bank> <first> <last> <on|off>");
  433. }
  434. return ERROR_OK;
  435. }
  436. int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  437. {
  438. u32 offset;
  439. u8 *buffer;
  440. u32 buf_cnt;
  441. u32 image_size;
  442. int i;
  443. image_t image;
  444. duration_t duration;
  445. char *duration_text;
  446. int retval;
  447. flash_bank_t *p;
  448. if (argc < 3)
  449. {
  450. command_print(cmd_ctx, "usage: flash write <bank> <file> <offset> [type]");
  451. return ERROR_OK;
  452. }
  453. duration_start_measure(&duration);
  454. image.base_address_set = 1;
  455. image.base_address = strtoul(args[1], NULL, 0);
  456. image.start_address_set = 0;
  457. offset = strtoul(args[2], NULL, 0);
  458. p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
  459. if (!p)
  460. {
  461. command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
  462. return ERROR_OK;
  463. }
  464. if (image_open(&image, args[1], (argc == 4) ? args[3] : NULL) != ERROR_OK)
  465. {
  466. command_print(cmd_ctx, "flash write error: %s", image.error_str);
  467. return ERROR_OK;
  468. }
  469. image_size = 0x0;
  470. for (i = 0; i < image.num_sections; i++)
  471. {
  472. buffer = malloc(image.sections[i].size);
  473. if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
  474. {
  475. ERROR("image_read_section failed with error code: %i", retval);
  476. command_print(cmd_ctx, "image reading failed, flash write aborted");
  477. free(buffer);
  478. image_close(&image);
  479. return ERROR_OK;
  480. }
  481. if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
  482. {
  483. command_print(cmd_ctx, "failed writing file %s to flash bank %i at offset 0x%8.8x",
  484. args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
  485. switch (retval)
  486. {
  487. case ERROR_TARGET_NOT_HALTED:
  488. command_print(cmd_ctx, "can't work with this flash while target is running");
  489. break;
  490. case ERROR_INVALID_ARGUMENTS:
  491. command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
  492. break;
  493. case ERROR_FLASH_BANK_INVALID:
  494. command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
  495. break;
  496. case ERROR_FLASH_OPERATION_FAILED:
  497. command_print(cmd_ctx, "flash program error");
  498. break;
  499. case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
  500. command_print(cmd_ctx, "offset breaks required alignment");
  501. break;
  502. case ERROR_FLASH_DST_OUT_OF_BANK:
  503. command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
  504. break;
  505. case ERROR_FLASH_SECTOR_NOT_ERASED:
  506. command_print(cmd_ctx, "destination sector(s) not erased");
  507. break;
  508. default:
  509. command_print(cmd_ctx, "unknown error");
  510. }
  511. }
  512. image_size += buf_cnt;
  513. free(buffer);
  514. }
  515. duration_stop_measure(&duration, &duration_text);
  516. command_print(cmd_ctx, "wrote %u byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
  517. image_size, args[1], strtoul(args[0], NULL, 0), offset, duration_text,
  518. (float)image_size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
  519. free(duration_text);
  520. image_close(&image);
  521. return ERROR_OK;
  522. }