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.
 
 
 
 
 
 

2325 lines
57 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "replacements.h"
  24. #include "target.h"
  25. #include "target_request.h"
  26. #include "log.h"
  27. #include "configuration.h"
  28. #include "binarybuffer.h"
  29. #include "jtag.h"
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <inttypes.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <unistd.h>
  36. #include <errno.h>
  37. #include <sys/time.h>
  38. #include <time.h>
  39. #include <time_support.h>
  40. #include <fileio.h>
  41. #include <image.h>
  42. int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv);
  43. int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  44. int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  45. int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  46. int handle_target_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  47. int handle_run_and_halt_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  48. int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  49. int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  50. int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  51. int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  52. int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  53. int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  54. int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  55. int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  56. int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  57. int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  58. int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  59. int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  60. int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  61. int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  62. int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  63. int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  64. int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  65. int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  66. int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc);
  67. /* targets
  68. */
  69. extern target_type_t arm7tdmi_target;
  70. extern target_type_t arm720t_target;
  71. extern target_type_t arm9tdmi_target;
  72. extern target_type_t arm920t_target;
  73. extern target_type_t arm966e_target;
  74. extern target_type_t arm926ejs_target;
  75. extern target_type_t xscale_target;
  76. extern target_type_t cortexm3_target;
  77. target_type_t *target_types[] =
  78. {
  79. &arm7tdmi_target,
  80. &arm9tdmi_target,
  81. &arm920t_target,
  82. &arm720t_target,
  83. &arm966e_target,
  84. &arm926ejs_target,
  85. &xscale_target,
  86. &cortexm3_target,
  87. NULL,
  88. };
  89. target_t *targets = NULL;
  90. target_event_callback_t *target_event_callbacks = NULL;
  91. target_timer_callback_t *target_timer_callbacks = NULL;
  92. char *target_state_strings[] =
  93. {
  94. "unknown",
  95. "running",
  96. "halted",
  97. "reset",
  98. "debug_running",
  99. };
  100. char *target_debug_reason_strings[] =
  101. {
  102. "debug request", "breakpoint", "watchpoint",
  103. "watchpoint and breakpoint", "single step",
  104. "target not halted"
  105. };
  106. char *target_endianess_strings[] =
  107. {
  108. "big endian",
  109. "little endian",
  110. };
  111. enum daemon_startup_mode startup_mode = DAEMON_ATTACH;
  112. static int target_continous_poll = 1;
  113. /* read a u32 from a buffer in target memory endianness */
  114. u32 target_buffer_get_u32(target_t *target, u8 *buffer)
  115. {
  116. if (target->endianness == TARGET_LITTLE_ENDIAN)
  117. return le_to_h_u32(buffer);
  118. else
  119. return be_to_h_u32(buffer);
  120. }
  121. /* read a u16 from a buffer in target memory endianness */
  122. u16 target_buffer_get_u16(target_t *target, u8 *buffer)
  123. {
  124. if (target->endianness == TARGET_LITTLE_ENDIAN)
  125. return le_to_h_u16(buffer);
  126. else
  127. return be_to_h_u16(buffer);
  128. }
  129. /* write a u32 to a buffer in target memory endianness */
  130. void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
  131. {
  132. if (target->endianness == TARGET_LITTLE_ENDIAN)
  133. h_u32_to_le(buffer, value);
  134. else
  135. h_u32_to_be(buffer, value);
  136. }
  137. /* write a u16 to a buffer in target memory endianness */
  138. void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
  139. {
  140. if (target->endianness == TARGET_LITTLE_ENDIAN)
  141. h_u16_to_le(buffer, value);
  142. else
  143. h_u16_to_be(buffer, value);
  144. }
  145. /* returns a pointer to the n-th configured target */
  146. target_t* get_target_by_num(int num)
  147. {
  148. target_t *target = targets;
  149. int i = 0;
  150. while (target)
  151. {
  152. if (num == i)
  153. return target;
  154. target = target->next;
  155. i++;
  156. }
  157. return NULL;
  158. }
  159. int get_num_by_target(target_t *query_target)
  160. {
  161. target_t *target = targets;
  162. int i = 0;
  163. while (target)
  164. {
  165. if (target == query_target)
  166. return i;
  167. target = target->next;
  168. i++;
  169. }
  170. return -1;
  171. }
  172. target_t* get_current_target(command_context_t *cmd_ctx)
  173. {
  174. target_t *target = get_target_by_num(cmd_ctx->current_target);
  175. if (target == NULL)
  176. {
  177. ERROR("BUG: current_target out of bounds");
  178. exit(-1);
  179. }
  180. return target;
  181. }
  182. /* Process target initialization, when target entered debug out of reset
  183. * the handler is unregistered at the end of this function, so it's only called once
  184. */
  185. int target_init_handler(struct target_s *target, enum target_event event, void *priv)
  186. {
  187. FILE *script;
  188. struct command_context_s *cmd_ctx = priv;
  189. if ((event == TARGET_EVENT_HALTED) && (target->reset_script))
  190. {
  191. target_unregister_event_callback(target_init_handler, priv);
  192. script = open_file_from_path(cmd_ctx, target->reset_script, "r");
  193. if (!script)
  194. {
  195. ERROR("couldn't open script file %s", target->reset_script);
  196. return ERROR_OK;
  197. }
  198. INFO("executing reset script '%s'", target->reset_script);
  199. command_run_file(cmd_ctx, script, COMMAND_EXEC);
  200. fclose(script);
  201. jtag_execute_queue();
  202. }
  203. return ERROR_OK;
  204. }
  205. int target_run_and_halt_handler(void *priv)
  206. {
  207. target_t *target = priv;
  208. target->type->halt(target);
  209. return ERROR_OK;
  210. }
  211. int target_process_reset(struct command_context_s *cmd_ctx)
  212. {
  213. int retval = ERROR_OK;
  214. target_t *target;
  215. struct timeval timeout, now;
  216. /* prepare reset_halt where necessary */
  217. target = targets;
  218. while (target)
  219. {
  220. if (jtag_reset_config & RESET_SRST_PULLS_TRST)
  221. {
  222. switch (target->reset_mode)
  223. {
  224. case RESET_HALT:
  225. command_print(cmd_ctx, "nSRST pulls nTRST, falling back to RESET_RUN_AND_HALT");
  226. target->reset_mode = RESET_RUN_AND_HALT;
  227. break;
  228. case RESET_INIT:
  229. command_print(cmd_ctx, "nSRST pulls nTRST, falling back to RESET_RUN_AND_INIT");
  230. target->reset_mode = RESET_RUN_AND_INIT;
  231. break;
  232. default:
  233. break;
  234. }
  235. }
  236. switch (target->reset_mode)
  237. {
  238. case RESET_HALT:
  239. case RESET_INIT:
  240. target->type->prepare_reset_halt(target);
  241. break;
  242. default:
  243. break;
  244. }
  245. target = target->next;
  246. }
  247. target = targets;
  248. while (target)
  249. {
  250. target->type->assert_reset(target);
  251. target = target->next;
  252. }
  253. jtag_execute_queue();
  254. /* request target halt if necessary, and schedule further action */
  255. target = targets;
  256. while (target)
  257. {
  258. switch (target->reset_mode)
  259. {
  260. case RESET_RUN:
  261. /* nothing to do if target just wants to be run */
  262. break;
  263. case RESET_RUN_AND_HALT:
  264. /* schedule halt */
  265. target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
  266. break;
  267. case RESET_RUN_AND_INIT:
  268. /* schedule halt */
  269. target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
  270. target_register_event_callback(target_init_handler, cmd_ctx);
  271. break;
  272. case RESET_HALT:
  273. target->type->halt(target);
  274. break;
  275. case RESET_INIT:
  276. target->type->halt(target);
  277. target_register_event_callback(target_init_handler, cmd_ctx);
  278. break;
  279. default:
  280. ERROR("BUG: unknown target->reset_mode");
  281. }
  282. target = target->next;
  283. }
  284. target = targets;
  285. while (target)
  286. {
  287. target->type->deassert_reset(target);
  288. target = target->next;
  289. }
  290. jtag_execute_queue();
  291. /* Wait for reset to complete, maximum 5 seconds. */
  292. gettimeofday(&timeout, NULL);
  293. timeval_add_time(&timeout, 5, 0);
  294. for(;;)
  295. {
  296. gettimeofday(&now, NULL);
  297. target_call_timer_callbacks();
  298. target = targets;
  299. while (target)
  300. {
  301. target->type->poll(target);
  302. if ((target->reset_mode == RESET_RUN_AND_INIT) || (target->reset_mode == RESET_RUN_AND_HALT))
  303. {
  304. if (target->state != TARGET_HALTED)
  305. {
  306. if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
  307. {
  308. command_print(cmd_ctx, "Timed out waiting for reset");
  309. goto done;
  310. }
  311. usleep(100*1000); /* Do not eat all cpu */
  312. goto again;
  313. }
  314. }
  315. target = target->next;
  316. }
  317. /* All targets we're waiting for are halted */
  318. break;
  319. again:;
  320. }
  321. done:
  322. /* We want any events to be processed before the prompt */
  323. target_call_timer_callbacks();
  324. return retval;
  325. }
  326. static int default_virt2phys(struct target_s *target, u32 virtual, u32 *physical)
  327. {
  328. *physical = virtual;
  329. return ERROR_OK;
  330. }
  331. static int default_mmu(struct target_s *target, int *enabled)
  332. {
  333. USER("No MMU present");
  334. *enabled = 0;
  335. return ERROR_OK;
  336. }
  337. int target_init(struct command_context_s *cmd_ctx)
  338. {
  339. target_t *target = targets;
  340. while (target)
  341. {
  342. if (target->type->init_target(cmd_ctx, target) != ERROR_OK)
  343. {
  344. ERROR("target '%s' init failed", target->type->name);
  345. exit(-1);
  346. }
  347. /* Set up default functions if none are provided by target */
  348. if (target->type->virt2phys == NULL)
  349. {
  350. target->type->virt2phys = default_virt2phys;
  351. }
  352. if (target->type->mmu == NULL)
  353. {
  354. target->type->mmu = default_mmu;
  355. }
  356. target = target->next;
  357. }
  358. if (targets)
  359. {
  360. target_register_user_commands(cmd_ctx);
  361. target_register_timer_callback(handle_target, 100, 1, NULL);
  362. }
  363. return ERROR_OK;
  364. }
  365. int target_init_reset(struct command_context_s *cmd_ctx)
  366. {
  367. if (startup_mode == DAEMON_RESET)
  368. target_process_reset(cmd_ctx);
  369. return ERROR_OK;
  370. }
  371. int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
  372. {
  373. target_event_callback_t **callbacks_p = &target_event_callbacks;
  374. if (callback == NULL)
  375. {
  376. return ERROR_INVALID_ARGUMENTS;
  377. }
  378. if (*callbacks_p)
  379. {
  380. while ((*callbacks_p)->next)
  381. callbacks_p = &((*callbacks_p)->next);
  382. callbacks_p = &((*callbacks_p)->next);
  383. }
  384. (*callbacks_p) = malloc(sizeof(target_event_callback_t));
  385. (*callbacks_p)->callback = callback;
  386. (*callbacks_p)->priv = priv;
  387. (*callbacks_p)->next = NULL;
  388. return ERROR_OK;
  389. }
  390. int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
  391. {
  392. target_timer_callback_t **callbacks_p = &target_timer_callbacks;
  393. struct timeval now;
  394. if (callback == NULL)
  395. {
  396. return ERROR_INVALID_ARGUMENTS;
  397. }
  398. if (*callbacks_p)
  399. {
  400. while ((*callbacks_p)->next)
  401. callbacks_p = &((*callbacks_p)->next);
  402. callbacks_p = &((*callbacks_p)->next);
  403. }
  404. (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
  405. (*callbacks_p)->callback = callback;
  406. (*callbacks_p)->periodic = periodic;
  407. (*callbacks_p)->time_ms = time_ms;
  408. gettimeofday(&now, NULL);
  409. (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
  410. time_ms -= (time_ms % 1000);
  411. (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
  412. if ((*callbacks_p)->when.tv_usec > 1000000)
  413. {
  414. (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
  415. (*callbacks_p)->when.tv_sec += 1;
  416. }
  417. (*callbacks_p)->priv = priv;
  418. (*callbacks_p)->next = NULL;
  419. return ERROR_OK;
  420. }
  421. int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
  422. {
  423. target_event_callback_t **p = &target_event_callbacks;
  424. target_event_callback_t *c = target_event_callbacks;
  425. if (callback == NULL)
  426. {
  427. return ERROR_INVALID_ARGUMENTS;
  428. }
  429. while (c)
  430. {
  431. target_event_callback_t *next = c->next;
  432. if ((c->callback == callback) && (c->priv == priv))
  433. {
  434. *p = next;
  435. free(c);
  436. return ERROR_OK;
  437. }
  438. else
  439. p = &(c->next);
  440. c = next;
  441. }
  442. return ERROR_OK;
  443. }
  444. int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
  445. {
  446. target_timer_callback_t **p = &target_timer_callbacks;
  447. target_timer_callback_t *c = target_timer_callbacks;
  448. if (callback == NULL)
  449. {
  450. return ERROR_INVALID_ARGUMENTS;
  451. }
  452. while (c)
  453. {
  454. target_timer_callback_t *next = c->next;
  455. if ((c->callback == callback) && (c->priv == priv))
  456. {
  457. *p = next;
  458. free(c);
  459. return ERROR_OK;
  460. }
  461. else
  462. p = &(c->next);
  463. c = next;
  464. }
  465. return ERROR_OK;
  466. }
  467. int target_call_event_callbacks(target_t *target, enum target_event event)
  468. {
  469. target_event_callback_t *callback = target_event_callbacks;
  470. target_event_callback_t *next_callback;
  471. DEBUG("target event %i", event);
  472. while (callback)
  473. {
  474. next_callback = callback->next;
  475. callback->callback(target, event, callback->priv);
  476. callback = next_callback;
  477. }
  478. return ERROR_OK;
  479. }
  480. int target_call_timer_callbacks()
  481. {
  482. target_timer_callback_t *callback = target_timer_callbacks;
  483. target_timer_callback_t *next_callback;
  484. struct timeval now;
  485. gettimeofday(&now, NULL);
  486. while (callback)
  487. {
  488. next_callback = callback->next;
  489. if (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
  490. || (now.tv_sec > callback->when.tv_sec))
  491. {
  492. callback->callback(callback->priv);
  493. if (callback->periodic)
  494. {
  495. int time_ms = callback->time_ms;
  496. callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
  497. time_ms -= (time_ms % 1000);
  498. callback->when.tv_sec = now.tv_sec + time_ms / 1000;
  499. if (callback->when.tv_usec > 1000000)
  500. {
  501. callback->when.tv_usec = callback->when.tv_usec - 1000000;
  502. callback->when.tv_sec += 1;
  503. }
  504. }
  505. else
  506. target_unregister_timer_callback(callback->callback, callback->priv);
  507. }
  508. callback = next_callback;
  509. }
  510. return ERROR_OK;
  511. }
  512. int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
  513. {
  514. working_area_t *c = target->working_areas;
  515. working_area_t *new_wa = NULL;
  516. /* Reevaluate working area address based on MMU state*/
  517. if (target->working_areas == NULL)
  518. {
  519. int retval;
  520. int enabled;
  521. retval = target->type->mmu(target, &enabled);
  522. if (retval != ERROR_OK)
  523. {
  524. return retval;
  525. }
  526. if (enabled)
  527. {
  528. target->working_area = target->working_area_virt;
  529. }
  530. else
  531. {
  532. target->working_area = target->working_area_phys;
  533. }
  534. }
  535. /* only allocate multiples of 4 byte */
  536. if (size % 4)
  537. {
  538. ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
  539. size = CEIL(size, 4);
  540. }
  541. /* see if there's already a matching working area */
  542. while (c)
  543. {
  544. if ((c->free) && (c->size == size))
  545. {
  546. new_wa = c;
  547. break;
  548. }
  549. c = c->next;
  550. }
  551. /* if not, allocate a new one */
  552. if (!new_wa)
  553. {
  554. working_area_t **p = &target->working_areas;
  555. u32 first_free = target->working_area;
  556. u32 free_size = target->working_area_size;
  557. DEBUG("allocating new working area");
  558. c = target->working_areas;
  559. while (c)
  560. {
  561. first_free += c->size;
  562. free_size -= c->size;
  563. p = &c->next;
  564. c = c->next;
  565. }
  566. if (free_size < size)
  567. {
  568. WARNING("not enough working area available(requested %d, free %d)", size, free_size);
  569. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  570. }
  571. new_wa = malloc(sizeof(working_area_t));
  572. new_wa->next = NULL;
  573. new_wa->size = size;
  574. new_wa->address = first_free;
  575. if (target->backup_working_area)
  576. {
  577. new_wa->backup = malloc(new_wa->size);
  578. target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup);
  579. }
  580. else
  581. {
  582. new_wa->backup = NULL;
  583. }
  584. /* put new entry in list */
  585. *p = new_wa;
  586. }
  587. /* mark as used, and return the new (reused) area */
  588. new_wa->free = 0;
  589. *area = new_wa;
  590. /* user pointer */
  591. new_wa->user = area;
  592. return ERROR_OK;
  593. }
  594. int target_free_working_area(struct target_s *target, working_area_t *area)
  595. {
  596. if (area->free)
  597. return ERROR_OK;
  598. if (target->backup_working_area)
  599. target->type->write_memory(target, area->address, 4, area->size / 4, area->backup);
  600. area->free = 1;
  601. /* mark user pointer invalid */
  602. *area->user = NULL;
  603. area->user = NULL;
  604. return ERROR_OK;
  605. }
  606. int target_free_all_working_areas(struct target_s *target)
  607. {
  608. working_area_t *c = target->working_areas;
  609. while (c)
  610. {
  611. working_area_t *next = c->next;
  612. target_free_working_area(target, c);
  613. if (c->backup)
  614. free(c->backup);
  615. free(c);
  616. c = next;
  617. }
  618. target->working_areas = NULL;
  619. return ERROR_OK;
  620. }
  621. int target_register_commands(struct command_context_s *cmd_ctx)
  622. {
  623. register_command(cmd_ctx, NULL, "target", handle_target_command, COMMAND_CONFIG, NULL);
  624. register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, NULL);
  625. register_command(cmd_ctx, NULL, "daemon_startup", handle_daemon_startup_command, COMMAND_CONFIG, NULL);
  626. register_command(cmd_ctx, NULL, "target_script", handle_target_script_command, COMMAND_CONFIG, NULL);
  627. register_command(cmd_ctx, NULL, "run_and_halt_time", handle_run_and_halt_time_command, COMMAND_CONFIG, NULL);
  628. register_command(cmd_ctx, NULL, "working_area", handle_working_area_command, COMMAND_ANY, "working_area <target#> <address> <size> <'backup'|'nobackup'> [virtual address]");
  629. register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "virt2phys <virtual address>");
  630. return ERROR_OK;
  631. }
  632. /* Single aligned words are guaranteed to use 16 or 32 bit access
  633. * mode respectively, otherwise data is handled as quickly as
  634. * possible
  635. */
  636. int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
  637. {
  638. int retval;
  639. DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
  640. if (((address % 2) == 0) && (size == 2))
  641. {
  642. return target->type->write_memory(target, address, 2, 1, buffer);
  643. }
  644. /* handle unaligned head bytes */
  645. if (address % 4)
  646. {
  647. int unaligned = 4 - (address % 4);
  648. if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
  649. return retval;
  650. buffer += unaligned;
  651. address += unaligned;
  652. size -= unaligned;
  653. }
  654. /* handle aligned words */
  655. if (size >= 4)
  656. {
  657. int aligned = size - (size % 4);
  658. /* use bulk writes above a certain limit. This may have to be changed */
  659. if (aligned > 128)
  660. {
  661. if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
  662. return retval;
  663. }
  664. else
  665. {
  666. if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
  667. return retval;
  668. }
  669. buffer += aligned;
  670. address += aligned;
  671. size -= aligned;
  672. }
  673. /* handle tail writes of less than 4 bytes */
  674. if (size > 0)
  675. {
  676. if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
  677. return retval;
  678. }
  679. return ERROR_OK;
  680. }
  681. /* Single aligned words are guaranteed to use 16 or 32 bit access
  682. * mode respectively, otherwise data is handled as quickly as
  683. * possible
  684. */
  685. int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
  686. {
  687. int retval;
  688. DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
  689. if (((address % 2) == 0) && (size == 2))
  690. {
  691. return target->type->read_memory(target, address, 2, 1, buffer);
  692. }
  693. /* handle unaligned head bytes */
  694. if (address % 4)
  695. {
  696. int unaligned = 4 - (address % 4);
  697. if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
  698. return retval;
  699. buffer += unaligned;
  700. address += unaligned;
  701. size -= unaligned;
  702. }
  703. /* handle aligned words */
  704. if (size >= 4)
  705. {
  706. int aligned = size - (size % 4);
  707. if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
  708. return retval;
  709. buffer += aligned;
  710. address += aligned;
  711. size -= aligned;
  712. }
  713. /* handle tail writes of less than 4 bytes */
  714. if (size > 0)
  715. {
  716. if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
  717. return retval;
  718. }
  719. return ERROR_OK;
  720. }
  721. int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* crc)
  722. {
  723. u8 *buffer;
  724. int retval;
  725. int i;
  726. u32 checksum = 0;
  727. if ((retval = target->type->checksum_memory(target, address,
  728. size, &checksum)) == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
  729. {
  730. buffer = malloc(size);
  731. if (buffer == NULL)
  732. {
  733. ERROR("error allocating buffer for section (%d bytes)", size);
  734. return ERROR_INVALID_ARGUMENTS;
  735. }
  736. retval = target_read_buffer(target, address, size, buffer);
  737. if (retval != ERROR_OK)
  738. {
  739. free(buffer);
  740. return retval;
  741. }
  742. /* convert to target endianess */
  743. for (i = 0; i < (size/sizeof(u32)); i++)
  744. {
  745. u32 target_data;
  746. target_data = target_buffer_get_u32(target, &buffer[i*sizeof(u32)]);
  747. target_buffer_set_u32(target, &buffer[i*sizeof(u32)], target_data);
  748. }
  749. retval = image_calculate_checksum( buffer, size, &checksum );
  750. free(buffer);
  751. }
  752. *crc = checksum;
  753. return retval;
  754. }
  755. int target_read_u32(struct target_s *target, u32 address, u32 *value)
  756. {
  757. u8 value_buf[4];
  758. int retval = target->type->read_memory(target, address, 4, 1, value_buf);
  759. if (retval == ERROR_OK)
  760. {
  761. *value = target_buffer_get_u32(target, value_buf);
  762. DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
  763. }
  764. else
  765. {
  766. *value = 0x0;
  767. DEBUG("address: 0x%8.8x failed", address);
  768. }
  769. return retval;
  770. }
  771. int target_read_u16(struct target_s *target, u32 address, u16 *value)
  772. {
  773. u8 value_buf[2];
  774. int retval = target->type->read_memory(target, address, 2, 1, value_buf);
  775. if (retval == ERROR_OK)
  776. {
  777. *value = target_buffer_get_u16(target, value_buf);
  778. DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
  779. }
  780. else
  781. {
  782. *value = 0x0;
  783. DEBUG("address: 0x%8.8x failed", address);
  784. }
  785. return retval;
  786. }
  787. int target_read_u8(struct target_s *target, u32 address, u8 *value)
  788. {
  789. int retval = target->type->read_memory(target, address, 1, 1, value);
  790. if (retval == ERROR_OK)
  791. {
  792. DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
  793. }
  794. else
  795. {
  796. *value = 0x0;
  797. DEBUG("address: 0x%8.8x failed", address);
  798. }
  799. return retval;
  800. }
  801. int target_write_u32(struct target_s *target, u32 address, u32 value)
  802. {
  803. int retval;
  804. u8 value_buf[4];
  805. DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
  806. target_buffer_set_u32(target, value_buf, value);
  807. if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
  808. {
  809. DEBUG("failed: %i", retval);
  810. }
  811. return retval;
  812. }
  813. int target_write_u16(struct target_s *target, u32 address, u16 value)
  814. {
  815. int retval;
  816. u8 value_buf[2];
  817. DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
  818. target_buffer_set_u16(target, value_buf, value);
  819. if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
  820. {
  821. DEBUG("failed: %i", retval);
  822. }
  823. return retval;
  824. }
  825. int target_write_u8(struct target_s *target, u32 address, u8 value)
  826. {
  827. int retval;
  828. DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
  829. if ((retval = target->type->read_memory(target, address, 1, 1, &value)) != ERROR_OK)
  830. {
  831. DEBUG("failed: %i", retval);
  832. }
  833. return retval;
  834. }
  835. int target_register_user_commands(struct command_context_s *cmd_ctx)
  836. {
  837. register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, NULL);
  838. register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
  839. register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
  840. register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
  841. register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
  842. register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
  843. register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init|run_and_halt|run_and_init]");
  844. register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
  845. register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
  846. register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
  847. register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
  848. register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value>");
  849. register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value>");
  850. register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value>");
  851. register_command(cmd_ctx, NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
  852. register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
  853. register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
  854. register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
  855. register_command(cmd_ctx, NULL, "load_image", handle_load_image_command, COMMAND_EXEC, "load_image <file> <address> ['bin'|'ihex'|'elf'|'s19']");
  856. register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
  857. register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
  858. register_command(cmd_ctx, NULL, "load_binary", handle_load_image_command, COMMAND_EXEC, "[DEPRECATED] load_binary <file> <address>");
  859. register_command(cmd_ctx, NULL, "dump_binary", handle_dump_image_command, COMMAND_EXEC, "[DEPRECATED] dump_binary <file> <address> <size>");
  860. target_request_register_commands(cmd_ctx);
  861. trace_register_commands(cmd_ctx);
  862. return ERROR_OK;
  863. }
  864. int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  865. {
  866. target_t *target = targets;
  867. int count = 0;
  868. if (argc == 1)
  869. {
  870. int num = strtoul(args[0], NULL, 0);
  871. while (target)
  872. {
  873. count++;
  874. target = target->next;
  875. }
  876. if (num < count)
  877. cmd_ctx->current_target = num;
  878. else
  879. command_print(cmd_ctx, "%i is out of bounds, only %i targets are configured", num, count);
  880. return ERROR_OK;
  881. }
  882. while (target)
  883. {
  884. command_print(cmd_ctx, "%i: %s (%s), state: %s", count++, target->type->name, target_endianess_strings[target->endianness], target_state_strings[target->state]);
  885. target = target->next;
  886. }
  887. return ERROR_OK;
  888. }
  889. int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  890. {
  891. int i;
  892. int found = 0;
  893. if (argc < 3)
  894. {
  895. ERROR("target command requires at least three arguments: <type> <endianess> <reset_mode>");
  896. exit(-1);
  897. }
  898. /* search for the specified target */
  899. if (args[0] && (args[0][0] != 0))
  900. {
  901. for (i = 0; target_types[i]; i++)
  902. {
  903. if (strcmp(args[0], target_types[i]->name) == 0)
  904. {
  905. target_t **last_target_p = &targets;
  906. /* register target specific commands */
  907. if (target_types[i]->register_commands(cmd_ctx) != ERROR_OK)
  908. {
  909. ERROR("couldn't register '%s' commands", args[0]);
  910. exit(-1);
  911. }
  912. if (*last_target_p)
  913. {
  914. while ((*last_target_p)->next)
  915. last_target_p = &((*last_target_p)->next);
  916. last_target_p = &((*last_target_p)->next);
  917. }
  918. *last_target_p = malloc(sizeof(target_t));
  919. (*last_target_p)->type = target_types[i];
  920. if (strcmp(args[1], "big") == 0)
  921. (*last_target_p)->endianness = TARGET_BIG_ENDIAN;
  922. else if (strcmp(args[1], "little") == 0)
  923. (*last_target_p)->endianness = TARGET_LITTLE_ENDIAN;
  924. else
  925. {
  926. ERROR("endianness must be either 'little' or 'big', not '%s'", args[1]);
  927. exit(-1);
  928. }
  929. /* what to do on a target reset */
  930. if (strcmp(args[2], "reset_halt") == 0)
  931. (*last_target_p)->reset_mode = RESET_HALT;
  932. else if (strcmp(args[2], "reset_run") == 0)
  933. (*last_target_p)->reset_mode = RESET_RUN;
  934. else if (strcmp(args[2], "reset_init") == 0)
  935. (*last_target_p)->reset_mode = RESET_INIT;
  936. else if (strcmp(args[2], "run_and_halt") == 0)
  937. (*last_target_p)->reset_mode = RESET_RUN_AND_HALT;
  938. else if (strcmp(args[2], "run_and_init") == 0)
  939. (*last_target_p)->reset_mode = RESET_RUN_AND_INIT;
  940. else
  941. {
  942. ERROR("unknown target startup mode %s", args[2]);
  943. exit(-1);
  944. }
  945. (*last_target_p)->run_and_halt_time = 1000; /* default 1s */
  946. (*last_target_p)->reset_script = NULL;
  947. (*last_target_p)->post_halt_script = NULL;
  948. (*last_target_p)->pre_resume_script = NULL;
  949. (*last_target_p)->gdb_program_script = NULL;
  950. (*last_target_p)->working_area = 0x0;
  951. (*last_target_p)->working_area_size = 0x0;
  952. (*last_target_p)->working_areas = NULL;
  953. (*last_target_p)->backup_working_area = 0;
  954. (*last_target_p)->state = TARGET_UNKNOWN;
  955. (*last_target_p)->reg_cache = NULL;
  956. (*last_target_p)->breakpoints = NULL;
  957. (*last_target_p)->watchpoints = NULL;
  958. (*last_target_p)->next = NULL;
  959. (*last_target_p)->arch_info = NULL;
  960. /* initialize trace information */
  961. (*last_target_p)->trace_info = malloc(sizeof(trace_t));
  962. (*last_target_p)->trace_info->num_trace_points = 0;
  963. (*last_target_p)->trace_info->trace_points_size = 0;
  964. (*last_target_p)->trace_info->trace_points = NULL;
  965. (*last_target_p)->trace_info->trace_history_size = 0;
  966. (*last_target_p)->trace_info->trace_history = NULL;
  967. (*last_target_p)->trace_info->trace_history_pos = 0;
  968. (*last_target_p)->trace_info->trace_history_overflowed = 0;
  969. (*last_target_p)->dbgmsg = NULL;
  970. (*last_target_p)->dbg_msg_enabled = 0;
  971. (*last_target_p)->type->target_command(cmd_ctx, cmd, args, argc, *last_target_p);
  972. found = 1;
  973. break;
  974. }
  975. }
  976. }
  977. /* no matching target found */
  978. if (!found)
  979. {
  980. ERROR("target '%s' not found", args[0]);
  981. exit(-1);
  982. }
  983. return ERROR_OK;
  984. }
  985. /* usage: target_script <target#> <event> <script_file> */
  986. int handle_target_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  987. {
  988. target_t *target = NULL;
  989. if (argc < 3)
  990. {
  991. ERROR("incomplete target_script command");
  992. exit(-1);
  993. }
  994. target = get_target_by_num(strtoul(args[0], NULL, 0));
  995. if (!target)
  996. {
  997. ERROR("target number '%s' not defined", args[0]);
  998. exit(-1);
  999. }
  1000. if (strcmp(args[1], "reset") == 0)
  1001. {
  1002. if (target->reset_script)
  1003. free(target->reset_script);
  1004. target->reset_script = strdup(args[2]);
  1005. }
  1006. else if (strcmp(args[1], "post_halt") == 0)
  1007. {
  1008. if (target->post_halt_script)
  1009. free(target->post_halt_script);
  1010. target->post_halt_script = strdup(args[2]);
  1011. }
  1012. else if (strcmp(args[1], "pre_resume") == 0)
  1013. {
  1014. if (target->pre_resume_script)
  1015. free(target->pre_resume_script);
  1016. target->pre_resume_script = strdup(args[2]);
  1017. }
  1018. else if (strcmp(args[1], "gdb_program_config") == 0)
  1019. {
  1020. if (target->gdb_program_script)
  1021. free(target->gdb_program_script);
  1022. target->gdb_program_script = strdup(args[2]);
  1023. }
  1024. else
  1025. {
  1026. ERROR("unknown event type: '%s", args[1]);
  1027. exit(-1);
  1028. }
  1029. return ERROR_OK;
  1030. }
  1031. int handle_run_and_halt_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1032. {
  1033. target_t *target = NULL;
  1034. if (argc < 2)
  1035. {
  1036. ERROR("incomplete run_and_halt_time command");
  1037. exit(-1);
  1038. }
  1039. target = get_target_by_num(strtoul(args[0], NULL, 0));
  1040. if (!target)
  1041. {
  1042. ERROR("target number '%s' not defined", args[0]);
  1043. exit(-1);
  1044. }
  1045. target->run_and_halt_time = strtoul(args[1], NULL, 0);
  1046. return ERROR_OK;
  1047. }
  1048. int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1049. {
  1050. target_t *target = NULL;
  1051. if ((argc < 4) || (argc > 5))
  1052. {
  1053. return ERROR_COMMAND_SYNTAX_ERROR;
  1054. }
  1055. target = get_target_by_num(strtoul(args[0], NULL, 0));
  1056. if (!target)
  1057. {
  1058. ERROR("target number '%s' not defined", args[0]);
  1059. exit(-1);
  1060. }
  1061. target_free_all_working_areas(target);
  1062. target->working_area_phys = target->working_area_virt = strtoul(args[1], NULL, 0);
  1063. if (argc == 5)
  1064. {
  1065. target->working_area_virt = strtoul(args[4], NULL, 0);
  1066. }
  1067. target->working_area_size = strtoul(args[2], NULL, 0);
  1068. if (strcmp(args[3], "backup") == 0)
  1069. {
  1070. target->backup_working_area = 1;
  1071. }
  1072. else if (strcmp(args[3], "nobackup") == 0)
  1073. {
  1074. target->backup_working_area = 0;
  1075. }
  1076. else
  1077. {
  1078. ERROR("unrecognized <backup|nobackup> argument (%s)", args[3]);
  1079. return ERROR_COMMAND_SYNTAX_ERROR;
  1080. }
  1081. return ERROR_OK;
  1082. }
  1083. /* process target state changes */
  1084. int handle_target(void *priv)
  1085. {
  1086. int retval;
  1087. target_t *target = targets;
  1088. while (target)
  1089. {
  1090. /* only poll if target isn't already halted */
  1091. if (target->state != TARGET_HALTED)
  1092. {
  1093. if (target_continous_poll)
  1094. if ((retval = target->type->poll(target)) < 0)
  1095. {
  1096. ERROR("couldn't poll target. It's due for a reset.");
  1097. }
  1098. }
  1099. target = target->next;
  1100. }
  1101. return ERROR_OK;
  1102. }
  1103. int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1104. {
  1105. target_t *target;
  1106. reg_t *reg = NULL;
  1107. int count = 0;
  1108. char *value;
  1109. DEBUG("-");
  1110. target = get_current_target(cmd_ctx);
  1111. /* list all available registers for the current target */
  1112. if (argc == 0)
  1113. {
  1114. reg_cache_t *cache = target->reg_cache;
  1115. count = 0;
  1116. while(cache)
  1117. {
  1118. int i;
  1119. for (i = 0; i < cache->num_regs; i++)
  1120. {
  1121. value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
  1122. command_print(cmd_ctx, "(%i) %s (/%i): 0x%s (dirty: %i, valid: %i)", count++, cache->reg_list[i].name, cache->reg_list[i].size, value, cache->reg_list[i].dirty, cache->reg_list[i].valid);
  1123. free(value);
  1124. }
  1125. cache = cache->next;
  1126. }
  1127. return ERROR_OK;
  1128. }
  1129. /* access a single register by its ordinal number */
  1130. if ((args[0][0] >= '0') && (args[0][0] <= '9'))
  1131. {
  1132. int num = strtoul(args[0], NULL, 0);
  1133. reg_cache_t *cache = target->reg_cache;
  1134. count = 0;
  1135. while(cache)
  1136. {
  1137. int i;
  1138. for (i = 0; i < cache->num_regs; i++)
  1139. {
  1140. if (count++ == num)
  1141. {
  1142. reg = &cache->reg_list[i];
  1143. break;
  1144. }
  1145. }
  1146. if (reg)
  1147. break;
  1148. cache = cache->next;
  1149. }
  1150. if (!reg)
  1151. {
  1152. command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
  1153. return ERROR_OK;
  1154. }
  1155. } else /* access a single register by its name */
  1156. {
  1157. reg = register_get_by_name(target->reg_cache, args[0], 1);
  1158. if (!reg)
  1159. {
  1160. command_print(cmd_ctx, "register %s not found in current target", args[0]);
  1161. return ERROR_OK;
  1162. }
  1163. }
  1164. /* display a register */
  1165. if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
  1166. {
  1167. if ((argc == 2) && (strcmp(args[1], "force") == 0))
  1168. reg->valid = 0;
  1169. if (reg->valid == 0)
  1170. {
  1171. reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
  1172. if (arch_type == NULL)
  1173. {
  1174. ERROR("BUG: encountered unregistered arch type");
  1175. return ERROR_OK;
  1176. }
  1177. arch_type->get(reg);
  1178. }
  1179. value = buf_to_str(reg->value, reg->size, 16);
  1180. command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
  1181. free(value);
  1182. return ERROR_OK;
  1183. }
  1184. /* set register value */
  1185. if (argc == 2)
  1186. {
  1187. u8 *buf = malloc(CEIL(reg->size, 8));
  1188. str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
  1189. reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
  1190. if (arch_type == NULL)
  1191. {
  1192. ERROR("BUG: encountered unregistered arch type");
  1193. return ERROR_OK;
  1194. }
  1195. arch_type->set(reg, buf);
  1196. value = buf_to_str(reg->value, reg->size, 16);
  1197. command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
  1198. free(value);
  1199. free(buf);
  1200. return ERROR_OK;
  1201. }
  1202. command_print(cmd_ctx, "usage: reg <#|name> [value]");
  1203. return ERROR_OK;
  1204. }
  1205. static int wait_state(struct command_context_s *cmd_ctx, char *cmd, enum target_state state, int ms);
  1206. int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1207. {
  1208. target_t *target = get_current_target(cmd_ctx);
  1209. char buffer[512];
  1210. if (argc == 0)
  1211. {
  1212. command_print(cmd_ctx, "target state: %s", target_state_strings[target->type->poll(target)]);
  1213. if (target->state == TARGET_HALTED)
  1214. {
  1215. target->type->arch_state(target, buffer, 512);
  1216. buffer[511] = 0;
  1217. command_print(cmd_ctx, "%s", buffer);
  1218. }
  1219. }
  1220. else
  1221. {
  1222. if (strcmp(args[0], "on") == 0)
  1223. {
  1224. target_continous_poll = 1;
  1225. }
  1226. else if (strcmp(args[0], "off") == 0)
  1227. {
  1228. target_continous_poll = 0;
  1229. }
  1230. else
  1231. {
  1232. command_print(cmd_ctx, "arg is \"on\" or \"off\"");
  1233. }
  1234. }
  1235. return ERROR_OK;
  1236. }
  1237. int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1238. {
  1239. int ms = 5000;
  1240. if (argc > 0)
  1241. {
  1242. char *end;
  1243. ms = strtoul(args[0], &end, 0) * 1000;
  1244. if (*end)
  1245. {
  1246. command_print(cmd_ctx, "usage: %s [seconds]", cmd);
  1247. return ERROR_OK;
  1248. }
  1249. }
  1250. return wait_state(cmd_ctx, cmd, TARGET_HALTED, ms);
  1251. }
  1252. static void target_process_events(struct command_context_s *cmd_ctx)
  1253. {
  1254. target_t *target = get_current_target(cmd_ctx);
  1255. target->type->poll(target);
  1256. target_call_timer_callbacks();
  1257. }
  1258. static int wait_state(struct command_context_s *cmd_ctx, char *cmd, enum target_state state, int ms)
  1259. {
  1260. struct timeval timeout, now;
  1261. gettimeofday(&timeout, NULL);
  1262. timeval_add_time(&timeout, 0, ms * 1000);
  1263. command_print(cmd_ctx, "waiting for target %s...", target_state_strings[state]);
  1264. target_t *target = get_current_target(cmd_ctx);
  1265. while (target->type->poll(target))
  1266. {
  1267. target_call_timer_callbacks();
  1268. if (target->state == state)
  1269. {
  1270. command_print(cmd_ctx, "target %s", target_state_strings[state]);
  1271. break;
  1272. }
  1273. gettimeofday(&now, NULL);
  1274. if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
  1275. {
  1276. command_print(cmd_ctx, "timed out while waiting for target %s", target_state_strings[state]);
  1277. ERROR("timed out while waiting for target %s", target_state_strings[state]);
  1278. break;
  1279. }
  1280. }
  1281. return ERROR_OK;
  1282. }
  1283. int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1284. {
  1285. int retval;
  1286. target_t *target = get_current_target(cmd_ctx);
  1287. DEBUG("-");
  1288. command_print(cmd_ctx, "requesting target halt...");
  1289. if ((retval = target->type->halt(target)) != ERROR_OK)
  1290. {
  1291. switch (retval)
  1292. {
  1293. case ERROR_TARGET_ALREADY_HALTED:
  1294. command_print(cmd_ctx, "target already halted");
  1295. break;
  1296. case ERROR_TARGET_TIMEOUT:
  1297. command_print(cmd_ctx, "target timed out... shutting down");
  1298. return retval;
  1299. default:
  1300. command_print(cmd_ctx, "unknown error... shutting down");
  1301. return retval;
  1302. }
  1303. }
  1304. return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
  1305. }
  1306. /* what to do on daemon startup */
  1307. int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1308. {
  1309. if (argc == 1)
  1310. {
  1311. if (strcmp(args[0], "attach") == 0)
  1312. {
  1313. startup_mode = DAEMON_ATTACH;
  1314. return ERROR_OK;
  1315. }
  1316. else if (strcmp(args[0], "reset") == 0)
  1317. {
  1318. startup_mode = DAEMON_RESET;
  1319. return ERROR_OK;
  1320. }
  1321. }
  1322. WARNING("invalid daemon_startup configuration directive: %s", args[0]);
  1323. return ERROR_OK;
  1324. }
  1325. int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1326. {
  1327. target_t *target = get_current_target(cmd_ctx);
  1328. int retval;
  1329. command_print(cmd_ctx, "requesting target halt and executing a soft reset");
  1330. if ((retval = target->type->soft_reset_halt(target)) != ERROR_OK)
  1331. {
  1332. switch (retval)
  1333. {
  1334. case ERROR_TARGET_TIMEOUT:
  1335. command_print(cmd_ctx, "target timed out... shutting down");
  1336. exit(-1);
  1337. default:
  1338. command_print(cmd_ctx, "unknown error... shutting down");
  1339. exit(-1);
  1340. }
  1341. }
  1342. return ERROR_OK;
  1343. }
  1344. int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1345. {
  1346. target_t *target = get_current_target(cmd_ctx);
  1347. enum target_reset_mode reset_mode = target->reset_mode;
  1348. enum target_reset_mode save = target->reset_mode;
  1349. DEBUG("-");
  1350. if (argc >= 1)
  1351. {
  1352. if (strcmp("run", args[0]) == 0)
  1353. reset_mode = RESET_RUN;
  1354. else if (strcmp("halt", args[0]) == 0)
  1355. reset_mode = RESET_HALT;
  1356. else if (strcmp("init", args[0]) == 0)
  1357. reset_mode = RESET_INIT;
  1358. else if (strcmp("run_and_halt", args[0]) == 0)
  1359. {
  1360. reset_mode = RESET_RUN_AND_HALT;
  1361. if (argc >= 2)
  1362. {
  1363. target->run_and_halt_time = strtoul(args[1], NULL, 0);
  1364. }
  1365. }
  1366. else if (strcmp("run_and_init", args[0]) == 0)
  1367. {
  1368. reset_mode = RESET_RUN_AND_INIT;
  1369. if (argc >= 2)
  1370. {
  1371. target->run_and_halt_time = strtoul(args[1], NULL, 0);
  1372. }
  1373. }
  1374. else
  1375. {
  1376. command_print(cmd_ctx, "usage: reset ['run', 'halt', 'init', 'run_and_halt', 'run_and_init]");
  1377. return ERROR_OK;
  1378. }
  1379. }
  1380. /* temporarily modify mode of current reset target */
  1381. target->reset_mode = reset_mode;
  1382. /* reset *all* targets */
  1383. target_process_reset(cmd_ctx);
  1384. /* Restore default reset mode for this target */
  1385. target->reset_mode = save;
  1386. return ERROR_OK;
  1387. }
  1388. int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1389. {
  1390. int retval;
  1391. target_t *target = get_current_target(cmd_ctx);
  1392. DEBUG("-");
  1393. if (argc == 0)
  1394. retval = target->type->resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
  1395. else if (argc == 1)
  1396. retval = target->type->resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
  1397. else
  1398. {
  1399. command_print(cmd_ctx, "usage: resume [address]");
  1400. return ERROR_OK;
  1401. }
  1402. if (retval != ERROR_OK)
  1403. {
  1404. switch (retval)
  1405. {
  1406. case ERROR_TARGET_NOT_HALTED:
  1407. command_print(cmd_ctx, "target not halted");
  1408. break;
  1409. default:
  1410. command_print(cmd_ctx, "unknown error... shutting down");
  1411. exit(-1);
  1412. }
  1413. }
  1414. target_process_events(cmd_ctx);
  1415. return ERROR_OK;
  1416. }
  1417. int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1418. {
  1419. target_t *target = get_current_target(cmd_ctx);
  1420. DEBUG("-");
  1421. if (argc == 0)
  1422. target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
  1423. if (argc == 1)
  1424. target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
  1425. return ERROR_OK;
  1426. }
  1427. int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1428. {
  1429. const int line_bytecnt = 32;
  1430. int count = 1;
  1431. int size = 4;
  1432. u32 address = 0;
  1433. int line_modulo;
  1434. int i;
  1435. char output[128];
  1436. int output_len;
  1437. int retval;
  1438. u8 *buffer;
  1439. target_t *target = get_current_target(cmd_ctx);
  1440. if (argc < 1)
  1441. return ERROR_OK;
  1442. if (argc == 2)
  1443. count = strtoul(args[1], NULL, 0);
  1444. address = strtoul(args[0], NULL, 0);
  1445. switch (cmd[2])
  1446. {
  1447. case 'w':
  1448. size = 4; line_modulo = line_bytecnt / 4;
  1449. break;
  1450. case 'h':
  1451. size = 2; line_modulo = line_bytecnt / 2;
  1452. break;
  1453. case 'b':
  1454. size = 1; line_modulo = line_bytecnt / 1;
  1455. break;
  1456. default:
  1457. return ERROR_OK;
  1458. }
  1459. buffer = calloc(count, size);
  1460. retval = target->type->read_memory(target, address, size, count, buffer);
  1461. if (retval != ERROR_OK)
  1462. {
  1463. switch (retval)
  1464. {
  1465. case ERROR_TARGET_UNALIGNED_ACCESS:
  1466. command_print(cmd_ctx, "error: address not aligned");
  1467. break;
  1468. case ERROR_TARGET_NOT_HALTED:
  1469. command_print(cmd_ctx, "error: target must be halted for memory accesses");
  1470. break;
  1471. case ERROR_TARGET_DATA_ABORT:
  1472. command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
  1473. break;
  1474. default:
  1475. command_print(cmd_ctx, "error: unknown error");
  1476. break;
  1477. }
  1478. return ERROR_OK;
  1479. }
  1480. output_len = 0;
  1481. for (i = 0; i < count; i++)
  1482. {
  1483. if (i%line_modulo == 0)
  1484. output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
  1485. switch (size)
  1486. {
  1487. case 4:
  1488. output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
  1489. break;
  1490. case 2:
  1491. output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
  1492. break;
  1493. case 1:
  1494. output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
  1495. break;
  1496. }
  1497. if ((i%line_modulo == line_modulo-1) || (i == count - 1))
  1498. {
  1499. command_print(cmd_ctx, output);
  1500. output_len = 0;
  1501. }
  1502. }
  1503. free(buffer);
  1504. return ERROR_OK;
  1505. }
  1506. int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1507. {
  1508. u32 address = 0;
  1509. u32 value = 0;
  1510. int retval;
  1511. target_t *target = get_current_target(cmd_ctx);
  1512. u8 value_buf[4];
  1513. if (argc < 2)
  1514. return ERROR_OK;
  1515. address = strtoul(args[0], NULL, 0);
  1516. value = strtoul(args[1], NULL, 0);
  1517. switch (cmd[2])
  1518. {
  1519. case 'w':
  1520. target_buffer_set_u32(target, value_buf, value);
  1521. retval = target->type->write_memory(target, address, 4, 1, value_buf);
  1522. break;
  1523. case 'h':
  1524. target_buffer_set_u16(target, value_buf, value);
  1525. retval = target->type->write_memory(target, address, 2, 1, value_buf);
  1526. break;
  1527. case 'b':
  1528. value_buf[0] = value;
  1529. retval = target->type->write_memory(target, address, 1, 1, value_buf);
  1530. break;
  1531. default:
  1532. return ERROR_OK;
  1533. }
  1534. switch (retval)
  1535. {
  1536. case ERROR_TARGET_UNALIGNED_ACCESS:
  1537. command_print(cmd_ctx, "error: address not aligned");
  1538. break;
  1539. case ERROR_TARGET_DATA_ABORT:
  1540. command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
  1541. break;
  1542. case ERROR_TARGET_NOT_HALTED:
  1543. command_print(cmd_ctx, "error: target must be halted for memory accesses");
  1544. break;
  1545. case ERROR_OK:
  1546. break;
  1547. default:
  1548. command_print(cmd_ctx, "error: unknown error");
  1549. break;
  1550. }
  1551. return ERROR_OK;
  1552. }
  1553. int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1554. {
  1555. u8 *buffer;
  1556. u32 buf_cnt;
  1557. u32 image_size;
  1558. int i;
  1559. int retval;
  1560. image_t image;
  1561. duration_t duration;
  1562. char *duration_text;
  1563. target_t *target = get_current_target(cmd_ctx);
  1564. if (argc < 1)
  1565. {
  1566. command_print(cmd_ctx, "usage: load_image <filename> [address] [type]");
  1567. return ERROR_OK;
  1568. }
  1569. /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
  1570. if (argc >= 2)
  1571. {
  1572. image.base_address_set = 1;
  1573. image.base_address = strtoul(args[1], NULL, 0);
  1574. }
  1575. else
  1576. {
  1577. image.base_address_set = 0;
  1578. }
  1579. image.start_address_set = 0;
  1580. duration_start_measure(&duration);
  1581. if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
  1582. {
  1583. command_print(cmd_ctx, "load_image error: %s", image.error_str);
  1584. return ERROR_OK;
  1585. }
  1586. image_size = 0x0;
  1587. for (i = 0; i < image.num_sections; i++)
  1588. {
  1589. buffer = malloc(image.sections[i].size);
  1590. if (buffer == NULL)
  1591. {
  1592. command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
  1593. break;
  1594. }
  1595. if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
  1596. {
  1597. ERROR("image_read_section failed with error code: %i", retval);
  1598. command_print(cmd_ctx, "image reading failed, download aborted");
  1599. free(buffer);
  1600. image_close(&image);
  1601. return ERROR_OK;
  1602. }
  1603. target_write_buffer(target, image.sections[i].base_address, buf_cnt, buffer);
  1604. image_size += buf_cnt;
  1605. command_print(cmd_ctx, "%u byte written at address 0x%8.8x", buf_cnt, image.sections[i].base_address);
  1606. free(buffer);
  1607. }
  1608. duration_stop_measure(&duration, &duration_text);
  1609. command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
  1610. free(duration_text);
  1611. image_close(&image);
  1612. return ERROR_OK;
  1613. }
  1614. int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1615. {
  1616. fileio_t fileio;
  1617. u32 address;
  1618. u32 size;
  1619. u8 buffer[560];
  1620. int retval;
  1621. duration_t duration;
  1622. char *duration_text;
  1623. target_t *target = get_current_target(cmd_ctx);
  1624. if (argc != 3)
  1625. {
  1626. command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
  1627. return ERROR_OK;
  1628. }
  1629. address = strtoul(args[1], NULL, 0);
  1630. size = strtoul(args[2], NULL, 0);
  1631. if ((address & 3) || (size & 3))
  1632. {
  1633. command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
  1634. return ERROR_OK;
  1635. }
  1636. if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
  1637. {
  1638. command_print(cmd_ctx, "dump_image error: %s", fileio.error_str);
  1639. return ERROR_OK;
  1640. }
  1641. duration_start_measure(&duration);
  1642. while (size > 0)
  1643. {
  1644. u32 size_written;
  1645. u32 this_run_size = (size > 560) ? 560 : size;
  1646. retval = target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
  1647. if (retval != ERROR_OK)
  1648. {
  1649. command_print(cmd_ctx, "Reading memory failed %d", retval);
  1650. break;
  1651. }
  1652. fileio_write(&fileio, this_run_size, buffer, &size_written);
  1653. size -= this_run_size;
  1654. address += this_run_size;
  1655. }
  1656. fileio_close(&fileio);
  1657. duration_stop_measure(&duration, &duration_text);
  1658. command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
  1659. free(duration_text);
  1660. return ERROR_OK;
  1661. }
  1662. int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1663. {
  1664. u8 *buffer;
  1665. u32 buf_cnt;
  1666. u32 image_size;
  1667. int i;
  1668. int retval;
  1669. u32 checksum = 0;
  1670. u32 mem_checksum = 0;
  1671. image_t image;
  1672. duration_t duration;
  1673. char *duration_text;
  1674. target_t *target = get_current_target(cmd_ctx);
  1675. if (argc < 1)
  1676. {
  1677. command_print(cmd_ctx, "usage: verify_image <file> [offset] [type]");
  1678. return ERROR_OK;
  1679. }
  1680. if (!target)
  1681. {
  1682. ERROR("no target selected");
  1683. return ERROR_OK;
  1684. }
  1685. duration_start_measure(&duration);
  1686. if (argc >= 2)
  1687. {
  1688. image.base_address_set = 1;
  1689. image.base_address = strtoul(args[1], NULL, 0);
  1690. }
  1691. else
  1692. {
  1693. image.base_address_set = 0;
  1694. image.base_address = 0x0;
  1695. }
  1696. image.start_address_set = 0;
  1697. if (image_open(&image, args[0], (argc == 3) ? args[2] : NULL) != ERROR_OK)
  1698. {
  1699. command_print(cmd_ctx, "verify_image error: %s", image.error_str);
  1700. return ERROR_OK;
  1701. }
  1702. image_size = 0x0;
  1703. for (i = 0; i < image.num_sections; i++)
  1704. {
  1705. buffer = malloc(image.sections[i].size);
  1706. if (buffer == NULL)
  1707. {
  1708. command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
  1709. break;
  1710. }
  1711. if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
  1712. {
  1713. ERROR("image_read_section failed with error code: %i", retval);
  1714. command_print(cmd_ctx, "image reading failed, verify aborted");
  1715. free(buffer);
  1716. image_close(&image);
  1717. return ERROR_OK;
  1718. }
  1719. /* calculate checksum of image */
  1720. image_calculate_checksum( buffer, buf_cnt, &checksum );
  1721. retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
  1722. if( retval != ERROR_OK )
  1723. {
  1724. command_print(cmd_ctx, "could not calculate checksum, verify aborted");
  1725. free(buffer);
  1726. image_close(&image);
  1727. return ERROR_OK;
  1728. }
  1729. if( checksum != mem_checksum )
  1730. {
  1731. /* failed crc checksum, fall back to a binary compare */
  1732. u8 *data;
  1733. command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
  1734. data = (u8*)malloc(buf_cnt);
  1735. /* Can we use 32bit word accesses? */
  1736. int size = 1;
  1737. int count = buf_cnt;
  1738. if ((count % 4) == 0)
  1739. {
  1740. size *= 4;
  1741. count /= 4;
  1742. }
  1743. retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
  1744. if (retval == ERROR_OK)
  1745. {
  1746. int t;
  1747. for (t = 0; t < buf_cnt; t++)
  1748. {
  1749. if (data[t] != buffer[t])
  1750. {
  1751. command_print(cmd_ctx, "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n", t + image.sections[i].base_address, data[t], buffer[t]);
  1752. free(data);
  1753. free(buffer);
  1754. image_close(&image);
  1755. return ERROR_OK;
  1756. }
  1757. }
  1758. }
  1759. free(data);
  1760. }
  1761. free(buffer);
  1762. image_size += buf_cnt;
  1763. }
  1764. duration_stop_measure(&duration, &duration_text);
  1765. command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
  1766. free(duration_text);
  1767. image_close(&image);
  1768. return ERROR_OK;
  1769. }
  1770. int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1771. {
  1772. int retval;
  1773. target_t *target = get_current_target(cmd_ctx);
  1774. if (argc == 0)
  1775. {
  1776. breakpoint_t *breakpoint = target->breakpoints;
  1777. while (breakpoint)
  1778. {
  1779. if (breakpoint->type == BKPT_SOFT)
  1780. {
  1781. char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
  1782. command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
  1783. free(buf);
  1784. }
  1785. else
  1786. {
  1787. command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
  1788. }
  1789. breakpoint = breakpoint->next;
  1790. }
  1791. }
  1792. else if (argc >= 2)
  1793. {
  1794. int hw = BKPT_SOFT;
  1795. u32 length = 0;
  1796. length = strtoul(args[1], NULL, 0);
  1797. if (argc >= 3)
  1798. if (strcmp(args[2], "hw") == 0)
  1799. hw = BKPT_HARD;
  1800. if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
  1801. {
  1802. switch (retval)
  1803. {
  1804. case ERROR_TARGET_NOT_HALTED:
  1805. command_print(cmd_ctx, "target must be halted to set breakpoints");
  1806. break;
  1807. case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
  1808. command_print(cmd_ctx, "no more breakpoints available");
  1809. break;
  1810. default:
  1811. command_print(cmd_ctx, "unknown error, breakpoint not set");
  1812. break;
  1813. }
  1814. }
  1815. else
  1816. {
  1817. command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
  1818. }
  1819. }
  1820. else
  1821. {
  1822. command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
  1823. }
  1824. return ERROR_OK;
  1825. }
  1826. int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1827. {
  1828. target_t *target = get_current_target(cmd_ctx);
  1829. if (argc > 0)
  1830. breakpoint_remove(target, strtoul(args[0], NULL, 0));
  1831. return ERROR_OK;
  1832. }
  1833. int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1834. {
  1835. target_t *target = get_current_target(cmd_ctx);
  1836. int retval;
  1837. if (argc == 0)
  1838. {
  1839. watchpoint_t *watchpoint = target->watchpoints;
  1840. while (watchpoint)
  1841. {
  1842. command_print(cmd_ctx, "address: 0x%8.8x, mask: 0x%8.8x, r/w/a: %i, value: 0x%8.8x, mask: 0x%8.8x", watchpoint->address, watchpoint->length, watchpoint->rw, watchpoint->value, watchpoint->mask);
  1843. watchpoint = watchpoint->next;
  1844. }
  1845. }
  1846. else if (argc >= 2)
  1847. {
  1848. enum watchpoint_rw type = WPT_ACCESS;
  1849. u32 data_value = 0x0;
  1850. u32 data_mask = 0xffffffff;
  1851. if (argc >= 3)
  1852. {
  1853. switch(args[2][0])
  1854. {
  1855. case 'r':
  1856. type = WPT_READ;
  1857. break;
  1858. case 'w':
  1859. type = WPT_WRITE;
  1860. break;
  1861. case 'a':
  1862. type = WPT_ACCESS;
  1863. break;
  1864. default:
  1865. command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
  1866. return ERROR_OK;
  1867. }
  1868. }
  1869. if (argc >= 4)
  1870. {
  1871. data_value = strtoul(args[3], NULL, 0);
  1872. }
  1873. if (argc >= 5)
  1874. {
  1875. data_mask = strtoul(args[4], NULL, 0);
  1876. }
  1877. if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
  1878. strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
  1879. {
  1880. switch (retval)
  1881. {
  1882. case ERROR_TARGET_NOT_HALTED:
  1883. command_print(cmd_ctx, "target must be halted to set watchpoints");
  1884. break;
  1885. case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
  1886. command_print(cmd_ctx, "no more watchpoints available");
  1887. break;
  1888. default:
  1889. command_print(cmd_ctx, "unknown error, watchpoint not set");
  1890. break;
  1891. }
  1892. }
  1893. }
  1894. else
  1895. {
  1896. command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
  1897. }
  1898. return ERROR_OK;
  1899. }
  1900. int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1901. {
  1902. target_t *target = get_current_target(cmd_ctx);
  1903. if (argc > 0)
  1904. watchpoint_remove(target, strtoul(args[0], NULL, 0));
  1905. return ERROR_OK;
  1906. }
  1907. int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
  1908. {
  1909. int retval;
  1910. target_t *target = get_current_target(cmd_ctx);
  1911. u32 va;
  1912. u32 pa;
  1913. if (argc != 1)
  1914. {
  1915. return ERROR_COMMAND_SYNTAX_ERROR;
  1916. }
  1917. va = strtoul(args[0], NULL, 0);
  1918. retval = target->type->virt2phys(target, va, &pa);
  1919. if (retval == ERROR_OK)
  1920. {
  1921. command_print(cmd_ctx, "Physical address 0x%08x", pa);
  1922. }
  1923. else
  1924. {
  1925. /* lower levels will have logged a detailed error which is
  1926. * forwarded to telnet/GDB session.
  1927. */
  1928. }
  1929. return retval;
  1930. }