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.
 
 
 
 
 
 

4893 lines
118 KiB

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