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.
 
 
 
 
 
 

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