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.
 
 
 
 
 
 

4595 lines
114 KiB

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