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.
 
 
 
 
 
 

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