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.
 
 
 
 
 
 

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