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.
 
 
 
 
 
 

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