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.
 
 
 
 
 
 

4206 lines
103 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, Duane Ellis *
  9. * openocd@duaneeellis.com *
  10. * *
  11. * Copyright (C) 2008 by Spencer Oliver *
  12. * spen@spen-soft.co.uk *
  13. * *
  14. * Copyright (C) 2008 by Rick Altherr *
  15. * kc8apf@kc8apf.net> *
  16. * *
  17. * This program is free software; you can redistribute it and/or modify *
  18. * it under the terms of the GNU General Public License as published by *
  19. * the Free Software Foundation; either version 2 of the License, or *
  20. * (at your option) any later version. *
  21. * *
  22. * This program is distributed in the hope that it will be useful, *
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  25. * GNU General Public License for more details. *
  26. * *
  27. * You should have received a copy of the GNU General Public License *
  28. * along with this program; if not, write to the *
  29. * Free Software Foundation, Inc., *
  30. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  31. ***************************************************************************/
  32. #ifdef HAVE_CONFIG_H
  33. #include "config.h"
  34. #endif
  35. #include "replacements.h"
  36. #include "target.h"
  37. #include "target_request.h"
  38. #include "log.h"
  39. #include "configuration.h"
  40. #include "binarybuffer.h"
  41. #include "jtag.h"
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include <inttypes.h>
  45. #include <sys/types.h>
  46. #include <sys/stat.h>
  47. #include <unistd.h>
  48. #include <errno.h>
  49. #include <sys/time.h>
  50. #include <time.h>
  51. #include <time_support.h>
  52. #include <fileio.h>
  53. #include <image.h>
  54. int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv);
  55. int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  56. int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  57. int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  58. int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  59. int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  60. int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  61. int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  62. int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  63. int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  64. int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  65. int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  66. int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  67. int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  68. int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  69. int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  70. int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  71. int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  72. int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  73. int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc);
  74. int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  75. int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  76. int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  77. static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
  78. static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
  79. static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv);
  80. static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
  81. static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
  82. /* targets */
  83. extern target_type_t arm7tdmi_target;
  84. extern target_type_t arm720t_target;
  85. extern target_type_t arm9tdmi_target;
  86. extern target_type_t arm920t_target;
  87. extern target_type_t arm966e_target;
  88. extern target_type_t arm926ejs_target;
  89. extern target_type_t feroceon_target;
  90. extern target_type_t xscale_target;
  91. extern target_type_t cortexm3_target;
  92. extern target_type_t arm11_target;
  93. extern target_type_t mips_m4k_target;
  94. target_type_t *target_types[] =
  95. {
  96. &arm7tdmi_target,
  97. &arm9tdmi_target,
  98. &arm920t_target,
  99. &arm720t_target,
  100. &arm966e_target,
  101. &arm926ejs_target,
  102. &feroceon_target,
  103. &xscale_target,
  104. &cortexm3_target,
  105. &arm11_target,
  106. &mips_m4k_target,
  107. NULL,
  108. };
  109. target_t *all_targets = NULL;
  110. target_event_callback_t *target_event_callbacks = NULL;
  111. target_timer_callback_t *target_timer_callbacks = NULL;
  112. const Jim_Nvp nvp_assert[] = {
  113. { .name = "assert", NVP_ASSERT },
  114. { .name = "deassert", NVP_DEASSERT },
  115. { .name = "T", NVP_ASSERT },
  116. { .name = "F", NVP_DEASSERT },
  117. { .name = "t", NVP_ASSERT },
  118. { .name = "f", NVP_DEASSERT },
  119. { .name = NULL, .value = -1 }
  120. };
  121. const Jim_Nvp nvp_error_target[] = {
  122. { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
  123. { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
  124. { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
  125. { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
  126. { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
  127. { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
  128. { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
  129. { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
  130. { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
  131. { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
  132. { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
  133. { .value = -1, .name = NULL }
  134. };
  135. const char *target_strerror_safe( int err )
  136. {
  137. const Jim_Nvp *n;
  138. n = Jim_Nvp_value2name_simple( nvp_error_target, err );
  139. if( n->name == NULL ){
  140. return "unknown";
  141. } else {
  142. return n->name;
  143. }
  144. }
  145. const Jim_Nvp nvp_target_event[] = {
  146. { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
  147. { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
  148. { .value = TARGET_EVENT_EARLY_HALTED, .name = "early-halted" },
  149. { .value = TARGET_EVENT_HALTED, .name = "halted" },
  150. { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
  151. { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
  152. { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
  153. { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
  154. { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
  155. /* historical name */
  156. { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
  157. { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
  158. { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
  159. { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
  160. { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
  161. { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
  162. { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
  163. { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
  164. { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
  165. { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
  166. { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
  167. { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
  168. { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
  169. { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
  170. { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
  171. { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
  172. { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
  173. { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
  174. { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
  175. { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
  176. { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
  177. { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
  178. { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
  179. { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
  180. { .name = NULL, .value = -1 }
  181. };
  182. const Jim_Nvp nvp_target_state[] = {
  183. { .name = "unknown", .value = TARGET_UNKNOWN },
  184. { .name = "running", .value = TARGET_RUNNING },
  185. { .name = "halted", .value = TARGET_HALTED },
  186. { .name = "reset", .value = TARGET_RESET },
  187. { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
  188. { .name = NULL, .value = -1 },
  189. };
  190. const Jim_Nvp nvp_target_debug_reason [] = {
  191. { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
  192. { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
  193. { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
  194. { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
  195. { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
  196. { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
  197. { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
  198. { .name = NULL, .value = -1 },
  199. };
  200. const Jim_Nvp nvp_target_endian[] = {
  201. { .name = "big", .value = TARGET_BIG_ENDIAN },
  202. { .name = "little", .value = TARGET_LITTLE_ENDIAN },
  203. { .name = "be", .value = TARGET_BIG_ENDIAN },
  204. { .name = "le", .value = TARGET_LITTLE_ENDIAN },
  205. { .name = NULL, .value = -1 },
  206. };
  207. const Jim_Nvp nvp_reset_modes[] = {
  208. { .name = "unknown", .value = RESET_UNKNOWN },
  209. { .name = "run" , .value = RESET_RUN },
  210. { .name = "halt" , .value = RESET_HALT },
  211. { .name = "init" , .value = RESET_INIT },
  212. { .name = NULL , .value = -1 },
  213. };
  214. static int max_target_number(void)
  215. {
  216. target_t *t;
  217. int x;
  218. x = -1;
  219. t = all_targets;
  220. while( t ){
  221. if( x < t->target_number ){
  222. x = (t->target_number)+1;
  223. }
  224. t = t->next;
  225. }
  226. return x;
  227. }
  228. /* determine the number of the new target */
  229. static int new_target_number(void)
  230. {
  231. target_t *t;
  232. int x;
  233. /* number is 0 based */
  234. x = -1;
  235. t = all_targets;
  236. while(t){
  237. if( x < t->target_number ){
  238. x = t->target_number;
  239. }
  240. t = t->next;
  241. }
  242. return x+1;
  243. }
  244. static int target_continous_poll = 1;
  245. /* read a u32 from a buffer in target memory endianness */
  246. u32 target_buffer_get_u32(target_t *target, u8 *buffer)
  247. {
  248. if (target->endianness == TARGET_LITTLE_ENDIAN)
  249. return le_to_h_u32(buffer);
  250. else
  251. return be_to_h_u32(buffer);
  252. }
  253. /* read a u16 from a buffer in target memory endianness */
  254. u16 target_buffer_get_u16(target_t *target, u8 *buffer)
  255. {
  256. if (target->endianness == TARGET_LITTLE_ENDIAN)
  257. return le_to_h_u16(buffer);
  258. else
  259. return be_to_h_u16(buffer);
  260. }
  261. /* read a u8 from a buffer in target memory endianness */
  262. u8 target_buffer_get_u8(target_t *target, u8 *buffer)
  263. {
  264. return *buffer & 0x0ff;
  265. }
  266. /* write a u32 to a buffer in target memory endianness */
  267. void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
  268. {
  269. if (target->endianness == TARGET_LITTLE_ENDIAN)
  270. h_u32_to_le(buffer, value);
  271. else
  272. h_u32_to_be(buffer, value);
  273. }
  274. /* write a u16 to a buffer in target memory endianness */
  275. void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
  276. {
  277. if (target->endianness == TARGET_LITTLE_ENDIAN)
  278. h_u16_to_le(buffer, value);
  279. else
  280. h_u16_to_be(buffer, value);
  281. }
  282. /* write a u8 to a buffer in target memory endianness */
  283. void target_buffer_set_u8(target_t *target, u8 *buffer, u8 value)
  284. {
  285. *buffer = value;
  286. }
  287. /* returns a pointer to the n-th configured target */
  288. target_t* get_target_by_num(int num)
  289. {
  290. target_t *target = all_targets;
  291. while (target){
  292. if( target->target_number == num ){
  293. return target;
  294. }
  295. target = target->next;
  296. }
  297. return NULL;
  298. }
  299. int get_num_by_target(target_t *query_target)
  300. {
  301. return query_target->target_number;
  302. }
  303. target_t* get_current_target(command_context_t *cmd_ctx)
  304. {
  305. target_t *target = get_target_by_num(cmd_ctx->current_target);
  306. if (target == NULL)
  307. {
  308. LOG_ERROR("BUG: current_target out of bounds");
  309. exit(-1);
  310. }
  311. return target;
  312. }
  313. int target_poll(struct target_s *target)
  314. {
  315. /* We can't poll until after examine */
  316. if (!target->type->examined)
  317. {
  318. /* Fail silently lest we pollute the log */
  319. return ERROR_FAIL;
  320. }
  321. return target->type->poll(target);
  322. }
  323. int target_halt(struct target_s *target)
  324. {
  325. /* We can't poll until after examine */
  326. if (!target->type->examined)
  327. {
  328. LOG_ERROR("Target not examined yet");
  329. return ERROR_FAIL;
  330. }
  331. return target->type->halt(target);
  332. }
  333. int target_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
  334. {
  335. int retval;
  336. /* We can't poll until after examine */
  337. if (!target->type->examined)
  338. {
  339. LOG_ERROR("Target not examined yet");
  340. return ERROR_FAIL;
  341. }
  342. /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
  343. * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
  344. * the application.
  345. */
  346. if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
  347. return retval;
  348. return retval;
  349. }
  350. int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
  351. {
  352. char buf[100];
  353. int retval;
  354. Jim_Nvp *n;
  355. n = Jim_Nvp_value2name_simple( nvp_reset_modes, reset_mode );
  356. if( n->name == NULL ){
  357. LOG_ERROR("invalid reset mode");
  358. return ERROR_FAIL;
  359. }
  360. sprintf( buf, "ocd_process_reset %s", n->name );
  361. retval = Jim_Eval( interp, buf );
  362. if(retval != JIM_OK) {
  363. Jim_PrintErrorMessage(interp);
  364. return ERROR_FAIL;
  365. }
  366. /* We want any events to be processed before the prompt */
  367. retval = target_call_timer_callbacks_now();
  368. return retval;
  369. }
  370. static int default_virt2phys(struct target_s *target, u32 virtual, u32 *physical)
  371. {
  372. *physical = virtual;
  373. return ERROR_OK;
  374. }
  375. static int default_mmu(struct target_s *target, int *enabled)
  376. {
  377. *enabled = 0;
  378. return ERROR_OK;
  379. }
  380. static int default_examine(struct target_s *target)
  381. {
  382. target->type->examined = 1;
  383. return ERROR_OK;
  384. }
  385. /* Targets that correctly implement init+examine, i.e.
  386. * no communication with target during init:
  387. *
  388. * XScale
  389. */
  390. int target_examine(void)
  391. {
  392. int retval = ERROR_OK;
  393. target_t *target = all_targets;
  394. while (target)
  395. {
  396. if ((retval = target->type->examine(target))!=ERROR_OK)
  397. return retval;
  398. target = target->next;
  399. }
  400. return retval;
  401. }
  402. static int target_write_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
  403. {
  404. if (!target->type->examined)
  405. {
  406. LOG_ERROR("Target not examined yet");
  407. return ERROR_FAIL;
  408. }
  409. return target->type->write_memory_imp(target, address, size, count, buffer);
  410. }
  411. static int target_read_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
  412. {
  413. if (!target->type->examined)
  414. {
  415. LOG_ERROR("Target not examined yet");
  416. return ERROR_FAIL;
  417. }
  418. return target->type->read_memory_imp(target, address, size, count, buffer);
  419. }
  420. static int target_soft_reset_halt_imp(struct target_s *target)
  421. {
  422. if (!target->type->examined)
  423. {
  424. LOG_ERROR("Target not examined yet");
  425. return ERROR_FAIL;
  426. }
  427. return target->type->soft_reset_halt_imp(target);
  428. }
  429. static int target_run_algorithm_imp(struct target_s *target, int num_mem_params, mem_param_t *mem_params, int num_reg_params, reg_param_t *reg_param, u32 entry_point, u32 exit_point, int timeout_ms, void *arch_info)
  430. {
  431. if (!target->type->examined)
  432. {
  433. LOG_ERROR("Target not examined yet");
  434. return ERROR_FAIL;
  435. }
  436. return target->type->run_algorithm_imp(target, num_mem_params, mem_params, num_reg_params, reg_param, entry_point, exit_point, timeout_ms, arch_info);
  437. }
  438. int target_init(struct command_context_s *cmd_ctx)
  439. {
  440. target_t *target = all_targets;
  441. int retval;
  442. while (target)
  443. {
  444. target->type->examined = 0;
  445. if (target->type->examine == NULL)
  446. {
  447. target->type->examine = default_examine;
  448. }
  449. if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
  450. {
  451. LOG_ERROR("target '%s' init failed", target->type->name);
  452. return retval;
  453. }
  454. /* Set up default functions if none are provided by target */
  455. if (target->type->virt2phys == NULL)
  456. {
  457. target->type->virt2phys = default_virt2phys;
  458. }
  459. target->type->virt2phys = default_virt2phys;
  460. /* a non-invasive way(in terms of patches) to add some code that
  461. * runs before the type->write/read_memory implementation
  462. */
  463. target->type->write_memory_imp = target->type->write_memory;
  464. target->type->write_memory = target_write_memory_imp;
  465. target->type->read_memory_imp = target->type->read_memory;
  466. target->type->read_memory = target_read_memory_imp;
  467. target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
  468. target->type->soft_reset_halt = target_soft_reset_halt_imp;
  469. target->type->run_algorithm_imp = target->type->run_algorithm;
  470. target->type->run_algorithm = target_run_algorithm_imp;
  471. if (target->type->mmu == NULL)
  472. {
  473. target->type->mmu = default_mmu;
  474. }
  475. target = target->next;
  476. }
  477. if (all_targets)
  478. {
  479. if((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
  480. return retval;
  481. if((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
  482. return retval;
  483. }
  484. return ERROR_OK;
  485. }
  486. int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
  487. {
  488. target_event_callback_t **callbacks_p = &target_event_callbacks;
  489. if (callback == NULL)
  490. {
  491. return ERROR_INVALID_ARGUMENTS;
  492. }
  493. if (*callbacks_p)
  494. {
  495. while ((*callbacks_p)->next)
  496. callbacks_p = &((*callbacks_p)->next);
  497. callbacks_p = &((*callbacks_p)->next);
  498. }
  499. (*callbacks_p) = malloc(sizeof(target_event_callback_t));
  500. (*callbacks_p)->callback = callback;
  501. (*callbacks_p)->priv = priv;
  502. (*callbacks_p)->next = NULL;
  503. return ERROR_OK;
  504. }
  505. int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
  506. {
  507. target_timer_callback_t **callbacks_p = &target_timer_callbacks;
  508. struct timeval now;
  509. if (callback == NULL)
  510. {
  511. return ERROR_INVALID_ARGUMENTS;
  512. }
  513. if (*callbacks_p)
  514. {
  515. while ((*callbacks_p)->next)
  516. callbacks_p = &((*callbacks_p)->next);
  517. callbacks_p = &((*callbacks_p)->next);
  518. }
  519. (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
  520. (*callbacks_p)->callback = callback;
  521. (*callbacks_p)->periodic = periodic;
  522. (*callbacks_p)->time_ms = time_ms;
  523. gettimeofday(&now, NULL);
  524. (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
  525. time_ms -= (time_ms % 1000);
  526. (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
  527. if ((*callbacks_p)->when.tv_usec > 1000000)
  528. {
  529. (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
  530. (*callbacks_p)->when.tv_sec += 1;
  531. }
  532. (*callbacks_p)->priv = priv;
  533. (*callbacks_p)->next = NULL;
  534. return ERROR_OK;
  535. }
  536. int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
  537. {
  538. target_event_callback_t **p = &target_event_callbacks;
  539. target_event_callback_t *c = target_event_callbacks;
  540. if (callback == NULL)
  541. {
  542. return ERROR_INVALID_ARGUMENTS;
  543. }
  544. while (c)
  545. {
  546. target_event_callback_t *next = c->next;
  547. if ((c->callback == callback) && (c->priv == priv))
  548. {
  549. *p = next;
  550. free(c);
  551. return ERROR_OK;
  552. }
  553. else
  554. p = &(c->next);
  555. c = next;
  556. }
  557. return ERROR_OK;
  558. }
  559. int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
  560. {
  561. target_timer_callback_t **p = &target_timer_callbacks;
  562. target_timer_callback_t *c = target_timer_callbacks;
  563. if (callback == NULL)
  564. {
  565. return ERROR_INVALID_ARGUMENTS;
  566. }
  567. while (c)
  568. {
  569. target_timer_callback_t *next = c->next;
  570. if ((c->callback == callback) && (c->priv == priv))
  571. {
  572. *p = next;
  573. free(c);
  574. return ERROR_OK;
  575. }
  576. else
  577. p = &(c->next);
  578. c = next;
  579. }
  580. return ERROR_OK;
  581. }
  582. int target_call_event_callbacks(target_t *target, enum target_event event)
  583. {
  584. target_event_callback_t *callback = target_event_callbacks;
  585. target_event_callback_t *next_callback;
  586. if (event == TARGET_EVENT_HALTED)
  587. {
  588. /* execute early halted first */
  589. target_call_event_callbacks(target, TARGET_EVENT_EARLY_HALTED);
  590. }
  591. LOG_DEBUG("target event %i (%s)",
  592. event,
  593. Jim_Nvp_value2name_simple( nvp_target_event, event )->name );
  594. target_handle_event( target, event );
  595. while (callback)
  596. {
  597. next_callback = callback->next;
  598. callback->callback(target, event, callback->priv);
  599. callback = next_callback;
  600. }
  601. return ERROR_OK;
  602. }
  603. static int target_call_timer_callbacks_check_time(int checktime)
  604. {
  605. target_timer_callback_t *callback = target_timer_callbacks;
  606. target_timer_callback_t *next_callback;
  607. struct timeval now;
  608. keep_alive();
  609. gettimeofday(&now, NULL);
  610. while (callback)
  611. {
  612. next_callback = callback->next;
  613. if ((!checktime&&callback->periodic)||
  614. (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
  615. || (now.tv_sec > callback->when.tv_sec)))
  616. {
  617. if(callback->callback != NULL)
  618. {
  619. callback->callback(callback->priv);
  620. if (callback->periodic)
  621. {
  622. int time_ms = callback->time_ms;
  623. callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
  624. time_ms -= (time_ms % 1000);
  625. callback->when.tv_sec = now.tv_sec + time_ms / 1000;
  626. if (callback->when.tv_usec > 1000000)
  627. {
  628. callback->when.tv_usec = callback->when.tv_usec - 1000000;
  629. callback->when.tv_sec += 1;
  630. }
  631. }
  632. else
  633. {
  634. int retval;
  635. if((retval = target_unregister_timer_callback(callback->callback, callback->priv)) != ERROR_OK)
  636. return retval;
  637. }
  638. }
  639. }
  640. callback = next_callback;
  641. }
  642. return ERROR_OK;
  643. }
  644. int target_call_timer_callbacks(void)
  645. {
  646. return target_call_timer_callbacks_check_time(1);
  647. }
  648. /* invoke periodic callbacks immediately */
  649. int target_call_timer_callbacks_now(void)
  650. {
  651. return target_call_timer_callbacks_check_time(0);
  652. }
  653. int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
  654. {
  655. working_area_t *c = target->working_areas;
  656. working_area_t *new_wa = NULL;
  657. /* Reevaluate working area address based on MMU state*/
  658. if (target->working_areas == NULL)
  659. {
  660. int retval;
  661. int enabled;
  662. retval = target->type->mmu(target, &enabled);
  663. if (retval != ERROR_OK)
  664. {
  665. return retval;
  666. }
  667. if (enabled)
  668. {
  669. target->working_area = target->working_area_virt;
  670. }
  671. else
  672. {
  673. target->working_area = target->working_area_phys;
  674. }
  675. }
  676. /* only allocate multiples of 4 byte */
  677. if (size % 4)
  678. {
  679. LOG_ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
  680. size = CEIL(size, 4);
  681. }
  682. /* see if there's already a matching working area */
  683. while (c)
  684. {
  685. if ((c->free) && (c->size == size))
  686. {
  687. new_wa = c;
  688. break;
  689. }
  690. c = c->next;
  691. }
  692. /* if not, allocate a new one */
  693. if (!new_wa)
  694. {
  695. working_area_t **p = &target->working_areas;
  696. u32 first_free = target->working_area;
  697. u32 free_size = target->working_area_size;
  698. LOG_DEBUG("allocating new working area");
  699. c = target->working_areas;
  700. while (c)
  701. {
  702. first_free += c->size;
  703. free_size -= c->size;
  704. p = &c->next;
  705. c = c->next;
  706. }
  707. if (free_size < size)
  708. {
  709. LOG_WARNING("not enough working area available(requested %d, free %d)", size, free_size);
  710. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  711. }
  712. new_wa = malloc(sizeof(working_area_t));
  713. new_wa->next = NULL;
  714. new_wa->size = size;
  715. new_wa->address = first_free;
  716. if (target->backup_working_area)
  717. {
  718. int retval;
  719. new_wa->backup = malloc(new_wa->size);
  720. if((retval = target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
  721. {
  722. free(new_wa->backup);
  723. free(new_wa);
  724. return retval;
  725. }
  726. }
  727. else
  728. {
  729. new_wa->backup = NULL;
  730. }
  731. /* put new entry in list */
  732. *p = new_wa;
  733. }
  734. /* mark as used, and return the new (reused) area */
  735. new_wa->free = 0;
  736. *area = new_wa;
  737. /* user pointer */
  738. new_wa->user = area;
  739. return ERROR_OK;
  740. }
  741. int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
  742. {
  743. if (area->free)
  744. return ERROR_OK;
  745. if (restore&&target->backup_working_area)
  746. {
  747. int retval;
  748. if((retval = target->type->write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
  749. return retval;
  750. }
  751. area->free = 1;
  752. /* mark user pointer invalid */
  753. *area->user = NULL;
  754. area->user = NULL;
  755. return ERROR_OK;
  756. }
  757. int target_free_working_area(struct target_s *target, working_area_t *area)
  758. {
  759. return target_free_working_area_restore(target, area, 1);
  760. }
  761. /* free resources and restore memory, if restoring memory fails,
  762. * free up resources anyway
  763. */
  764. void target_free_all_working_areas_restore(struct target_s *target, int restore)
  765. {
  766. working_area_t *c = target->working_areas;
  767. while (c)
  768. {
  769. working_area_t *next = c->next;
  770. target_free_working_area_restore(target, c, restore);
  771. if (c->backup)
  772. free(c->backup);
  773. free(c);
  774. c = next;
  775. }
  776. target->working_areas = NULL;
  777. }
  778. void target_free_all_working_areas(struct target_s *target)
  779. {
  780. target_free_all_working_areas_restore(target, 1);
  781. }
  782. int target_register_commands(struct command_context_s *cmd_ctx)
  783. {
  784. register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, "change the current command line target (one parameter) or lists targets (with no parameter)");
  785. register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address");
  786. register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
  787. register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
  788. "same args as load_image, image stored in memory - mainly for profiling purposes");
  789. register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
  790. "loads active fast load image to current target - mainly for profiling purposes");
  791. register_jim(cmd_ctx, "target", jim_target, "configure target" );
  792. /* script procedures */
  793. register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array, "read memory and return as a TCL array for script processing");
  794. register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem, "convert a TCL array to memory locations and write the values");
  795. return ERROR_OK;
  796. }
  797. int target_arch_state(struct target_s *target)
  798. {
  799. int retval;
  800. if (target==NULL)
  801. {
  802. LOG_USER("No target has been configured");
  803. return ERROR_OK;
  804. }
  805. LOG_USER("target state: %s",
  806. Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name);
  807. if (target->state!=TARGET_HALTED)
  808. return ERROR_OK;
  809. retval=target->type->arch_state(target);
  810. return retval;
  811. }
  812. /* Single aligned words are guaranteed to use 16 or 32 bit access
  813. * mode respectively, otherwise data is handled as quickly as
  814. * possible
  815. */
  816. int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
  817. {
  818. int retval;
  819. LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
  820. if (!target->type->examined)
  821. {
  822. LOG_ERROR("Target not examined yet");
  823. return ERROR_FAIL;
  824. }
  825. if ((address + size - 1) < address)
  826. {
  827. /* GDB can request this when e.g. PC is 0xfffffffc*/
  828. LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
  829. return ERROR_FAIL;
  830. }
  831. if (((address % 2) == 0) && (size == 2))
  832. {
  833. return target->type->write_memory(target, address, 2, 1, buffer);
  834. }
  835. /* handle unaligned head bytes */
  836. if (address % 4)
  837. {
  838. int unaligned = 4 - (address % 4);
  839. if (unaligned > size)
  840. unaligned = size;
  841. if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
  842. return retval;
  843. buffer += unaligned;
  844. address += unaligned;
  845. size -= unaligned;
  846. }
  847. /* handle aligned words */
  848. if (size >= 4)
  849. {
  850. int aligned = size - (size % 4);
  851. /* use bulk writes above a certain limit. This may have to be changed */
  852. if (aligned > 128)
  853. {
  854. if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
  855. return retval;
  856. }
  857. else
  858. {
  859. if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
  860. return retval;
  861. }
  862. buffer += aligned;
  863. address += aligned;
  864. size -= aligned;
  865. }
  866. /* handle tail writes of less than 4 bytes */
  867. if (size > 0)
  868. {
  869. if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
  870. return retval;
  871. }
  872. return ERROR_OK;
  873. }
  874. /* Single aligned words are guaranteed to use 16 or 32 bit access
  875. * mode respectively, otherwise data is handled as quickly as
  876. * possible
  877. */
  878. int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
  879. {
  880. int retval;
  881. LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
  882. if (!target->type->examined)
  883. {
  884. LOG_ERROR("Target not examined yet");
  885. return ERROR_FAIL;
  886. }
  887. if ((address + size - 1) < address)
  888. {
  889. /* GDB can request this when e.g. PC is 0xfffffffc*/
  890. LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
  891. return ERROR_FAIL;
  892. }
  893. if (((address % 2) == 0) && (size == 2))
  894. {
  895. return target->type->read_memory(target, address, 2, 1, buffer);
  896. }
  897. /* handle unaligned head bytes */
  898. if (address % 4)
  899. {
  900. int unaligned = 4 - (address % 4);
  901. if (unaligned > size)
  902. unaligned = size;
  903. if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
  904. return retval;
  905. buffer += unaligned;
  906. address += unaligned;
  907. size -= unaligned;
  908. }
  909. /* handle aligned words */
  910. if (size >= 4)
  911. {
  912. int aligned = size - (size % 4);
  913. if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
  914. return retval;
  915. buffer += aligned;
  916. address += aligned;
  917. size -= aligned;
  918. }
  919. /* handle tail writes of less than 4 bytes */
  920. if (size > 0)
  921. {
  922. if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
  923. return retval;
  924. }
  925. return ERROR_OK;
  926. }
  927. int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* crc)
  928. {
  929. u8 *buffer;
  930. int retval;
  931. int i;
  932. u32 checksum = 0;
  933. if (!target->type->examined)
  934. {
  935. LOG_ERROR("Target not examined yet");
  936. return ERROR_FAIL;
  937. }
  938. if ((retval = target->type->checksum_memory(target, address,
  939. size, &checksum)) != ERROR_OK)
  940. {
  941. buffer = malloc(size);
  942. if (buffer == NULL)
  943. {
  944. LOG_ERROR("error allocating buffer for section (%d bytes)", size);
  945. return ERROR_INVALID_ARGUMENTS;
  946. }
  947. retval = target_read_buffer(target, address, size, buffer);
  948. if (retval != ERROR_OK)
  949. {
  950. free(buffer);
  951. return retval;
  952. }
  953. /* convert to target endianess */
  954. for (i = 0; i < (size/sizeof(u32)); i++)
  955. {
  956. u32 target_data;
  957. target_data = target_buffer_get_u32(target, &buffer[i*sizeof(u32)]);
  958. target_buffer_set_u32(target, &buffer[i*sizeof(u32)], target_data);
  959. }
  960. retval = image_calculate_checksum( buffer, size, &checksum );
  961. free(buffer);
  962. }
  963. *crc = checksum;
  964. return retval;
  965. }
  966. int target_blank_check_memory(struct target_s *target, u32 address, u32 size, u32* blank)
  967. {
  968. int retval;
  969. if (!target->type->examined)
  970. {
  971. LOG_ERROR("Target not examined yet");
  972. return ERROR_FAIL;
  973. }
  974. if (target->type->blank_check_memory == 0)
  975. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  976. retval = target->type->blank_check_memory(target, address, size, blank);
  977. return retval;
  978. }
  979. int target_read_u32(struct target_s *target, u32 address, u32 *value)
  980. {
  981. u8 value_buf[4];
  982. if (!target->type->examined)
  983. {
  984. LOG_ERROR("Target not examined yet");
  985. return ERROR_FAIL;
  986. }
  987. int retval = target->type->read_memory(target, address, 4, 1, value_buf);
  988. if (retval == ERROR_OK)
  989. {
  990. *value = target_buffer_get_u32(target, value_buf);
  991. LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
  992. }
  993. else
  994. {
  995. *value = 0x0;
  996. LOG_DEBUG("address: 0x%8.8x failed", address);
  997. }
  998. return retval;
  999. }
  1000. int target_read_u16(struct target_s *target, u32 address, u16 *value)
  1001. {
  1002. u8 value_buf[2];
  1003. if (!target->type->examined)
  1004. {
  1005. LOG_ERROR("Target not examined yet");
  1006. return ERROR_FAIL;
  1007. }
  1008. int retval = target->type->read_memory(target, address, 2, 1, value_buf);
  1009. if (retval == ERROR_OK)
  1010. {
  1011. *value = target_buffer_get_u16(target, value_buf);
  1012. LOG_DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
  1013. }
  1014. else
  1015. {
  1016. *value = 0x0;
  1017. LOG_DEBUG("address: 0x%8.8x failed", address);
  1018. }
  1019. return retval;
  1020. }
  1021. int target_read_u8(struct target_s *target, u32 address, u8 *value)
  1022. {
  1023. int retval = target->type->read_memory(target, address, 1, 1, value);
  1024. if (!target->type->examined)
  1025. {
  1026. LOG_ERROR("Target not examined yet");
  1027. return ERROR_FAIL;
  1028. }
  1029. if (retval == ERROR_OK)
  1030. {
  1031. LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
  1032. }
  1033. else
  1034. {
  1035. *value = 0x0;
  1036. LOG_DEBUG("address: 0x%8.8x failed", address);
  1037. }
  1038. return retval;
  1039. }
  1040. int target_write_u32(struct target_s *target, u32 address, u32 value)
  1041. {
  1042. int retval;
  1043. u8 value_buf[4];
  1044. if (!target->type->examined)
  1045. {
  1046. LOG_ERROR("Target not examined yet");
  1047. return ERROR_FAIL;
  1048. }
  1049. LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
  1050. target_buffer_set_u32(target, value_buf, value);
  1051. if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
  1052. {
  1053. LOG_DEBUG("failed: %i", retval);
  1054. }
  1055. return retval;
  1056. }
  1057. int target_write_u16(struct target_s *target, u32 address, u16 value)
  1058. {
  1059. int retval;
  1060. u8 value_buf[2];
  1061. if (!target->type->examined)
  1062. {
  1063. LOG_ERROR("Target not examined yet");
  1064. return ERROR_FAIL;
  1065. }
  1066. LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
  1067. target_buffer_set_u16(target, value_buf, value);
  1068. if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
  1069. {
  1070. LOG_DEBUG("failed: %i", retval);
  1071. }
  1072. return retval;
  1073. }
  1074. int target_write_u8(struct target_s *target, u32 address, u8 value)
  1075. {
  1076. int retval;
  1077. if (!target->type->examined)
  1078. {
  1079. LOG_ERROR("Target not examined yet");
  1080. return ERROR_FAIL;
  1081. }
  1082. LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
  1083. if ((retval = target->type->write_memory(target, address, 1, 1, &value)) != ERROR_OK)
  1084. {
  1085. LOG_DEBUG("failed: %i", retval);
  1086. }
  1087. return retval;
  1088. }
  1089. int target_register_user_commands(struct command_context_s *cmd_ctx)
  1090. {
  1091. int retval = ERROR_OK;
  1092. register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
  1093. register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
  1094. register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
  1095. register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
  1096. register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
  1097. register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
  1098. register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init] - default is run");
  1099. register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
  1100. register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
  1101. register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
  1102. register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
  1103. register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value> [count]");
  1104. register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value> [count]");
  1105. register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value> [count]");
  1106. register_command(cmd_ctx, NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
  1107. register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
  1108. register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
  1109. register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
  1110. register_command(cmd_ctx, NULL, "load_image", handle_load_image_command, COMMAND_EXEC, "load_image <file> <address> ['bin'|'ihex'|'elf'|'s19'] [min_address] [max_length]");
  1111. register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
  1112. register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
  1113. if((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
  1114. return retval;
  1115. if((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
  1116. return retval;
  1117. return retval;
  1118. }
  1119. int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1120. {
  1121. char *cp;
  1122. target_t *target = all_targets;
  1123. if (argc == 1)
  1124. {
  1125. /* try as tcltarget name */
  1126. for( target = all_targets ; target ; target = target->next ){
  1127. if( target->cmd_name ){
  1128. if( 0 == strcmp( args[0], target->cmd_name ) ){
  1129. /* MATCH */
  1130. goto Match;
  1131. }
  1132. }
  1133. }
  1134. /* no match, try as number */
  1135. int num = strtoul(args[0], &cp, 0 );
  1136. if( *cp != 0 ){
  1137. /* then it was not a number */
  1138. command_print( cmd_ctx, "Target: %s unknown, try one of:\n", args[0] );
  1139. goto DumpTargets;
  1140. }
  1141. target = get_target_by_num( num );
  1142. if( target == NULL ){
  1143. command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0] );
  1144. goto DumpTargets;
  1145. }
  1146. Match:
  1147. cmd_ctx->current_target = target->target_number;
  1148. return ERROR_OK;
  1149. }
  1150. DumpTargets:
  1151. target = all_targets;
  1152. command_print(cmd_ctx, " CmdName Type Endian AbsChainPos Name State ");
  1153. command_print(cmd_ctx, "-- ---------- ---------- ---------- ----------- ------------- ----------");
  1154. while (target)
  1155. {
  1156. /* XX: abcdefghij abcdefghij abcdefghij abcdefghij */
  1157. command_print(cmd_ctx, "%2d: %-10s %-10s %-10s %10d %14s %s",
  1158. target->target_number,
  1159. target->cmd_name,
  1160. target->type->name,
  1161. Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness )->name,
  1162. target->tap->abs_chain_position,
  1163. target->tap->dotted_name,
  1164. Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name );
  1165. target = target->next;
  1166. }
  1167. return ERROR_OK;
  1168. }
  1169. /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
  1170. static int powerDropout;
  1171. static int srstAsserted;
  1172. static int runPowerRestore;
  1173. static int runPowerDropout;
  1174. static int runSrstAsserted;
  1175. static int runSrstDeasserted;
  1176. static int sense_handler(void)
  1177. {
  1178. static int prevSrstAsserted = 0;
  1179. static int prevPowerdropout = 0;
  1180. int retval;
  1181. if ((retval=jtag_power_dropout(&powerDropout))!=ERROR_OK)
  1182. return retval;
  1183. int powerRestored;
  1184. powerRestored = prevPowerdropout && !powerDropout;
  1185. if (powerRestored)
  1186. {
  1187. runPowerRestore = 1;
  1188. }
  1189. long long current = timeval_ms();
  1190. static long long lastPower = 0;
  1191. int waitMore = lastPower + 2000 > current;
  1192. if (powerDropout && !waitMore)
  1193. {
  1194. runPowerDropout = 1;
  1195. lastPower = current;
  1196. }
  1197. if ((retval=jtag_srst_asserted(&srstAsserted))!=ERROR_OK)
  1198. return retval;
  1199. int srstDeasserted;
  1200. srstDeasserted = prevSrstAsserted && !srstAsserted;
  1201. static long long lastSrst = 0;
  1202. waitMore = lastSrst + 2000 > current;
  1203. if (srstDeasserted && !waitMore)
  1204. {
  1205. runSrstDeasserted = 1;
  1206. lastSrst = current;
  1207. }
  1208. if (!prevSrstAsserted && srstAsserted)
  1209. {
  1210. runSrstAsserted = 1;
  1211. }
  1212. prevSrstAsserted = srstAsserted;
  1213. prevPowerdropout = powerDropout;
  1214. if (srstDeasserted || powerRestored)
  1215. {
  1216. /* Other than logging the event we can't do anything here.
  1217. * Issuing a reset is a particularly bad idea as we might
  1218. * be inside a reset already.
  1219. */
  1220. }
  1221. return ERROR_OK;
  1222. }
  1223. /* process target state changes */
  1224. int handle_target(void *priv)
  1225. {
  1226. int retval = ERROR_OK;
  1227. /* we do not want to recurse here... */
  1228. static int recursive = 0;
  1229. if (! recursive)
  1230. {
  1231. recursive = 1;
  1232. sense_handler();
  1233. /* danger! running these procedures can trigger srst assertions and power dropouts.
  1234. * We need to avoid an infinite loop/recursion here and we do that by
  1235. * clearing the flags after running these events.
  1236. */
  1237. int did_something = 0;
  1238. if (runSrstAsserted)
  1239. {
  1240. Jim_Eval( interp, "srst_asserted");
  1241. did_something = 1;
  1242. }
  1243. if (runSrstDeasserted)
  1244. {
  1245. Jim_Eval( interp, "srst_deasserted");
  1246. did_something = 1;
  1247. }
  1248. if (runPowerDropout)
  1249. {
  1250. Jim_Eval( interp, "power_dropout");
  1251. did_something = 1;
  1252. }
  1253. if (runPowerRestore)
  1254. {
  1255. Jim_Eval( interp, "power_restore");
  1256. did_something = 1;
  1257. }
  1258. if (did_something)
  1259. {
  1260. /* clear detect flags */
  1261. sense_handler();
  1262. }
  1263. /* clear action flags */
  1264. runSrstAsserted=0;
  1265. runSrstDeasserted=0;
  1266. runPowerRestore=0;
  1267. runPowerDropout=0;
  1268. recursive = 0;
  1269. }
  1270. target_t *target = all_targets;
  1271. while (target)
  1272. {
  1273. /* only poll target if we've got power and srst isn't asserted */
  1274. if (target_continous_poll&&!powerDropout&&!srstAsserted)
  1275. {
  1276. /* polling may fail silently until the target has been examined */
  1277. if((retval = target_poll(target)) != ERROR_OK)
  1278. return retval;
  1279. }
  1280. target = target->next;
  1281. }
  1282. return retval;
  1283. }
  1284. int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1285. {
  1286. target_t *target;
  1287. reg_t *reg = NULL;
  1288. int count = 0;
  1289. char *value;
  1290. LOG_DEBUG("-");
  1291. target = get_current_target(cmd_ctx);
  1292. /* list all available registers for the current target */
  1293. if (argc == 0)
  1294. {
  1295. reg_cache_t *cache = target->reg_cache;
  1296. count = 0;
  1297. while(cache)
  1298. {
  1299. int i;
  1300. for (i = 0; i < cache->num_regs; i++)
  1301. {
  1302. value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
  1303. command_print(cmd_ctx, "(%i) %s (/%i): 0x%s (dirty: %i, valid: %i)", count++, cache->reg_list[i].name, cache->reg_list[i].size, value, cache->reg_list[i].dirty, cache->reg_list[i].valid);
  1304. free(value);
  1305. }
  1306. cache = cache->next;
  1307. }
  1308. return ERROR_OK;
  1309. }
  1310. /* access a single register by its ordinal number */
  1311. if ((args[0][0] >= '0') && (args[0][0] <= '9'))
  1312. {
  1313. int num = strtoul(args[0], NULL, 0);
  1314. reg_cache_t *cache = target->reg_cache;
  1315. count = 0;
  1316. while(cache)
  1317. {
  1318. int i;
  1319. for (i = 0; i < cache->num_regs; i++)
  1320. {
  1321. if (count++ == num)
  1322. {
  1323. reg = &cache->reg_list[i];
  1324. break;
  1325. }
  1326. }
  1327. if (reg)
  1328. break;
  1329. cache = cache->next;
  1330. }
  1331. if (!reg)
  1332. {
  1333. command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
  1334. return ERROR_OK;
  1335. }
  1336. } else /* access a single register by its name */
  1337. {
  1338. reg = register_get_by_name(target->reg_cache, args[0], 1);
  1339. if (!reg)
  1340. {
  1341. command_print(cmd_ctx, "register %s not found in current target", args[0]);
  1342. return ERROR_OK;
  1343. }
  1344. }
  1345. /* display a register */
  1346. if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
  1347. {
  1348. if ((argc == 2) && (strcmp(args[1], "force") == 0))
  1349. reg->valid = 0;
  1350. if (reg->valid == 0)
  1351. {
  1352. reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
  1353. arch_type->get(reg);
  1354. }
  1355. value = buf_to_str(reg->value, reg->size, 16);
  1356. command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
  1357. free(value);
  1358. return ERROR_OK;
  1359. }
  1360. /* set register value */
  1361. if (argc == 2)
  1362. {
  1363. u8 *buf = malloc(CEIL(reg->size, 8));
  1364. str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
  1365. reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
  1366. arch_type->set(reg, buf);
  1367. value = buf_to_str(reg->value, reg->size, 16);
  1368. command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
  1369. free(value);
  1370. free(buf);
  1371. return ERROR_OK;
  1372. }
  1373. command_print(cmd_ctx, "usage: reg <#|name> [value]");
  1374. return ERROR_OK;
  1375. }
  1376. int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1377. {
  1378. int retval = ERROR_OK;
  1379. target_t *target = get_current_target(cmd_ctx);
  1380. if (argc == 0)
  1381. {
  1382. if((retval = target_poll(target)) != ERROR_OK)
  1383. return retval;
  1384. if((retval = target_arch_state(target)) != ERROR_OK)
  1385. return retval;
  1386. }
  1387. else if (argc==1)
  1388. {
  1389. if (strcmp(args[0], "on") == 0)
  1390. {
  1391. target_continous_poll = 1;
  1392. }
  1393. else if (strcmp(args[0], "off") == 0)
  1394. {
  1395. target_continous_poll = 0;
  1396. }
  1397. else
  1398. {
  1399. command_print(cmd_ctx, "arg is \"on\" or \"off\"");
  1400. }
  1401. } else
  1402. {
  1403. return ERROR_COMMAND_SYNTAX_ERROR;
  1404. }
  1405. return retval;
  1406. }
  1407. int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1408. {
  1409. int ms = 5000;
  1410. if (argc > 0)
  1411. {
  1412. char *end;
  1413. ms = strtoul(args[0], &end, 0) * 1000;
  1414. if (*end)
  1415. {
  1416. command_print(cmd_ctx, "usage: %s [seconds]", cmd);
  1417. return ERROR_OK;
  1418. }
  1419. }
  1420. target_t *target = get_current_target(cmd_ctx);
  1421. return target_wait_state(target, TARGET_HALTED, ms);
  1422. }
  1423. int target_wait_state(target_t *target, enum target_state state, int ms)
  1424. {
  1425. int retval;
  1426. struct timeval timeout, now;
  1427. int once=1;
  1428. gettimeofday(&timeout, NULL);
  1429. timeval_add_time(&timeout, 0, ms * 1000);
  1430. for (;;)
  1431. {
  1432. if ((retval=target_poll(target))!=ERROR_OK)
  1433. return retval;
  1434. keep_alive();
  1435. if (target->state == state)
  1436. {
  1437. break;
  1438. }
  1439. if (once)
  1440. {
  1441. once=0;
  1442. LOG_DEBUG("waiting for target %s...",
  1443. Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
  1444. }
  1445. gettimeofday(&now, NULL);
  1446. if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
  1447. {
  1448. LOG_ERROR("timed out while waiting for target %s",
  1449. Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
  1450. return ERROR_FAIL;
  1451. }
  1452. }
  1453. return ERROR_OK;
  1454. }
  1455. int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1456. {
  1457. int retval;
  1458. target_t *target = get_current_target(cmd_ctx);
  1459. LOG_DEBUG("-");
  1460. if ((retval = target_halt(target)) != ERROR_OK)
  1461. {
  1462. return retval;
  1463. }
  1464. return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
  1465. }
  1466. int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1467. {
  1468. target_t *target = get_current_target(cmd_ctx);
  1469. LOG_USER("requesting target halt and executing a soft reset");
  1470. target->type->soft_reset_halt(target);
  1471. return ERROR_OK;
  1472. }
  1473. int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1474. {
  1475. const Jim_Nvp *n;
  1476. enum target_reset_mode reset_mode = RESET_RUN;
  1477. if (argc >= 1)
  1478. {
  1479. n = Jim_Nvp_name2value_simple( nvp_reset_modes, args[0] );
  1480. if( (n->name == NULL) || (n->value == RESET_UNKNOWN) ){
  1481. return ERROR_COMMAND_SYNTAX_ERROR;
  1482. }
  1483. reset_mode = n->value;
  1484. }
  1485. /* reset *all* targets */
  1486. return target_process_reset(cmd_ctx, reset_mode);
  1487. }
  1488. int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1489. {
  1490. int retval;
  1491. target_t *target = get_current_target(cmd_ctx);
  1492. target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
  1493. if (argc == 0)
  1494. retval = target_resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
  1495. else if (argc == 1)
  1496. retval = target_resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
  1497. else
  1498. {
  1499. retval = ERROR_COMMAND_SYNTAX_ERROR;
  1500. }
  1501. return retval;
  1502. }
  1503. int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1504. {
  1505. target_t *target = get_current_target(cmd_ctx);
  1506. LOG_DEBUG("-");
  1507. if (argc == 0)
  1508. return target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
  1509. if (argc == 1)
  1510. return target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
  1511. return ERROR_OK;
  1512. }
  1513. int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1514. {
  1515. const int line_bytecnt = 32;
  1516. int count = 1;
  1517. int size = 4;
  1518. u32 address = 0;
  1519. int line_modulo;
  1520. int i;
  1521. char output[128];
  1522. int output_len;
  1523. int retval;
  1524. u8 *buffer;
  1525. target_t *target = get_current_target(cmd_ctx);
  1526. if (argc < 1)
  1527. return ERROR_OK;
  1528. if (argc == 2)
  1529. count = strtoul(args[1], NULL, 0);
  1530. address = strtoul(args[0], NULL, 0);
  1531. switch (cmd[2])
  1532. {
  1533. case 'w':
  1534. size = 4; line_modulo = line_bytecnt / 4;
  1535. break;
  1536. case 'h':
  1537. size = 2; line_modulo = line_bytecnt / 2;
  1538. break;
  1539. case 'b':
  1540. size = 1; line_modulo = line_bytecnt / 1;
  1541. break;
  1542. default:
  1543. return ERROR_OK;
  1544. }
  1545. buffer = calloc(count, size);
  1546. retval = target->type->read_memory(target, address, size, count, buffer);
  1547. if (retval == ERROR_OK)
  1548. {
  1549. output_len = 0;
  1550. for (i = 0; i < count; i++)
  1551. {
  1552. if (i%line_modulo == 0)
  1553. output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
  1554. switch (size)
  1555. {
  1556. case 4:
  1557. output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
  1558. break;
  1559. case 2:
  1560. output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
  1561. break;
  1562. case 1:
  1563. output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
  1564. break;
  1565. }
  1566. if ((i%line_modulo == line_modulo-1) || (i == count - 1))
  1567. {
  1568. command_print(cmd_ctx, output);
  1569. output_len = 0;
  1570. }
  1571. }
  1572. }
  1573. free(buffer);
  1574. return retval;
  1575. }
  1576. int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1577. {
  1578. u32 address = 0;
  1579. u32 value = 0;
  1580. int count = 1;
  1581. int i;
  1582. int wordsize;
  1583. target_t *target = get_current_target(cmd_ctx);
  1584. u8 value_buf[4];
  1585. if ((argc < 2) || (argc > 3))
  1586. return ERROR_COMMAND_SYNTAX_ERROR;
  1587. address = strtoul(args[0], NULL, 0);
  1588. value = strtoul(args[1], NULL, 0);
  1589. if (argc == 3)
  1590. count = strtoul(args[2], NULL, 0);
  1591. switch (cmd[2])
  1592. {
  1593. case 'w':
  1594. wordsize = 4;
  1595. target_buffer_set_u32(target, value_buf, value);
  1596. break;
  1597. case 'h':
  1598. wordsize = 2;
  1599. target_buffer_set_u16(target, value_buf, value);
  1600. break;
  1601. case 'b':
  1602. wordsize = 1;
  1603. value_buf[0] = value;
  1604. break;
  1605. default:
  1606. return ERROR_COMMAND_SYNTAX_ERROR;
  1607. }
  1608. for (i=0; i<count; i++)
  1609. {
  1610. int retval;
  1611. switch (wordsize)
  1612. {
  1613. case 4:
  1614. retval = target->type->write_memory(target, address + i*wordsize, 4, 1, value_buf);
  1615. break;
  1616. case 2:
  1617. retval = target->type->write_memory(target, address + i*wordsize, 2, 1, value_buf);
  1618. break;
  1619. case 1:
  1620. retval = target->type->write_memory(target, address + i*wordsize, 1, 1, value_buf);
  1621. break;
  1622. default:
  1623. return ERROR_OK;
  1624. }
  1625. keep_alive();
  1626. if (retval!=ERROR_OK)
  1627. {
  1628. return retval;
  1629. }
  1630. }
  1631. return ERROR_OK;
  1632. }
  1633. int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1634. {
  1635. u8 *buffer;
  1636. u32 buf_cnt;
  1637. u32 image_size;
  1638. u32 min_address=0;
  1639. u32 max_address=0xffffffff;
  1640. int i;
  1641. int retval, retvaltemp;
  1642. image_t image;
  1643. duration_t duration;
  1644. char *duration_text;
  1645. target_t *target = get_current_target(cmd_ctx);
  1646. if ((argc < 1)||(argc > 5))
  1647. {
  1648. return ERROR_COMMAND_SYNTAX_ERROR;
  1649. }
  1650. /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
  1651. if (argc >= 2)
  1652. {
  1653. image.base_address_set = 1;
  1654. image.base_address = strtoul(args[1], NULL, 0);
  1655. }
  1656. else
  1657. {
  1658. image.base_address_set = 0;
  1659. }
  1660. image.start_address_set = 0;
  1661. if (argc>=4)
  1662. {
  1663. min_address=strtoul(args[3], NULL, 0);
  1664. }
  1665. if (argc>=5)
  1666. {
  1667. max_address=strtoul(args[4], NULL, 0)+min_address;
  1668. }
  1669. if (min_address>max_address)
  1670. {
  1671. return ERROR_COMMAND_SYNTAX_ERROR;
  1672. }
  1673. duration_start_measure(&duration);
  1674. if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
  1675. {
  1676. return ERROR_OK;
  1677. }
  1678. image_size = 0x0;
  1679. retval = ERROR_OK;
  1680. for (i = 0; i < image.num_sections; i++)
  1681. {
  1682. buffer = malloc(image.sections[i].size);
  1683. if (buffer == NULL)
  1684. {
  1685. command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
  1686. break;
  1687. }
  1688. if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
  1689. {
  1690. free(buffer);
  1691. break;
  1692. }
  1693. u32 offset=0;
  1694. u32 length=buf_cnt;
  1695. /* DANGER!!! beware of unsigned comparision here!!! */
  1696. if ((image.sections[i].base_address+buf_cnt>=min_address)&&
  1697. (image.sections[i].base_address<max_address))
  1698. {
  1699. if (image.sections[i].base_address<min_address)
  1700. {
  1701. /* clip addresses below */
  1702. offset+=min_address-image.sections[i].base_address;
  1703. length-=offset;
  1704. }
  1705. if (image.sections[i].base_address+buf_cnt>max_address)
  1706. {
  1707. length-=(image.sections[i].base_address+buf_cnt)-max_address;
  1708. }
  1709. if ((retval = target_write_buffer(target, image.sections[i].base_address+offset, length, buffer+offset)) != ERROR_OK)
  1710. {
  1711. free(buffer);
  1712. break;
  1713. }
  1714. image_size += length;
  1715. command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
  1716. }
  1717. free(buffer);
  1718. }
  1719. if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
  1720. {
  1721. image_close(&image);
  1722. return retvaltemp;
  1723. }
  1724. if (retval==ERROR_OK)
  1725. {
  1726. command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
  1727. }
  1728. free(duration_text);
  1729. image_close(&image);
  1730. return retval;
  1731. }
  1732. int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1733. {
  1734. fileio_t fileio;
  1735. u32 address;
  1736. u32 size;
  1737. u8 buffer[560];
  1738. int retval=ERROR_OK, retvaltemp;
  1739. duration_t duration;
  1740. char *duration_text;
  1741. target_t *target = get_current_target(cmd_ctx);
  1742. if (argc != 3)
  1743. {
  1744. command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
  1745. return ERROR_OK;
  1746. }
  1747. address = strtoul(args[1], NULL, 0);
  1748. size = strtoul(args[2], NULL, 0);
  1749. if ((address & 3) || (size & 3))
  1750. {
  1751. command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
  1752. return ERROR_OK;
  1753. }
  1754. if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
  1755. {
  1756. return ERROR_OK;
  1757. }
  1758. duration_start_measure(&duration);
  1759. while (size > 0)
  1760. {
  1761. u32 size_written;
  1762. u32 this_run_size = (size > 560) ? 560 : size;
  1763. retval = target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
  1764. if (retval != ERROR_OK)
  1765. {
  1766. break;
  1767. }
  1768. retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
  1769. if (retval != ERROR_OK)
  1770. {
  1771. break;
  1772. }
  1773. size -= this_run_size;
  1774. address += this_run_size;
  1775. }
  1776. if((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
  1777. return retvaltemp;
  1778. if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
  1779. return retvaltemp;
  1780. if (retval==ERROR_OK)
  1781. {
  1782. command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
  1783. }
  1784. free(duration_text);
  1785. return ERROR_OK;
  1786. }
  1787. int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1788. {
  1789. u8 *buffer;
  1790. u32 buf_cnt;
  1791. u32 image_size;
  1792. int i;
  1793. int retval, retvaltemp;
  1794. u32 checksum = 0;
  1795. u32 mem_checksum = 0;
  1796. image_t image;
  1797. duration_t duration;
  1798. char *duration_text;
  1799. target_t *target = get_current_target(cmd_ctx);
  1800. if (argc < 1)
  1801. {
  1802. return ERROR_COMMAND_SYNTAX_ERROR;
  1803. }
  1804. if (!target)
  1805. {
  1806. LOG_ERROR("no target selected");
  1807. return ERROR_FAIL;
  1808. }
  1809. duration_start_measure(&duration);
  1810. if (argc >= 2)
  1811. {
  1812. image.base_address_set = 1;
  1813. image.base_address = strtoul(args[1], NULL, 0);
  1814. }
  1815. else
  1816. {
  1817. image.base_address_set = 0;
  1818. image.base_address = 0x0;
  1819. }
  1820. image.start_address_set = 0;
  1821. if ((retval=image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
  1822. {
  1823. return retval;
  1824. }
  1825. image_size = 0x0;
  1826. retval=ERROR_OK;
  1827. for (i = 0; i < image.num_sections; i++)
  1828. {
  1829. buffer = malloc(image.sections[i].size);
  1830. if (buffer == NULL)
  1831. {
  1832. command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
  1833. break;
  1834. }
  1835. if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
  1836. {
  1837. free(buffer);
  1838. break;
  1839. }
  1840. /* calculate checksum of image */
  1841. image_calculate_checksum( buffer, buf_cnt, &checksum );
  1842. retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
  1843. if( retval != ERROR_OK )
  1844. {
  1845. free(buffer);
  1846. break;
  1847. }
  1848. if( checksum != mem_checksum )
  1849. {
  1850. /* failed crc checksum, fall back to a binary compare */
  1851. u8 *data;
  1852. command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
  1853. data = (u8*)malloc(buf_cnt);
  1854. /* Can we use 32bit word accesses? */
  1855. int size = 1;
  1856. int count = buf_cnt;
  1857. if ((count % 4) == 0)
  1858. {
  1859. size *= 4;
  1860. count /= 4;
  1861. }
  1862. retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
  1863. if (retval == ERROR_OK)
  1864. {
  1865. int t;
  1866. for (t = 0; t < buf_cnt; t++)
  1867. {
  1868. if (data[t] != buffer[t])
  1869. {
  1870. command_print(cmd_ctx, "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n", t + image.sections[i].base_address, data[t], buffer[t]);
  1871. free(data);
  1872. free(buffer);
  1873. retval=ERROR_FAIL;
  1874. goto done;
  1875. }
  1876. if ((t%16384)==0)
  1877. {
  1878. keep_alive();
  1879. }
  1880. }
  1881. }
  1882. free(data);
  1883. }
  1884. free(buffer);
  1885. image_size += buf_cnt;
  1886. }
  1887. done:
  1888. if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
  1889. {
  1890. image_close(&image);
  1891. return retvaltemp;
  1892. }
  1893. if (retval==ERROR_OK)
  1894. {
  1895. command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
  1896. }
  1897. free(duration_text);
  1898. image_close(&image);
  1899. return retval;
  1900. }
  1901. int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1902. {
  1903. int retval;
  1904. target_t *target = get_current_target(cmd_ctx);
  1905. if (argc == 0)
  1906. {
  1907. breakpoint_t *breakpoint = target->breakpoints;
  1908. while (breakpoint)
  1909. {
  1910. if (breakpoint->type == BKPT_SOFT)
  1911. {
  1912. char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
  1913. command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
  1914. free(buf);
  1915. }
  1916. else
  1917. {
  1918. command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
  1919. }
  1920. breakpoint = breakpoint->next;
  1921. }
  1922. }
  1923. else if (argc >= 2)
  1924. {
  1925. int hw = BKPT_SOFT;
  1926. u32 length = 0;
  1927. length = strtoul(args[1], NULL, 0);
  1928. if (argc >= 3)
  1929. if (strcmp(args[2], "hw") == 0)
  1930. hw = BKPT_HARD;
  1931. if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
  1932. {
  1933. LOG_ERROR("Failure setting breakpoints");
  1934. }
  1935. else
  1936. {
  1937. command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
  1938. }
  1939. }
  1940. else
  1941. {
  1942. command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
  1943. }
  1944. return ERROR_OK;
  1945. }
  1946. int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1947. {
  1948. target_t *target = get_current_target(cmd_ctx);
  1949. if (argc > 0)
  1950. breakpoint_remove(target, strtoul(args[0], NULL, 0));
  1951. return ERROR_OK;
  1952. }
  1953. int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1954. {
  1955. target_t *target = get_current_target(cmd_ctx);
  1956. int retval;
  1957. if (argc == 0)
  1958. {
  1959. watchpoint_t *watchpoint = target->watchpoints;
  1960. while (watchpoint)
  1961. {
  1962. command_print(cmd_ctx, "address: 0x%8.8x, len: 0x%8.8x, r/w/a: %i, value: 0x%8.8x, mask: 0x%8.8x", watchpoint->address, watchpoint->length, watchpoint->rw, watchpoint->value, watchpoint->mask);
  1963. watchpoint = watchpoint->next;
  1964. }
  1965. }
  1966. else if (argc >= 2)
  1967. {
  1968. enum watchpoint_rw type = WPT_ACCESS;
  1969. u32 data_value = 0x0;
  1970. u32 data_mask = 0xffffffff;
  1971. if (argc >= 3)
  1972. {
  1973. switch(args[2][0])
  1974. {
  1975. case 'r':
  1976. type = WPT_READ;
  1977. break;
  1978. case 'w':
  1979. type = WPT_WRITE;
  1980. break;
  1981. case 'a':
  1982. type = WPT_ACCESS;
  1983. break;
  1984. default:
  1985. command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
  1986. return ERROR_OK;
  1987. }
  1988. }
  1989. if (argc >= 4)
  1990. {
  1991. data_value = strtoul(args[3], NULL, 0);
  1992. }
  1993. if (argc >= 5)
  1994. {
  1995. data_mask = strtoul(args[4], NULL, 0);
  1996. }
  1997. if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
  1998. strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
  1999. {
  2000. LOG_ERROR("Failure setting breakpoints");
  2001. }
  2002. }
  2003. else
  2004. {
  2005. command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
  2006. }
  2007. return ERROR_OK;
  2008. }
  2009. int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2010. {
  2011. target_t *target = get_current_target(cmd_ctx);
  2012. if (argc > 0)
  2013. watchpoint_remove(target, strtoul(args[0], NULL, 0));
  2014. return ERROR_OK;
  2015. }
  2016. int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
  2017. {
  2018. int retval;
  2019. target_t *target = get_current_target(cmd_ctx);
  2020. u32 va;
  2021. u32 pa;
  2022. if (argc != 1)
  2023. {
  2024. return ERROR_COMMAND_SYNTAX_ERROR;
  2025. }
  2026. va = strtoul(args[0], NULL, 0);
  2027. retval = target->type->virt2phys(target, va, &pa);
  2028. if (retval == ERROR_OK)
  2029. {
  2030. command_print(cmd_ctx, "Physical address 0x%08x", pa);
  2031. }
  2032. else
  2033. {
  2034. /* lower levels will have logged a detailed error which is
  2035. * forwarded to telnet/GDB session.
  2036. */
  2037. }
  2038. return retval;
  2039. }
  2040. static void writeLong(FILE *f, int l)
  2041. {
  2042. int i;
  2043. for (i=0; i<4; i++)
  2044. {
  2045. char c=(l>>(i*8))&0xff;
  2046. fwrite(&c, 1, 1, f);
  2047. }
  2048. }
  2049. static void writeString(FILE *f, char *s)
  2050. {
  2051. fwrite(s, 1, strlen(s), f);
  2052. }
  2053. /* Dump a gmon.out histogram file. */
  2054. static void writeGmon(u32 *samples, int sampleNum, char *filename)
  2055. {
  2056. int i;
  2057. FILE *f=fopen(filename, "w");
  2058. if (f==NULL)
  2059. return;
  2060. fwrite("gmon", 1, 4, f);
  2061. writeLong(f, 0x00000001); /* Version */
  2062. writeLong(f, 0); /* padding */
  2063. writeLong(f, 0); /* padding */
  2064. writeLong(f, 0); /* padding */
  2065. fwrite("", 1, 1, f); /* GMON_TAG_TIME_HIST */
  2066. /* figure out bucket size */
  2067. u32 min=samples[0];
  2068. u32 max=samples[0];
  2069. for (i=0; i<sampleNum; i++)
  2070. {
  2071. if (min>samples[i])
  2072. {
  2073. min=samples[i];
  2074. }
  2075. if (max<samples[i])
  2076. {
  2077. max=samples[i];
  2078. }
  2079. }
  2080. int addressSpace=(max-min+1);
  2081. static int const maxBuckets=256*1024; /* maximum buckets. */
  2082. int length=addressSpace;
  2083. if (length > maxBuckets)
  2084. {
  2085. length=maxBuckets;
  2086. }
  2087. int *buckets=malloc(sizeof(int)*length);
  2088. if (buckets==NULL)
  2089. {
  2090. fclose(f);
  2091. return;
  2092. }
  2093. memset(buckets, 0, sizeof(int)*length);
  2094. for (i=0; i<sampleNum;i++)
  2095. {
  2096. u32 address=samples[i];
  2097. long long a=address-min;
  2098. long long b=length-1;
  2099. long long c=addressSpace-1;
  2100. int index=(a*b)/c; /* danger!!!! int32 overflows */
  2101. buckets[index]++;
  2102. }
  2103. /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
  2104. writeLong(f, min); /* low_pc */
  2105. writeLong(f, max); /* high_pc */
  2106. writeLong(f, length); /* # of samples */
  2107. writeLong(f, 64000000); /* 64MHz */
  2108. writeString(f, "seconds");
  2109. for (i=0; i<(15-strlen("seconds")); i++)
  2110. {
  2111. fwrite("", 1, 1, f); /* padding */
  2112. }
  2113. writeString(f, "s");
  2114. /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
  2115. char *data=malloc(2*length);
  2116. if (data!=NULL)
  2117. {
  2118. for (i=0; i<length;i++)
  2119. {
  2120. int val;
  2121. val=buckets[i];
  2122. if (val>65535)
  2123. {
  2124. val=65535;
  2125. }
  2126. data[i*2]=val&0xff;
  2127. data[i*2+1]=(val>>8)&0xff;
  2128. }
  2129. free(buckets);
  2130. fwrite(data, 1, length*2, f);
  2131. free(data);
  2132. } else
  2133. {
  2134. free(buckets);
  2135. }
  2136. fclose(f);
  2137. }
  2138. /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
  2139. int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2140. {
  2141. target_t *target = get_current_target(cmd_ctx);
  2142. struct timeval timeout, now;
  2143. gettimeofday(&timeout, NULL);
  2144. if (argc!=2)
  2145. {
  2146. return ERROR_COMMAND_SYNTAX_ERROR;
  2147. }
  2148. char *end;
  2149. timeval_add_time(&timeout, strtoul(args[0], &end, 0), 0);
  2150. if (*end)
  2151. {
  2152. return ERROR_OK;
  2153. }
  2154. command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
  2155. static const int maxSample=10000;
  2156. u32 *samples=malloc(sizeof(u32)*maxSample);
  2157. if (samples==NULL)
  2158. return ERROR_OK;
  2159. int numSamples=0;
  2160. int retval=ERROR_OK;
  2161. /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
  2162. reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
  2163. for (;;)
  2164. {
  2165. target_poll(target);
  2166. if (target->state == TARGET_HALTED)
  2167. {
  2168. u32 t=*((u32 *)reg->value);
  2169. samples[numSamples++]=t;
  2170. retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
  2171. target_poll(target);
  2172. alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
  2173. } else if (target->state == TARGET_RUNNING)
  2174. {
  2175. /* We want to quickly sample the PC. */
  2176. if((retval = target_halt(target)) != ERROR_OK)
  2177. {
  2178. free(samples);
  2179. return retval;
  2180. }
  2181. } else
  2182. {
  2183. command_print(cmd_ctx, "Target not halted or running");
  2184. retval=ERROR_OK;
  2185. break;
  2186. }
  2187. if (retval!=ERROR_OK)
  2188. {
  2189. break;
  2190. }
  2191. gettimeofday(&now, NULL);
  2192. if ((numSamples>=maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
  2193. {
  2194. command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
  2195. if((retval = target_poll(target)) != ERROR_OK)
  2196. {
  2197. free(samples);
  2198. return retval;
  2199. }
  2200. if (target->state == TARGET_HALTED)
  2201. {
  2202. target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
  2203. }
  2204. if((retval = target_poll(target)) != ERROR_OK)
  2205. {
  2206. free(samples);
  2207. return retval;
  2208. }
  2209. writeGmon(samples, numSamples, args[1]);
  2210. command_print(cmd_ctx, "Wrote %s", args[1]);
  2211. break;
  2212. }
  2213. }
  2214. free(samples);
  2215. return ERROR_OK;
  2216. }
  2217. static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 val)
  2218. {
  2219. char *namebuf;
  2220. Jim_Obj *nameObjPtr, *valObjPtr;
  2221. int result;
  2222. namebuf = alloc_printf("%s(%d)", varname, idx);
  2223. if (!namebuf)
  2224. return JIM_ERR;
  2225. nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
  2226. valObjPtr = Jim_NewIntObj(interp, val);
  2227. if (!nameObjPtr || !valObjPtr)
  2228. {
  2229. free(namebuf);
  2230. return JIM_ERR;
  2231. }
  2232. Jim_IncrRefCount(nameObjPtr);
  2233. Jim_IncrRefCount(valObjPtr);
  2234. result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
  2235. Jim_DecrRefCount(interp, nameObjPtr);
  2236. Jim_DecrRefCount(interp, valObjPtr);
  2237. free(namebuf);
  2238. /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
  2239. return result;
  2240. }
  2241. static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  2242. {
  2243. command_context_t *context;
  2244. target_t *target;
  2245. context = Jim_GetAssocData(interp, "context");
  2246. if (context == NULL)
  2247. {
  2248. LOG_ERROR("mem2array: no command context");
  2249. return JIM_ERR;
  2250. }
  2251. target = get_current_target(context);
  2252. if (target == NULL)
  2253. {
  2254. LOG_ERROR("mem2array: no current target");
  2255. return JIM_ERR;
  2256. }
  2257. return target_mem2array(interp, target, argc,argv);
  2258. }
  2259. static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
  2260. {
  2261. long l;
  2262. u32 width;
  2263. int len;
  2264. u32 addr;
  2265. u32 count;
  2266. u32 v;
  2267. const char *varname;
  2268. u8 buffer[4096];
  2269. int i, n, e, retval;
  2270. /* argv[1] = name of array to receive the data
  2271. * argv[2] = desired width
  2272. * argv[3] = memory address
  2273. * argv[4] = count of times to read
  2274. */
  2275. if (argc != 5) {
  2276. Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
  2277. return JIM_ERR;
  2278. }
  2279. varname = Jim_GetString(argv[1], &len);
  2280. /* given "foo" get space for worse case "foo(%d)" .. add 20 */
  2281. e = Jim_GetLong(interp, argv[2], &l);
  2282. width = l;
  2283. if (e != JIM_OK) {
  2284. return e;
  2285. }
  2286. e = Jim_GetLong(interp, argv[3], &l);
  2287. addr = l;
  2288. if (e != JIM_OK) {
  2289. return e;
  2290. }
  2291. e = Jim_GetLong(interp, argv[4], &l);
  2292. len = l;
  2293. if (e != JIM_OK) {
  2294. return e;
  2295. }
  2296. switch (width) {
  2297. case 8:
  2298. width = 1;
  2299. break;
  2300. case 16:
  2301. width = 2;
  2302. break;
  2303. case 32:
  2304. width = 4;
  2305. break;
  2306. default:
  2307. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2308. Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
  2309. return JIM_ERR;
  2310. }
  2311. if (len == 0) {
  2312. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2313. Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
  2314. return JIM_ERR;
  2315. }
  2316. if ((addr + (len * width)) < addr) {
  2317. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2318. Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
  2319. return JIM_ERR;
  2320. }
  2321. /* absurd transfer size? */
  2322. if (len > 65536) {
  2323. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2324. Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
  2325. return JIM_ERR;
  2326. }
  2327. if ((width == 1) ||
  2328. ((width == 2) && ((addr & 1) == 0)) ||
  2329. ((width == 4) && ((addr & 3) == 0))) {
  2330. /* all is well */
  2331. } else {
  2332. char buf[100];
  2333. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2334. sprintf(buf, "mem2array address: 0x%08x is not aligned for %d byte reads", addr, width);
  2335. Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
  2336. return JIM_ERR;
  2337. }
  2338. /* Transfer loop */
  2339. /* index counter */
  2340. n = 0;
  2341. /* assume ok */
  2342. e = JIM_OK;
  2343. while (len) {
  2344. /* Slurp... in buffer size chunks */
  2345. count = len; /* in objects.. */
  2346. if (count > (sizeof(buffer)/width)) {
  2347. count = (sizeof(buffer)/width);
  2348. }
  2349. retval = target->type->read_memory( target, addr, width, count, buffer );
  2350. if (retval != ERROR_OK) {
  2351. /* BOO !*/
  2352. LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
  2353. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2354. Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
  2355. e = JIM_ERR;
  2356. len = 0;
  2357. } else {
  2358. v = 0; /* shut up gcc */
  2359. for (i = 0 ;i < count ;i++, n++) {
  2360. switch (width) {
  2361. case 4:
  2362. v = target_buffer_get_u32(target, &buffer[i*width]);
  2363. break;
  2364. case 2:
  2365. v = target_buffer_get_u16(target, &buffer[i*width]);
  2366. break;
  2367. case 1:
  2368. v = buffer[i] & 0x0ff;
  2369. break;
  2370. }
  2371. new_int_array_element(interp, varname, n, v);
  2372. }
  2373. len -= count;
  2374. }
  2375. }
  2376. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2377. return JIM_OK;
  2378. }
  2379. static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 *val)
  2380. {
  2381. char *namebuf;
  2382. Jim_Obj *nameObjPtr, *valObjPtr;
  2383. int result;
  2384. long l;
  2385. namebuf = alloc_printf("%s(%d)", varname, idx);
  2386. if (!namebuf)
  2387. return JIM_ERR;
  2388. nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
  2389. if (!nameObjPtr)
  2390. {
  2391. free(namebuf);
  2392. return JIM_ERR;
  2393. }
  2394. Jim_IncrRefCount(nameObjPtr);
  2395. valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
  2396. Jim_DecrRefCount(interp, nameObjPtr);
  2397. free(namebuf);
  2398. if (valObjPtr == NULL)
  2399. return JIM_ERR;
  2400. result = Jim_GetLong(interp, valObjPtr, &l);
  2401. /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
  2402. *val = l;
  2403. return result;
  2404. }
  2405. static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  2406. {
  2407. command_context_t *context;
  2408. target_t *target;
  2409. context = Jim_GetAssocData(interp, "context");
  2410. if (context == NULL){
  2411. LOG_ERROR("array2mem: no command context");
  2412. return JIM_ERR;
  2413. }
  2414. target = get_current_target(context);
  2415. if (target == NULL){
  2416. LOG_ERROR("array2mem: no current target");
  2417. return JIM_ERR;
  2418. }
  2419. return target_array2mem( interp,target, argc, argv );
  2420. }
  2421. static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
  2422. {
  2423. long l;
  2424. u32 width;
  2425. int len;
  2426. u32 addr;
  2427. u32 count;
  2428. u32 v;
  2429. const char *varname;
  2430. u8 buffer[4096];
  2431. int i, n, e, retval;
  2432. /* argv[1] = name of array to get the data
  2433. * argv[2] = desired width
  2434. * argv[3] = memory address
  2435. * argv[4] = count to write
  2436. */
  2437. if (argc != 5) {
  2438. Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
  2439. return JIM_ERR;
  2440. }
  2441. varname = Jim_GetString(argv[1], &len);
  2442. /* given "foo" get space for worse case "foo(%d)" .. add 20 */
  2443. e = Jim_GetLong(interp, argv[2], &l);
  2444. width = l;
  2445. if (e != JIM_OK) {
  2446. return e;
  2447. }
  2448. e = Jim_GetLong(interp, argv[3], &l);
  2449. addr = l;
  2450. if (e != JIM_OK) {
  2451. return e;
  2452. }
  2453. e = Jim_GetLong(interp, argv[4], &l);
  2454. len = l;
  2455. if (e != JIM_OK) {
  2456. return e;
  2457. }
  2458. switch (width) {
  2459. case 8:
  2460. width = 1;
  2461. break;
  2462. case 16:
  2463. width = 2;
  2464. break;
  2465. case 32:
  2466. width = 4;
  2467. break;
  2468. default:
  2469. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2470. Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
  2471. return JIM_ERR;
  2472. }
  2473. if (len == 0) {
  2474. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2475. Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
  2476. return JIM_ERR;
  2477. }
  2478. if ((addr + (len * width)) < addr) {
  2479. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2480. Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
  2481. return JIM_ERR;
  2482. }
  2483. /* absurd transfer size? */
  2484. if (len > 65536) {
  2485. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2486. Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
  2487. return JIM_ERR;
  2488. }
  2489. if ((width == 1) ||
  2490. ((width == 2) && ((addr & 1) == 0)) ||
  2491. ((width == 4) && ((addr & 3) == 0))) {
  2492. /* all is well */
  2493. } else {
  2494. char buf[100];
  2495. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2496. sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads", addr, width);
  2497. Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
  2498. return JIM_ERR;
  2499. }
  2500. /* Transfer loop */
  2501. /* index counter */
  2502. n = 0;
  2503. /* assume ok */
  2504. e = JIM_OK;
  2505. while (len) {
  2506. /* Slurp... in buffer size chunks */
  2507. count = len; /* in objects.. */
  2508. if (count > (sizeof(buffer)/width)) {
  2509. count = (sizeof(buffer)/width);
  2510. }
  2511. v = 0; /* shut up gcc */
  2512. for (i = 0 ;i < count ;i++, n++) {
  2513. get_int_array_element(interp, varname, n, &v);
  2514. switch (width) {
  2515. case 4:
  2516. target_buffer_set_u32(target, &buffer[i*width], v);
  2517. break;
  2518. case 2:
  2519. target_buffer_set_u16(target, &buffer[i*width], v);
  2520. break;
  2521. case 1:
  2522. buffer[i] = v & 0x0ff;
  2523. break;
  2524. }
  2525. }
  2526. len -= count;
  2527. retval = target->type->write_memory(target, addr, width, count, buffer);
  2528. if (retval != ERROR_OK) {
  2529. /* BOO !*/
  2530. LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
  2531. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2532. Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
  2533. e = JIM_ERR;
  2534. len = 0;
  2535. }
  2536. }
  2537. Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
  2538. return JIM_OK;
  2539. }
  2540. void target_all_handle_event( enum target_event e )
  2541. {
  2542. target_t *target;
  2543. LOG_DEBUG( "**all*targets: event: %d, %s",
  2544. e,
  2545. Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
  2546. target = all_targets;
  2547. while (target){
  2548. target_handle_event( target, e );
  2549. target = target->next;
  2550. }
  2551. }
  2552. void target_handle_event( target_t *target, enum target_event e )
  2553. {
  2554. target_event_action_t *teap;
  2555. int done;
  2556. teap = target->event_action;
  2557. done = 0;
  2558. while( teap ){
  2559. if( teap->event == e ){
  2560. done = 1;
  2561. LOG_DEBUG( "target: (%d) %s (%s) event: %d (%s) action: %s\n",
  2562. target->target_number,
  2563. target->cmd_name,
  2564. target->type->name,
  2565. e,
  2566. Jim_Nvp_value2name_simple( nvp_target_event, e )->name,
  2567. Jim_GetString( teap->body, NULL ) );
  2568. if (Jim_EvalObj( interp, teap->body )!=JIM_OK)
  2569. {
  2570. Jim_PrintErrorMessage(interp);
  2571. }
  2572. }
  2573. teap = teap->next;
  2574. }
  2575. if( !done ){
  2576. LOG_DEBUG( "event: %d %s - no action",
  2577. e,
  2578. Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
  2579. }
  2580. }
  2581. enum target_cfg_param {
  2582. TCFG_TYPE,
  2583. TCFG_EVENT,
  2584. TCFG_WORK_AREA_VIRT,
  2585. TCFG_WORK_AREA_PHYS,
  2586. TCFG_WORK_AREA_SIZE,
  2587. TCFG_WORK_AREA_BACKUP,
  2588. TCFG_ENDIAN,
  2589. TCFG_VARIANT,
  2590. TCFG_CHAIN_POSITION,
  2591. };
  2592. static Jim_Nvp nvp_config_opts[] = {
  2593. { .name = "-type", .value = TCFG_TYPE },
  2594. { .name = "-event", .value = TCFG_EVENT },
  2595. { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
  2596. { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
  2597. { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
  2598. { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
  2599. { .name = "-endian" , .value = TCFG_ENDIAN },
  2600. { .name = "-variant", .value = TCFG_VARIANT },
  2601. { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
  2602. { .name = NULL, .value = -1 }
  2603. };
  2604. static int target_configure( Jim_GetOptInfo *goi, target_t *target )
  2605. {
  2606. Jim_Nvp *n;
  2607. Jim_Obj *o;
  2608. jim_wide w;
  2609. char *cp;
  2610. int e;
  2611. /* parse config or cget options ... */
  2612. while( goi->argc > 0 ){
  2613. Jim_SetEmptyResult( goi->interp );
  2614. /* Jim_GetOpt_Debug( goi ); */
  2615. if( target->type->target_jim_configure ){
  2616. /* target defines a configure function */
  2617. /* target gets first dibs on parameters */
  2618. e = (*(target->type->target_jim_configure))( target, goi );
  2619. if( e == JIM_OK ){
  2620. /* more? */
  2621. continue;
  2622. }
  2623. if( e == JIM_ERR ){
  2624. /* An error */
  2625. return e;
  2626. }
  2627. /* otherwise we 'continue' below */
  2628. }
  2629. e = Jim_GetOpt_Nvp( goi, nvp_config_opts, &n );
  2630. if( e != JIM_OK ){
  2631. Jim_GetOpt_NvpUnknown( goi, nvp_config_opts, 0 );
  2632. return e;
  2633. }
  2634. switch( n->value ){
  2635. case TCFG_TYPE:
  2636. /* not setable */
  2637. if( goi->isconfigure ){
  2638. Jim_SetResult_sprintf( goi->interp, "not setable: %s", n->name );
  2639. return JIM_ERR;
  2640. } else {
  2641. no_params:
  2642. if( goi->argc != 0 ){
  2643. Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "NO PARAMS");
  2644. return JIM_ERR;
  2645. }
  2646. }
  2647. Jim_SetResultString( goi->interp, target->type->name, -1 );
  2648. /* loop for more */
  2649. break;
  2650. case TCFG_EVENT:
  2651. if( goi->argc == 0 ){
  2652. Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
  2653. return JIM_ERR;
  2654. }
  2655. e = Jim_GetOpt_Nvp( goi, nvp_target_event, &n );
  2656. if( e != JIM_OK ){
  2657. Jim_GetOpt_NvpUnknown( goi, nvp_target_event, 1 );
  2658. return e;
  2659. }
  2660. if( goi->isconfigure ){
  2661. if( goi->argc != 1 ){
  2662. Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
  2663. return JIM_ERR;
  2664. }
  2665. } else {
  2666. if( goi->argc != 0 ){
  2667. Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
  2668. return JIM_ERR;
  2669. }
  2670. }
  2671. {
  2672. target_event_action_t *teap;
  2673. teap = target->event_action;
  2674. /* replace existing? */
  2675. while( teap ){
  2676. if( teap->event == n->value ){
  2677. break;
  2678. }
  2679. teap = teap->next;
  2680. }
  2681. if( goi->isconfigure ){
  2682. if( teap == NULL ){
  2683. /* create new */
  2684. teap = calloc( 1, sizeof(*teap) );
  2685. }
  2686. teap->event = n->value;
  2687. Jim_GetOpt_Obj( goi, &o );
  2688. if( teap->body ){
  2689. Jim_DecrRefCount( interp, teap->body );
  2690. }
  2691. teap->body = Jim_DuplicateObj( goi->interp, o );
  2692. /*
  2693. * FIXME:
  2694. * Tcl/TK - "tk events" have a nice feature.
  2695. * See the "BIND" command.
  2696. * We should support that here.
  2697. * You can specify %X and %Y in the event code.
  2698. * The idea is: %T - target name.
  2699. * The idea is: %N - target number
  2700. * The idea is: %E - event name.
  2701. */
  2702. Jim_IncrRefCount( teap->body );
  2703. /* add to head of event list */
  2704. teap->next = target->event_action;
  2705. target->event_action = teap;
  2706. Jim_SetEmptyResult(goi->interp);
  2707. } else {
  2708. /* get */
  2709. if( teap == NULL ){
  2710. Jim_SetEmptyResult( goi->interp );
  2711. } else {
  2712. Jim_SetResult( goi->interp, Jim_DuplicateObj( goi->interp, teap->body ) );
  2713. }
  2714. }
  2715. }
  2716. /* loop for more */
  2717. break;
  2718. case TCFG_WORK_AREA_VIRT:
  2719. if( goi->isconfigure ){
  2720. target_free_all_working_areas(target);
  2721. e = Jim_GetOpt_Wide( goi, &w );
  2722. if( e != JIM_OK ){
  2723. return e;
  2724. }
  2725. target->working_area_virt = w;
  2726. } else {
  2727. if( goi->argc != 0 ){
  2728. goto no_params;
  2729. }
  2730. }
  2731. Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_virt ) );
  2732. /* loop for more */
  2733. break;
  2734. case TCFG_WORK_AREA_PHYS:
  2735. if( goi->isconfigure ){
  2736. target_free_all_working_areas(target);
  2737. e = Jim_GetOpt_Wide( goi, &w );
  2738. if( e != JIM_OK ){
  2739. return e;
  2740. }
  2741. target->working_area_phys = w;
  2742. } else {
  2743. if( goi->argc != 0 ){
  2744. goto no_params;
  2745. }
  2746. }
  2747. Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_phys ) );
  2748. /* loop for more */
  2749. break;
  2750. case TCFG_WORK_AREA_SIZE:
  2751. if( goi->isconfigure ){
  2752. target_free_all_working_areas(target);
  2753. e = Jim_GetOpt_Wide( goi, &w );
  2754. if( e != JIM_OK ){
  2755. return e;
  2756. }
  2757. target->working_area_size = w;
  2758. } else {
  2759. if( goi->argc != 0 ){
  2760. goto no_params;
  2761. }
  2762. }
  2763. Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
  2764. /* loop for more */
  2765. break;
  2766. case TCFG_WORK_AREA_BACKUP:
  2767. if( goi->isconfigure ){
  2768. target_free_all_working_areas(target);
  2769. e = Jim_GetOpt_Wide( goi, &w );
  2770. if( e != JIM_OK ){
  2771. return e;
  2772. }
  2773. /* make this exactly 1 or 0 */
  2774. target->backup_working_area = (!!w);
  2775. } else {
  2776. if( goi->argc != 0 ){
  2777. goto no_params;
  2778. }
  2779. }
  2780. Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
  2781. /* loop for more e*/
  2782. break;
  2783. case TCFG_ENDIAN:
  2784. if( goi->isconfigure ){
  2785. e = Jim_GetOpt_Nvp( goi, nvp_target_endian, &n );
  2786. if( e != JIM_OK ){
  2787. Jim_GetOpt_NvpUnknown( goi, nvp_target_endian, 1 );
  2788. return e;
  2789. }
  2790. target->endianness = n->value;
  2791. } else {
  2792. if( goi->argc != 0 ){
  2793. goto no_params;
  2794. }
  2795. }
  2796. n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
  2797. if( n->name == NULL ){
  2798. target->endianness = TARGET_LITTLE_ENDIAN;
  2799. n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
  2800. }
  2801. Jim_SetResultString( goi->interp, n->name, -1 );
  2802. /* loop for more */
  2803. break;
  2804. case TCFG_VARIANT:
  2805. if( goi->isconfigure ){
  2806. if( goi->argc < 1 ){
  2807. Jim_SetResult_sprintf( goi->interp,
  2808. "%s ?STRING?",
  2809. n->name );
  2810. return JIM_ERR;
  2811. }
  2812. if( target->variant ){
  2813. free((void *)(target->variant));
  2814. }
  2815. e = Jim_GetOpt_String( goi, &cp, NULL );
  2816. target->variant = strdup(cp);
  2817. } else {
  2818. if( goi->argc != 0 ){
  2819. goto no_params;
  2820. }
  2821. }
  2822. Jim_SetResultString( goi->interp, target->variant,-1 );
  2823. /* loop for more */
  2824. break;
  2825. case TCFG_CHAIN_POSITION:
  2826. if( goi->isconfigure ){
  2827. Jim_Obj *o;
  2828. jtag_tap_t *tap;
  2829. target_free_all_working_areas(target);
  2830. e = Jim_GetOpt_Obj( goi, &o );
  2831. if( e != JIM_OK ){
  2832. return e;
  2833. }
  2834. tap = jtag_TapByJimObj( goi->interp, o );
  2835. if( tap == NULL ){
  2836. return JIM_ERR;
  2837. }
  2838. /* make this exactly 1 or 0 */
  2839. target->tap = tap;
  2840. } else {
  2841. if( goi->argc != 0 ){
  2842. goto no_params;
  2843. }
  2844. }
  2845. Jim_SetResultString( interp, target->tap->dotted_name, -1 );
  2846. /* loop for more e*/
  2847. break;
  2848. }
  2849. } /* while( goi->argc ) */
  2850. /* done - we return */
  2851. return JIM_OK;
  2852. }
  2853. /** this is the 'tcl' handler for the target specific command */
  2854. static int tcl_target_func( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
  2855. {
  2856. Jim_GetOptInfo goi;
  2857. jim_wide a,b,c;
  2858. int x,y,z;
  2859. u8 target_buf[32];
  2860. Jim_Nvp *n;
  2861. target_t *target;
  2862. struct command_context_s *cmd_ctx;
  2863. int e;
  2864. enum {
  2865. TS_CMD_CONFIGURE,
  2866. TS_CMD_CGET,
  2867. TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
  2868. TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
  2869. TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
  2870. TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
  2871. TS_CMD_EXAMINE,
  2872. TS_CMD_POLL,
  2873. TS_CMD_RESET,
  2874. TS_CMD_HALT,
  2875. TS_CMD_WAITSTATE,
  2876. TS_CMD_EVENTLIST,
  2877. TS_CMD_CURSTATE,
  2878. TS_CMD_INVOKE_EVENT,
  2879. };
  2880. static const Jim_Nvp target_options[] = {
  2881. { .name = "configure", .value = TS_CMD_CONFIGURE },
  2882. { .name = "cget", .value = TS_CMD_CGET },
  2883. { .name = "mww", .value = TS_CMD_MWW },
  2884. { .name = "mwh", .value = TS_CMD_MWH },
  2885. { .name = "mwb", .value = TS_CMD_MWB },
  2886. { .name = "mdw", .value = TS_CMD_MDW },
  2887. { .name = "mdh", .value = TS_CMD_MDH },
  2888. { .name = "mdb", .value = TS_CMD_MDB },
  2889. { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
  2890. { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
  2891. { .name = "eventlist", .value = TS_CMD_EVENTLIST },
  2892. { .name = "curstate", .value = TS_CMD_CURSTATE },
  2893. { .name = "arp_examine", .value = TS_CMD_EXAMINE },
  2894. { .name = "arp_poll", .value = TS_CMD_POLL },
  2895. { .name = "arp_reset", .value = TS_CMD_RESET },
  2896. { .name = "arp_halt", .value = TS_CMD_HALT },
  2897. { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
  2898. { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
  2899. { .name = NULL, .value = -1 },
  2900. };
  2901. /* go past the "command" */
  2902. Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
  2903. target = Jim_CmdPrivData( goi.interp );
  2904. cmd_ctx = Jim_GetAssocData(goi.interp, "context");
  2905. /* commands here are in an NVP table */
  2906. e = Jim_GetOpt_Nvp( &goi, target_options, &n );
  2907. if( e != JIM_OK ){
  2908. Jim_GetOpt_NvpUnknown( &goi, target_options, 0 );
  2909. return e;
  2910. }
  2911. /* Assume blank result */
  2912. Jim_SetEmptyResult( goi.interp );
  2913. switch( n->value ){
  2914. case TS_CMD_CONFIGURE:
  2915. if( goi.argc < 2 ){
  2916. Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
  2917. return JIM_ERR;
  2918. }
  2919. goi.isconfigure = 1;
  2920. return target_configure( &goi, target );
  2921. case TS_CMD_CGET:
  2922. // some things take params
  2923. if( goi.argc < 1 ){
  2924. Jim_WrongNumArgs( goi.interp, 0, goi.argv, "missing: ?-option?");
  2925. return JIM_ERR;
  2926. }
  2927. goi.isconfigure = 0;
  2928. return target_configure( &goi, target );
  2929. break;
  2930. case TS_CMD_MWW:
  2931. case TS_CMD_MWH:
  2932. case TS_CMD_MWB:
  2933. /* argv[0] = cmd
  2934. * argv[1] = address
  2935. * argv[2] = data
  2936. * argv[3] = optional count.
  2937. */
  2938. if( (goi.argc == 3) || (goi.argc == 4) ){
  2939. /* all is well */
  2940. } else {
  2941. mwx_error:
  2942. Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR DATA [COUNT]", n->name );
  2943. return JIM_ERR;
  2944. }
  2945. e = Jim_GetOpt_Wide( &goi, &a );
  2946. if( e != JIM_OK ){
  2947. goto mwx_error;
  2948. }
  2949. e = Jim_GetOpt_Wide( &goi, &b );
  2950. if( e != JIM_OK ){
  2951. goto mwx_error;
  2952. }
  2953. if( goi.argc ){
  2954. e = Jim_GetOpt_Wide( &goi, &c );
  2955. if( e != JIM_OK ){
  2956. goto mwx_error;
  2957. }
  2958. } else {
  2959. c = 1;
  2960. }
  2961. switch( n->value ){
  2962. case TS_CMD_MWW:
  2963. target_buffer_set_u32( target, target_buf, b );
  2964. b = 4;
  2965. break;
  2966. case TS_CMD_MWH:
  2967. target_buffer_set_u16( target, target_buf, b );
  2968. b = 2;
  2969. break;
  2970. case TS_CMD_MWB:
  2971. target_buffer_set_u8( target, target_buf, b );
  2972. b = 1;
  2973. break;
  2974. }
  2975. for( x = 0 ; x < c ; x++ ){
  2976. e = target->type->write_memory( target, a, b, 1, target_buf );
  2977. if( e != ERROR_OK ){
  2978. Jim_SetResult_sprintf( interp, "Error writing @ 0x%08x: %d\n", (int)(a), e );
  2979. return JIM_ERR;
  2980. }
  2981. /* b = width */
  2982. a = a + b;
  2983. }
  2984. return JIM_OK;
  2985. break;
  2986. /* display */
  2987. case TS_CMD_MDW:
  2988. case TS_CMD_MDH:
  2989. case TS_CMD_MDB:
  2990. /* argv[0] = command
  2991. * argv[1] = address
  2992. * argv[2] = optional count
  2993. */
  2994. if( (goi.argc == 2) || (goi.argc == 3) ){
  2995. Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR [COUNT]", n->name );
  2996. return JIM_ERR;
  2997. }
  2998. e = Jim_GetOpt_Wide( &goi, &a );
  2999. if( e != JIM_OK ){
  3000. return JIM_ERR;
  3001. }
  3002. if( goi.argc ){
  3003. e = Jim_GetOpt_Wide( &goi, &c );
  3004. if( e != JIM_OK ){
  3005. return JIM_ERR;
  3006. }
  3007. } else {
  3008. c = 1;
  3009. }
  3010. b = 1; /* shut up gcc */
  3011. switch( n->value ){
  3012. case TS_CMD_MDW:
  3013. b = 4;
  3014. break;
  3015. case TS_CMD_MDH:
  3016. b = 2;
  3017. break;
  3018. case TS_CMD_MDB:
  3019. b = 1;
  3020. break;
  3021. }
  3022. /* convert to "bytes" */
  3023. c = c * b;
  3024. /* count is now in 'BYTES' */
  3025. while( c > 0 ){
  3026. y = c;
  3027. if( y > 16 ){
  3028. y = 16;
  3029. }
  3030. e = target->type->read_memory( target, a, b, y / b, target_buf );
  3031. if( e != ERROR_OK ){
  3032. Jim_SetResult_sprintf( interp, "error reading target @ 0x%08lx", (int)(a) );
  3033. return JIM_ERR;
  3034. }
  3035. Jim_fprintf( interp, interp->cookie_stdout, "0x%08x ", (int)(a) );
  3036. switch( b ){
  3037. case 4:
  3038. for( x = 0 ; (x < 16) && (x < y) ; x += 4 ){
  3039. z = target_buffer_get_u32( target, &(target_buf[ x * 4 ]) );
  3040. Jim_fprintf( interp, interp->cookie_stdout, "%08x ", (int)(z) );
  3041. }
  3042. for( ; (x < 16) ; x += 4 ){
  3043. Jim_fprintf( interp, interp->cookie_stdout, " " );
  3044. }
  3045. break;
  3046. case 2:
  3047. for( x = 0 ; (x < 16) && (x < y) ; x += 2 ){
  3048. z = target_buffer_get_u16( target, &(target_buf[ x * 2 ]) );
  3049. Jim_fprintf( interp, interp->cookie_stdout, "%04x ", (int)(z) );
  3050. }
  3051. for( ; (x < 16) ; x += 2 ){
  3052. Jim_fprintf( interp, interp->cookie_stdout, " " );
  3053. }
  3054. break;
  3055. case 1:
  3056. default:
  3057. for( x = 0 ; (x < 16) && (x < y) ; x += 1 ){
  3058. z = target_buffer_get_u8( target, &(target_buf[ x * 4 ]) );
  3059. Jim_fprintf( interp, interp->cookie_stdout, "%02x ", (int)(z) );
  3060. }
  3061. for( ; (x < 16) ; x += 1 ){
  3062. Jim_fprintf( interp, interp->cookie_stdout, " " );
  3063. }
  3064. break;
  3065. }
  3066. /* ascii-ify the bytes */
  3067. for( x = 0 ; x < y ; x++ ){
  3068. if( (target_buf[x] >= 0x20) &&
  3069. (target_buf[x] <= 0x7e) ){
  3070. /* good */
  3071. } else {
  3072. /* smack it */
  3073. target_buf[x] = '.';
  3074. }
  3075. }
  3076. /* space pad */
  3077. while( x < 16 ){
  3078. target_buf[x] = ' ';
  3079. x++;
  3080. }
  3081. /* terminate */
  3082. target_buf[16] = 0;
  3083. /* print - with a newline */
  3084. Jim_fprintf( interp, interp->cookie_stdout, "%s\n", target_buf );
  3085. /* NEXT... */
  3086. c -= 16;
  3087. a += 16;
  3088. }
  3089. return JIM_OK;
  3090. case TS_CMD_MEM2ARRAY:
  3091. return target_mem2array( goi.interp, target, goi.argc, goi.argv );
  3092. break;
  3093. case TS_CMD_ARRAY2MEM:
  3094. return target_array2mem( goi.interp, target, goi.argc, goi.argv );
  3095. break;
  3096. case TS_CMD_EXAMINE:
  3097. if( goi.argc ){
  3098. Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
  3099. return JIM_ERR;
  3100. }
  3101. e = target->type->examine( target );
  3102. if( e != ERROR_OK ){
  3103. Jim_SetResult_sprintf( interp, "examine-fails: %d", e );
  3104. return JIM_ERR;
  3105. }
  3106. return JIM_OK;
  3107. case TS_CMD_POLL:
  3108. if( goi.argc ){
  3109. Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
  3110. return JIM_ERR;
  3111. }
  3112. if( !(target->type->examined) ){
  3113. e = ERROR_TARGET_NOT_EXAMINED;
  3114. } else {
  3115. e = target->type->poll( target );
  3116. }
  3117. if( e != ERROR_OK ){
  3118. Jim_SetResult_sprintf( interp, "poll-fails: %d", e );
  3119. return JIM_ERR;
  3120. } else {
  3121. return JIM_OK;
  3122. }
  3123. break;
  3124. case TS_CMD_RESET:
  3125. if( goi.argc != 2 ){
  3126. Jim_WrongNumArgs( interp, 2, argv, "t|f|assert|deassert BOOL");
  3127. return JIM_ERR;
  3128. }
  3129. e = Jim_GetOpt_Nvp( &goi, nvp_assert, &n );
  3130. if( e != JIM_OK ){
  3131. Jim_GetOpt_NvpUnknown( &goi, nvp_assert, 1 );
  3132. return e;
  3133. }
  3134. /* the halt or not param */
  3135. e = Jim_GetOpt_Wide( &goi, &a);
  3136. if( e != JIM_OK ){
  3137. return e;
  3138. }
  3139. /* determine if we should halt or not. */
  3140. target->reset_halt = !!a;
  3141. /* When this happens - all workareas are invalid. */
  3142. target_free_all_working_areas_restore(target, 0);
  3143. /* do the assert */
  3144. if( n->value == NVP_ASSERT ){
  3145. target->type->assert_reset( target );
  3146. } else {
  3147. target->type->deassert_reset( target );
  3148. }
  3149. return JIM_OK;
  3150. case TS_CMD_HALT:
  3151. if( goi.argc ){
  3152. Jim_WrongNumArgs( goi.interp, 0, argv, "halt [no parameters]");
  3153. return JIM_ERR;
  3154. }
  3155. target->type->halt( target );
  3156. return JIM_OK;
  3157. case TS_CMD_WAITSTATE:
  3158. /* params: <name> statename timeoutmsecs */
  3159. if( goi.argc != 2 ){
  3160. Jim_SetResult_sprintf( goi.interp, "%s STATENAME TIMEOUTMSECS", n->name );
  3161. return JIM_ERR;
  3162. }
  3163. e = Jim_GetOpt_Nvp( &goi, nvp_target_state, &n );
  3164. if( e != JIM_OK ){
  3165. Jim_GetOpt_NvpUnknown( &goi, nvp_target_state,1 );
  3166. return e;
  3167. }
  3168. e = Jim_GetOpt_Wide( &goi, &a );
  3169. if( e != JIM_OK ){
  3170. return e;
  3171. }
  3172. e = target_wait_state( target, n->value, a );
  3173. if( e != ERROR_OK ){
  3174. Jim_SetResult_sprintf( goi.interp,
  3175. "target: %s wait %s fails (%d) %s",
  3176. target->cmd_name,
  3177. n->name,
  3178. e, target_strerror_safe(e) );
  3179. return JIM_ERR;
  3180. } else {
  3181. return JIM_OK;
  3182. }
  3183. case TS_CMD_EVENTLIST:
  3184. /* List for human, Events defined for this target.
  3185. * scripts/programs should use 'name cget -event NAME'
  3186. */
  3187. {
  3188. target_event_action_t *teap;
  3189. teap = target->event_action;
  3190. command_print( cmd_ctx, "Event actions for target (%d) %s\n",
  3191. target->target_number,
  3192. target->cmd_name );
  3193. command_print( cmd_ctx, "%-25s | Body", "Event");
  3194. command_print( cmd_ctx, "------------------------- | ----------------------------------------");
  3195. while( teap ){
  3196. command_print( cmd_ctx,
  3197. "%-25s | %s",
  3198. Jim_Nvp_value2name_simple( nvp_target_event, teap->event )->name,
  3199. Jim_GetString( teap->body, NULL ) );
  3200. teap = teap->next;
  3201. }
  3202. command_print( cmd_ctx, "***END***");
  3203. return JIM_OK;
  3204. }
  3205. case TS_CMD_CURSTATE:
  3206. if( goi.argc != 0 ){
  3207. Jim_WrongNumArgs( goi.interp, 0, argv, "[no parameters]");
  3208. return JIM_ERR;
  3209. }
  3210. Jim_SetResultString( goi.interp,
  3211. Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name,-1);
  3212. return JIM_OK;
  3213. case TS_CMD_INVOKE_EVENT:
  3214. if( goi.argc != 1 ){
  3215. Jim_SetResult_sprintf( goi.interp, "%s ?EVENTNAME?",n->name);
  3216. return JIM_ERR;
  3217. }
  3218. e = Jim_GetOpt_Nvp( &goi, nvp_target_event, &n );
  3219. if( e != JIM_OK ){
  3220. Jim_GetOpt_NvpUnknown( &goi, nvp_target_event, 1 );
  3221. return e;
  3222. }
  3223. target_handle_event( target, n->value );
  3224. return JIM_OK;
  3225. }
  3226. return JIM_ERR;
  3227. }
  3228. static int target_create( Jim_GetOptInfo *goi )
  3229. {
  3230. Jim_Obj *new_cmd;
  3231. Jim_Cmd *cmd;
  3232. const char *cp;
  3233. char *cp2;
  3234. int e;
  3235. int x;
  3236. target_t *target;
  3237. struct command_context_s *cmd_ctx;
  3238. cmd_ctx = Jim_GetAssocData(goi->interp, "context");
  3239. if( goi->argc < 3 ){
  3240. Jim_WrongNumArgs( goi->interp, 1, goi->argv, "?name? ?type? ..options...");
  3241. return JIM_ERR;
  3242. }
  3243. /* COMMAND */
  3244. Jim_GetOpt_Obj( goi, &new_cmd );
  3245. /* does this command exist? */
  3246. cmd = Jim_GetCommand( goi->interp, new_cmd, JIM_ERRMSG );
  3247. if( cmd ){
  3248. cp = Jim_GetString( new_cmd, NULL );
  3249. Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
  3250. return JIM_ERR;
  3251. }
  3252. /* TYPE */
  3253. e = Jim_GetOpt_String( goi, &cp2, NULL );
  3254. cp = cp2;
  3255. /* now does target type exist */
  3256. for( x = 0 ; target_types[x] ; x++ ){
  3257. if( 0 == strcmp( cp, target_types[x]->name ) ){
  3258. /* found */
  3259. break;
  3260. }
  3261. }
  3262. if( target_types[x] == NULL ){
  3263. Jim_SetResult_sprintf( goi->interp, "Unknown target type %s, try one of ", cp );
  3264. for( x = 0 ; target_types[x] ; x++ ){
  3265. if( target_types[x+1] ){
  3266. Jim_AppendStrings( goi->interp,
  3267. Jim_GetResult(goi->interp),
  3268. target_types[x]->name,
  3269. ", ", NULL);
  3270. } else {
  3271. Jim_AppendStrings( goi->interp,
  3272. Jim_GetResult(goi->interp),
  3273. " or ",
  3274. target_types[x]->name,NULL );
  3275. }
  3276. }
  3277. return JIM_ERR;
  3278. }
  3279. /* Create it */
  3280. target = calloc(1,sizeof(target_t));
  3281. /* set target number */
  3282. target->target_number = new_target_number();
  3283. /* allocate memory for each unique target type */
  3284. target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
  3285. memcpy( target->type, target_types[x], sizeof(target_type_t));
  3286. /* will be set by "-endian" */
  3287. target->endianness = TARGET_ENDIAN_UNKNOWN;
  3288. target->working_area = 0x0;
  3289. target->working_area_size = 0x0;
  3290. target->working_areas = NULL;
  3291. target->backup_working_area = 0;
  3292. target->state = TARGET_UNKNOWN;
  3293. target->debug_reason = DBG_REASON_UNDEFINED;
  3294. target->reg_cache = NULL;
  3295. target->breakpoints = NULL;
  3296. target->watchpoints = NULL;
  3297. target->next = NULL;
  3298. target->arch_info = NULL;
  3299. target->display = 1;
  3300. /* initialize trace information */
  3301. target->trace_info = malloc(sizeof(trace_t));
  3302. target->trace_info->num_trace_points = 0;
  3303. target->trace_info->trace_points_size = 0;
  3304. target->trace_info->trace_points = NULL;
  3305. target->trace_info->trace_history_size = 0;
  3306. target->trace_info->trace_history = NULL;
  3307. target->trace_info->trace_history_pos = 0;
  3308. target->trace_info->trace_history_overflowed = 0;
  3309. target->dbgmsg = NULL;
  3310. target->dbg_msg_enabled = 0;
  3311. target->endianness = TARGET_ENDIAN_UNKNOWN;
  3312. /* Do the rest as "configure" options */
  3313. goi->isconfigure = 1;
  3314. e = target_configure( goi, target);
  3315. if (target->tap == NULL)
  3316. {
  3317. Jim_SetResultString( interp, "-chain-position required when creating target", -1);
  3318. e=JIM_ERR;
  3319. }
  3320. if( e != JIM_OK ){
  3321. free( target->type );
  3322. free( target );
  3323. return e;
  3324. }
  3325. if( target->endianness == TARGET_ENDIAN_UNKNOWN ){
  3326. /* default endian to little if not specified */
  3327. target->endianness = TARGET_LITTLE_ENDIAN;
  3328. }
  3329. /* incase variant is not set */
  3330. if (!target->variant)
  3331. target->variant = strdup("");
  3332. /* create the target specific commands */
  3333. if( target->type->register_commands ){
  3334. (*(target->type->register_commands))( cmd_ctx );
  3335. }
  3336. if( target->type->target_create ){
  3337. (*(target->type->target_create))( target, goi->interp );
  3338. }
  3339. /* append to end of list */
  3340. {
  3341. target_t **tpp;
  3342. tpp = &(all_targets);
  3343. while( *tpp ){
  3344. tpp = &( (*tpp)->next );
  3345. }
  3346. *tpp = target;
  3347. }
  3348. cp = Jim_GetString( new_cmd, NULL );
  3349. target->cmd_name = strdup(cp);
  3350. /* now - create the new target name command */
  3351. e = Jim_CreateCommand( goi->interp,
  3352. /* name */
  3353. cp,
  3354. tcl_target_func, /* C function */
  3355. target, /* private data */
  3356. NULL ); /* no del proc */
  3357. return e;
  3358. }
  3359. static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
  3360. {
  3361. int x,r,e;
  3362. jim_wide w;
  3363. struct command_context_s *cmd_ctx;
  3364. target_t *target;
  3365. Jim_GetOptInfo goi;
  3366. enum tcmd {
  3367. /* TG = target generic */
  3368. TG_CMD_CREATE,
  3369. TG_CMD_TYPES,
  3370. TG_CMD_NAMES,
  3371. TG_CMD_CURRENT,
  3372. TG_CMD_NUMBER,
  3373. TG_CMD_COUNT,
  3374. };
  3375. const char *target_cmds[] = {
  3376. "create", "types", "names", "current", "number",
  3377. "count",
  3378. NULL /* terminate */
  3379. };
  3380. LOG_DEBUG("Target command params:");
  3381. LOG_DEBUG(Jim_Debug_ArgvString( interp, argc, argv) );
  3382. cmd_ctx = Jim_GetAssocData( interp, "context" );
  3383. Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
  3384. if( goi.argc == 0 ){
  3385. Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
  3386. return JIM_ERR;
  3387. }
  3388. /* Jim_GetOpt_Debug( &goi ); */
  3389. r = Jim_GetOpt_Enum( &goi, target_cmds, &x );
  3390. if( r != JIM_OK ){
  3391. return r;
  3392. }
  3393. switch(x){
  3394. default:
  3395. Jim_Panic(goi.interp,"Why am I here?");
  3396. return JIM_ERR;
  3397. case TG_CMD_CURRENT:
  3398. if( goi.argc != 0 ){
  3399. Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters");
  3400. return JIM_ERR;
  3401. }
  3402. Jim_SetResultString( goi.interp, get_current_target( cmd_ctx )->cmd_name, -1 );
  3403. return JIM_OK;
  3404. case TG_CMD_TYPES:
  3405. if( goi.argc != 0 ){
  3406. Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
  3407. return JIM_ERR;
  3408. }
  3409. Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
  3410. for( x = 0 ; target_types[x] ; x++ ){
  3411. Jim_ListAppendElement( goi.interp,
  3412. Jim_GetResult(goi.interp),
  3413. Jim_NewStringObj( goi.interp, target_types[x]->name, -1 ) );
  3414. }
  3415. return JIM_OK;
  3416. case TG_CMD_NAMES:
  3417. if( goi.argc != 0 ){
  3418. Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
  3419. return JIM_ERR;
  3420. }
  3421. Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
  3422. target = all_targets;
  3423. while( target ){
  3424. Jim_ListAppendElement( goi.interp,
  3425. Jim_GetResult(goi.interp),
  3426. Jim_NewStringObj( goi.interp, target->cmd_name, -1 ) );
  3427. target = target->next;
  3428. }
  3429. return JIM_OK;
  3430. case TG_CMD_CREATE:
  3431. if( goi.argc < 3 ){
  3432. Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "?name ... config options ...");
  3433. return JIM_ERR;
  3434. }
  3435. return target_create( &goi );
  3436. break;
  3437. case TG_CMD_NUMBER:
  3438. if( goi.argc != 1 ){
  3439. Jim_SetResult_sprintf( goi.interp, "expected: target number ?NUMBER?");
  3440. return JIM_ERR;
  3441. }
  3442. e = Jim_GetOpt_Wide( &goi, &w );
  3443. if( e != JIM_OK ){
  3444. return JIM_ERR;
  3445. }
  3446. {
  3447. target_t *t;
  3448. t = get_target_by_num(w);
  3449. if( t == NULL ){
  3450. Jim_SetResult_sprintf( goi.interp,"Target: number %d does not exist", (int)(w));
  3451. return JIM_ERR;
  3452. }
  3453. Jim_SetResultString( goi.interp, t->cmd_name, -1 );
  3454. return JIM_OK;
  3455. }
  3456. case TG_CMD_COUNT:
  3457. if( goi.argc != 0 ){
  3458. Jim_WrongNumArgs( goi.interp, 0, goi.argv, "<no parameters>");
  3459. return JIM_ERR;
  3460. }
  3461. Jim_SetResult( goi.interp,
  3462. Jim_NewIntObj( goi.interp, max_target_number()));
  3463. return JIM_OK;
  3464. }
  3465. return JIM_ERR;
  3466. }
  3467. struct FastLoad
  3468. {
  3469. u32 address;
  3470. u8 *data;
  3471. int length;
  3472. };
  3473. static int fastload_num;
  3474. static struct FastLoad *fastload;
  3475. static void free_fastload(void)
  3476. {
  3477. if (fastload!=NULL)
  3478. {
  3479. int i;
  3480. for (i=0; i<fastload_num; i++)
  3481. {
  3482. if (fastload[i].data)
  3483. free(fastload[i].data);
  3484. }
  3485. free(fastload);
  3486. fastload=NULL;
  3487. }
  3488. }
  3489. int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  3490. {
  3491. u8 *buffer;
  3492. u32 buf_cnt;
  3493. u32 image_size;
  3494. u32 min_address=0;
  3495. u32 max_address=0xffffffff;
  3496. int i;
  3497. int retval;
  3498. image_t image;
  3499. duration_t duration;
  3500. char *duration_text;
  3501. if ((argc < 1)||(argc > 5))
  3502. {
  3503. return ERROR_COMMAND_SYNTAX_ERROR;
  3504. }
  3505. /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
  3506. if (argc >= 2)
  3507. {
  3508. image.base_address_set = 1;
  3509. image.base_address = strtoul(args[1], NULL, 0);
  3510. }
  3511. else
  3512. {
  3513. image.base_address_set = 0;
  3514. }
  3515. image.start_address_set = 0;
  3516. if (argc>=4)
  3517. {
  3518. min_address=strtoul(args[3], NULL, 0);
  3519. }
  3520. if (argc>=5)
  3521. {
  3522. max_address=strtoul(args[4], NULL, 0)+min_address;
  3523. }
  3524. if (min_address>max_address)
  3525. {
  3526. return ERROR_COMMAND_SYNTAX_ERROR;
  3527. }
  3528. duration_start_measure(&duration);
  3529. if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
  3530. {
  3531. return ERROR_OK;
  3532. }
  3533. image_size = 0x0;
  3534. retval = ERROR_OK;
  3535. fastload_num=image.num_sections;
  3536. fastload=(struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
  3537. if (fastload==NULL)
  3538. {
  3539. image_close(&image);
  3540. return ERROR_FAIL;
  3541. }
  3542. memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
  3543. for (i = 0; i < image.num_sections; i++)
  3544. {
  3545. buffer = malloc(image.sections[i].size);
  3546. if (buffer == NULL)
  3547. {
  3548. command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
  3549. break;
  3550. }
  3551. if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
  3552. {
  3553. free(buffer);
  3554. break;
  3555. }
  3556. u32 offset=0;
  3557. u32 length=buf_cnt;
  3558. /* DANGER!!! beware of unsigned comparision here!!! */
  3559. if ((image.sections[i].base_address+buf_cnt>=min_address)&&
  3560. (image.sections[i].base_address<max_address))
  3561. {
  3562. if (image.sections[i].base_address<min_address)
  3563. {
  3564. /* clip addresses below */
  3565. offset+=min_address-image.sections[i].base_address;
  3566. length-=offset;
  3567. }
  3568. if (image.sections[i].base_address+buf_cnt>max_address)
  3569. {
  3570. length-=(image.sections[i].base_address+buf_cnt)-max_address;
  3571. }
  3572. fastload[i].address=image.sections[i].base_address+offset;
  3573. fastload[i].data=malloc(length);
  3574. if (fastload[i].data==NULL)
  3575. {
  3576. free(buffer);
  3577. break;
  3578. }
  3579. memcpy(fastload[i].data, buffer+offset, length);
  3580. fastload[i].length=length;
  3581. image_size += length;
  3582. command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
  3583. }
  3584. free(buffer);
  3585. }
  3586. duration_stop_measure(&duration, &duration_text);
  3587. if (retval==ERROR_OK)
  3588. {
  3589. command_print(cmd_ctx, "Loaded %u bytes in %s", image_size, duration_text);
  3590. command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
  3591. }
  3592. free(duration_text);
  3593. image_close(&image);
  3594. if (retval!=ERROR_OK)
  3595. {
  3596. free_fastload();
  3597. }
  3598. return retval;
  3599. }
  3600. int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  3601. {
  3602. if (argc>0)
  3603. return ERROR_COMMAND_SYNTAX_ERROR;
  3604. if (fastload==NULL)
  3605. {
  3606. LOG_ERROR("No image in memory");
  3607. return ERROR_FAIL;
  3608. }
  3609. int i;
  3610. int ms=timeval_ms();
  3611. int size=0;
  3612. for (i=0; i<fastload_num;i++)
  3613. {
  3614. int retval;
  3615. target_t *target = get_current_target(cmd_ctx);
  3616. if ((retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data)) != ERROR_OK)
  3617. {
  3618. return retval;
  3619. }
  3620. size+=fastload[i].length;
  3621. }
  3622. int after=timeval_ms();
  3623. command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
  3624. return ERROR_OK;
  3625. }