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.
 
 
 
 
 
 

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