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.
 
 
 
 
 
 

5455 lines
131 KiB

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