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.
 
 
 
 
 
 

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