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.
 
 
 
 
 
 

1955 lines
48 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_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  62. int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  63. int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  64. int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  65. /* targets
  66. */
  67. extern target_type_t arm7tdmi_target;
  68. extern target_type_t arm720t_target;
  69. extern target_type_t arm9tdmi_target;
  70. extern target_type_t arm920t_target;
  71. extern target_type_t arm966e_target;
  72. extern target_type_t arm926ejs_target;
  73. extern target_type_t xscale_target;
  74. extern target_type_t cortexm3_target;
  75. target_type_t *target_types[] =
  76. {
  77. &arm7tdmi_target,
  78. &arm9tdmi_target,
  79. &arm920t_target,
  80. &arm720t_target,
  81. &arm966e_target,
  82. &arm926ejs_target,
  83. &xscale_target,
  84. &cortexm3_target,
  85. NULL,
  86. };
  87. target_t *targets = NULL;
  88. target_event_callback_t *target_event_callbacks = NULL;
  89. target_timer_callback_t *target_timer_callbacks = NULL;
  90. char *target_state_strings[] =
  91. {
  92. "unknown",
  93. "running",
  94. "halted",
  95. "reset",
  96. "debug_running",
  97. };
  98. char *target_debug_reason_strings[] =
  99. {
  100. "debug request", "breakpoint", "watchpoint",
  101. "watchpoint and breakpoint", "single step",
  102. "target not halted"
  103. };
  104. char *target_endianess_strings[] =
  105. {
  106. "big endian",
  107. "little endian",
  108. };
  109. enum daemon_startup_mode startup_mode = DAEMON_ATTACH;
  110. static int target_continous_poll = 1;
  111. /* read a u32 from a buffer in target memory endianness */
  112. u32 target_buffer_get_u32(target_t *target, u8 *buffer)
  113. {
  114. if (target->endianness == TARGET_LITTLE_ENDIAN)
  115. return le_to_h_u32(buffer);
  116. else
  117. return be_to_h_u32(buffer);
  118. }
  119. /* read a u16 from a buffer in target memory endianness */
  120. u16 target_buffer_get_u16(target_t *target, u8 *buffer)
  121. {
  122. if (target->endianness == TARGET_LITTLE_ENDIAN)
  123. return le_to_h_u16(buffer);
  124. else
  125. return be_to_h_u16(buffer);
  126. }
  127. /* write a u32 to a buffer in target memory endianness */
  128. void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
  129. {
  130. if (target->endianness == TARGET_LITTLE_ENDIAN)
  131. h_u32_to_le(buffer, value);
  132. else
  133. h_u32_to_be(buffer, value);
  134. }
  135. /* write a u16 to a buffer in target memory endianness */
  136. void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
  137. {
  138. if (target->endianness == TARGET_LITTLE_ENDIAN)
  139. h_u16_to_le(buffer, value);
  140. else
  141. h_u16_to_be(buffer, value);
  142. }
  143. /* returns a pointer to the n-th configured target */
  144. target_t* get_target_by_num(int num)
  145. {
  146. target_t *target = targets;
  147. int i = 0;
  148. while (target)
  149. {
  150. if (num == i)
  151. return target;
  152. target = target->next;
  153. i++;
  154. }
  155. return NULL;
  156. }
  157. int get_num_by_target(target_t *query_target)
  158. {
  159. target_t *target = targets;
  160. int i = 0;
  161. while (target)
  162. {
  163. if (target == query_target)
  164. return i;
  165. target = target->next;
  166. i++;
  167. }
  168. return -1;
  169. }
  170. target_t* get_current_target(command_context_t *cmd_ctx)
  171. {
  172. target_t *target = get_target_by_num(cmd_ctx->current_target);
  173. if (target == NULL)
  174. {
  175. ERROR("BUG: current_target out of bounds");
  176. exit(-1);
  177. }
  178. return target;
  179. }
  180. /* Process target initialization, when target entered debug out of reset
  181. * the handler is unregistered at the end of this function, so it's only called once
  182. */
  183. int target_init_handler(struct target_s *target, enum target_event event, void *priv)
  184. {
  185. FILE *script;
  186. struct command_context_s *cmd_ctx = priv;
  187. if ((event == TARGET_EVENT_HALTED) && (target->reset_script))
  188. {
  189. target_unregister_event_callback(target_init_handler, priv);
  190. script = fopen(target->reset_script, "r");
  191. if (!script)
  192. {
  193. ERROR("couldn't open script file %s", target->reset_script);
  194. return ERROR_OK;
  195. }
  196. INFO("executing reset script '%s'", target->reset_script);
  197. command_run_file(cmd_ctx, script, COMMAND_EXEC);
  198. fclose(script);
  199. jtag_execute_queue();
  200. }
  201. return ERROR_OK;
  202. }
  203. int target_run_and_halt_handler(void *priv)
  204. {
  205. target_t *target = priv;
  206. target->type->halt(target);
  207. return ERROR_OK;
  208. }
  209. int target_process_reset(struct command_context_s *cmd_ctx)
  210. {
  211. int retval = ERROR_OK;
  212. target_t *target;
  213. /* prepare reset_halt where necessary */
  214. target = targets;
  215. while (target)
  216. {
  217. switch (target->reset_mode)
  218. {
  219. case RESET_HALT:
  220. case RESET_INIT:
  221. target->type->prepare_reset_halt(target);
  222. break;
  223. default:
  224. break;
  225. }
  226. target = target->next;
  227. }
  228. target = targets;
  229. while (target)
  230. {
  231. target->type->assert_reset(target);
  232. target = target->next;
  233. }
  234. jtag_execute_queue();
  235. /* request target halt if necessary, and schedule further action */
  236. target = targets;
  237. while (target)
  238. {
  239. switch (target->reset_mode)
  240. {
  241. case RESET_RUN:
  242. /* nothing to do if target just wants to be run */
  243. break;
  244. case RESET_RUN_AND_HALT:
  245. /* schedule halt */
  246. target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
  247. break;
  248. case RESET_RUN_AND_INIT:
  249. /* schedule halt */
  250. target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
  251. target_register_event_callback(target_init_handler, cmd_ctx);
  252. break;
  253. case RESET_HALT:
  254. target->type->halt(target);
  255. break;
  256. case RESET_INIT:
  257. target->type->halt(target);
  258. target_register_event_callback(target_init_handler, cmd_ctx);
  259. break;
  260. default:
  261. ERROR("BUG: unknown target->reset_mode");
  262. }
  263. target = target->next;
  264. }
  265. target = targets;
  266. while (target)
  267. {
  268. target->type->deassert_reset(target);
  269. target = target->next;
  270. }
  271. jtag_execute_queue();
  272. return retval;
  273. }
  274. int target_init(struct command_context_s *cmd_ctx)
  275. {
  276. target_t *target = targets;
  277. while (target)
  278. {
  279. if (target->type->init_target(cmd_ctx, target) != ERROR_OK)
  280. {
  281. ERROR("target '%s' init failed", target->type->name);
  282. exit(-1);
  283. }
  284. target = target->next;
  285. }
  286. if (targets)
  287. {
  288. target_register_user_commands(cmd_ctx);
  289. target_register_timer_callback(handle_target, 100, 1, NULL);
  290. }
  291. if (startup_mode == DAEMON_RESET)
  292. target_process_reset(cmd_ctx);
  293. return ERROR_OK;
  294. }
  295. int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
  296. {
  297. target_event_callback_t **callbacks_p = &target_event_callbacks;
  298. if (callback == NULL)
  299. {
  300. return ERROR_INVALID_ARGUMENTS;
  301. }
  302. if (*callbacks_p)
  303. {
  304. while ((*callbacks_p)->next)
  305. callbacks_p = &((*callbacks_p)->next);
  306. callbacks_p = &((*callbacks_p)->next);
  307. }
  308. (*callbacks_p) = malloc(sizeof(target_event_callback_t));
  309. (*callbacks_p)->callback = callback;
  310. (*callbacks_p)->priv = priv;
  311. (*callbacks_p)->next = NULL;
  312. return ERROR_OK;
  313. }
  314. int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
  315. {
  316. target_timer_callback_t **callbacks_p = &target_timer_callbacks;
  317. struct timeval now;
  318. if (callback == NULL)
  319. {
  320. return ERROR_INVALID_ARGUMENTS;
  321. }
  322. if (*callbacks_p)
  323. {
  324. while ((*callbacks_p)->next)
  325. callbacks_p = &((*callbacks_p)->next);
  326. callbacks_p = &((*callbacks_p)->next);
  327. }
  328. (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
  329. (*callbacks_p)->callback = callback;
  330. (*callbacks_p)->periodic = periodic;
  331. (*callbacks_p)->time_ms = time_ms;
  332. gettimeofday(&now, NULL);
  333. (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
  334. time_ms -= (time_ms % 1000);
  335. (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
  336. if ((*callbacks_p)->when.tv_usec > 1000000)
  337. {
  338. (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
  339. (*callbacks_p)->when.tv_sec += 1;
  340. }
  341. (*callbacks_p)->priv = priv;
  342. (*callbacks_p)->next = NULL;
  343. return ERROR_OK;
  344. }
  345. int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
  346. {
  347. target_event_callback_t **p = &target_event_callbacks;
  348. target_event_callback_t *c = target_event_callbacks;
  349. if (callback == NULL)
  350. {
  351. return ERROR_INVALID_ARGUMENTS;
  352. }
  353. while (c)
  354. {
  355. target_event_callback_t *next = c->next;
  356. if ((c->callback == callback) && (c->priv == priv))
  357. {
  358. *p = next;
  359. free(c);
  360. return ERROR_OK;
  361. }
  362. else
  363. p = &(c->next);
  364. c = next;
  365. }
  366. return ERROR_OK;
  367. }
  368. int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
  369. {
  370. target_timer_callback_t **p = &target_timer_callbacks;
  371. target_timer_callback_t *c = target_timer_callbacks;
  372. if (callback == NULL)
  373. {
  374. return ERROR_INVALID_ARGUMENTS;
  375. }
  376. while (c)
  377. {
  378. target_timer_callback_t *next = c->next;
  379. if ((c->callback == callback) && (c->priv == priv))
  380. {
  381. *p = next;
  382. free(c);
  383. return ERROR_OK;
  384. }
  385. else
  386. p = &(c->next);
  387. c = next;
  388. }
  389. return ERROR_OK;
  390. }
  391. int target_call_event_callbacks(target_t *target, enum target_event event)
  392. {
  393. target_event_callback_t *callback = target_event_callbacks;
  394. target_event_callback_t *next_callback;
  395. DEBUG("target event %i", event);
  396. while (callback)
  397. {
  398. next_callback = callback->next;
  399. callback->callback(target, event, callback->priv);
  400. callback = next_callback;
  401. }
  402. return ERROR_OK;
  403. }
  404. int target_call_timer_callbacks()
  405. {
  406. target_timer_callback_t *callback = target_timer_callbacks;
  407. target_timer_callback_t *next_callback;
  408. struct timeval now;
  409. gettimeofday(&now, NULL);
  410. while (callback)
  411. {
  412. next_callback = callback->next;
  413. if (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
  414. || (now.tv_sec > callback->when.tv_sec))
  415. {
  416. callback->callback(callback->priv);
  417. if (callback->periodic)
  418. {
  419. int time_ms = callback->time_ms;
  420. callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
  421. time_ms -= (time_ms % 1000);
  422. callback->when.tv_sec = now.tv_sec + time_ms / 1000;
  423. if (callback->when.tv_usec > 1000000)
  424. {
  425. callback->when.tv_usec = callback->when.tv_usec - 1000000;
  426. callback->when.tv_sec += 1;
  427. }
  428. }
  429. else
  430. target_unregister_timer_callback(callback->callback, callback->priv);
  431. }
  432. callback = next_callback;
  433. }
  434. return ERROR_OK;
  435. }
  436. int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
  437. {
  438. working_area_t *c = target->working_areas;
  439. working_area_t *new_wa = NULL;
  440. /* only allocate multiples of 4 byte */
  441. if (size % 4)
  442. {
  443. ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
  444. size = CEIL(size, 4);
  445. }
  446. /* see if there's already a matching working area */
  447. while (c)
  448. {
  449. if ((c->free) && (c->size == size))
  450. {
  451. new_wa = c;
  452. break;
  453. }
  454. c = c->next;
  455. }
  456. /* if not, allocate a new one */
  457. if (!new_wa)
  458. {
  459. working_area_t **p = &target->working_areas;
  460. u32 first_free = target->working_area;
  461. u32 free_size = target->working_area_size;
  462. DEBUG("allocating new working area");
  463. c = target->working_areas;
  464. while (c)
  465. {
  466. first_free += c->size;
  467. free_size -= c->size;
  468. p = &c->next;
  469. c = c->next;
  470. }
  471. if (free_size < size)
  472. {
  473. WARNING("not enough working area available");
  474. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  475. }
  476. new_wa = malloc(sizeof(working_area_t));
  477. new_wa->next = NULL;
  478. new_wa->size = size;
  479. new_wa->address = first_free;
  480. if (target->backup_working_area)
  481. {
  482. new_wa->backup = malloc(new_wa->size);
  483. target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup);
  484. }
  485. else
  486. {
  487. new_wa->backup = NULL;
  488. }
  489. /* put new entry in list */
  490. *p = new_wa;
  491. }
  492. /* mark as used, and return the new (reused) area */
  493. new_wa->free = 0;
  494. *area = new_wa;
  495. /* user pointer */
  496. new_wa->user = area;
  497. return ERROR_OK;
  498. }
  499. int target_free_working_area(struct target_s *target, working_area_t *area)
  500. {
  501. if (area->free)
  502. return ERROR_OK;
  503. if (target->backup_working_area)
  504. target->type->write_memory(target, area->address, 4, area->size / 4, area->backup);
  505. area->free = 1;
  506. /* mark user pointer invalid */
  507. *area->user = NULL;
  508. area->user = NULL;
  509. return ERROR_OK;
  510. }
  511. int target_free_all_working_areas(struct target_s *target)
  512. {
  513. working_area_t *c = target->working_areas;
  514. while (c)
  515. {
  516. working_area_t *next = c->next;
  517. target_free_working_area(target, c);
  518. if (c->backup)
  519. free(c->backup);
  520. free(c);
  521. c = next;
  522. }
  523. target->working_areas = NULL;
  524. return ERROR_OK;
  525. }
  526. int target_register_commands(struct command_context_s *cmd_ctx)
  527. {
  528. register_command(cmd_ctx, NULL, "target", handle_target_command, COMMAND_CONFIG, NULL);
  529. register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, NULL);
  530. register_command(cmd_ctx, NULL, "daemon_startup", handle_daemon_startup_command, COMMAND_CONFIG, NULL);
  531. register_command(cmd_ctx, NULL, "target_script", handle_target_script_command, COMMAND_CONFIG, NULL);
  532. register_command(cmd_ctx, NULL, "run_and_halt_time", handle_run_and_halt_time_command, COMMAND_CONFIG, NULL);
  533. register_command(cmd_ctx, NULL, "working_area", handle_working_area_command, COMMAND_CONFIG, NULL);
  534. return ERROR_OK;
  535. }
  536. int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
  537. {
  538. int retval;
  539. DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
  540. /* handle writes of less than 4 byte */
  541. if (size < 4)
  542. {
  543. if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
  544. return retval;
  545. return ERROR_OK;
  546. }
  547. /* handle unaligned head bytes */
  548. if (address % 4)
  549. {
  550. int unaligned = 4 - (address % 4);
  551. if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
  552. return retval;
  553. buffer += unaligned;
  554. address += unaligned;
  555. size -= unaligned;
  556. }
  557. /* handle aligned words */
  558. if (size >= 4)
  559. {
  560. int aligned = size - (size % 4);
  561. /* use bulk writes above a certain limit. This may have to be changed */
  562. if (aligned > 128)
  563. {
  564. if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
  565. return retval;
  566. }
  567. else
  568. {
  569. if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
  570. return retval;
  571. }
  572. buffer += aligned;
  573. address += aligned;
  574. size -= aligned;
  575. }
  576. /* handle tail writes of less than 4 bytes */
  577. if (size > 0)
  578. {
  579. if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
  580. return retval;
  581. }
  582. return ERROR_OK;
  583. }
  584. int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
  585. {
  586. int retval;
  587. DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
  588. /* handle reads of less than 4 byte */
  589. if (size < 4)
  590. {
  591. if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
  592. return retval;
  593. return ERROR_OK;
  594. }
  595. /* handle unaligned head bytes */
  596. if (address % 4)
  597. {
  598. int unaligned = 4 - (address % 4);
  599. if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
  600. return retval;
  601. buffer += unaligned;
  602. address += unaligned;
  603. size -= unaligned;
  604. }
  605. /* handle aligned words */
  606. if (size >= 4)
  607. {
  608. int aligned = size - (size % 4);
  609. if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
  610. return retval;
  611. buffer += aligned;
  612. address += aligned;
  613. size -= aligned;
  614. }
  615. /* handle tail writes of less than 4 bytes */
  616. if (size > 0)
  617. {
  618. if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
  619. return retval;
  620. }
  621. return ERROR_OK;
  622. }
  623. int target_read_u32(struct target_s *target, u32 address, u32 *value)
  624. {
  625. u8 value_buf[4];
  626. int retval = target->type->read_memory(target, address, 4, 1, value_buf);
  627. if (retval == ERROR_OK)
  628. {
  629. *value = target_buffer_get_u32(target, value_buf);
  630. DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
  631. }
  632. else
  633. {
  634. *value = 0x0;
  635. DEBUG("address: 0x%8.8x failed", address);
  636. }
  637. return retval;
  638. }
  639. int target_read_u16(struct target_s *target, u32 address, u16 *value)
  640. {
  641. u8 value_buf[2];
  642. int retval = target->type->read_memory(target, address, 2, 1, value_buf);
  643. if (retval == ERROR_OK)
  644. {
  645. *value = target_buffer_get_u16(target, value_buf);
  646. DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
  647. }
  648. else
  649. {
  650. *value = 0x0;
  651. DEBUG("address: 0x%8.8x failed", address);
  652. }
  653. return retval;
  654. }
  655. int target_read_u8(struct target_s *target, u32 address, u8 *value)
  656. {
  657. int retval = target->type->read_memory(target, address, 1, 1, value);
  658. if (retval == ERROR_OK)
  659. {
  660. DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
  661. }
  662. else
  663. {
  664. *value = 0x0;
  665. DEBUG("address: 0x%8.8x failed", address);
  666. }
  667. return retval;
  668. }
  669. int target_write_u32(struct target_s *target, u32 address, u32 value)
  670. {
  671. int retval;
  672. u8 value_buf[4];
  673. DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
  674. target_buffer_set_u32(target, value_buf, value);
  675. if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
  676. {
  677. DEBUG("failed: %i", retval);
  678. }
  679. return retval;
  680. }
  681. int target_write_u16(struct target_s *target, u32 address, u16 value)
  682. {
  683. int retval;
  684. u8 value_buf[2];
  685. DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
  686. target_buffer_set_u16(target, value_buf, value);
  687. if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
  688. {
  689. DEBUG("failed: %i", retval);
  690. }
  691. return retval;
  692. }
  693. int target_write_u8(struct target_s *target, u32 address, u8 value)
  694. {
  695. int retval;
  696. DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
  697. if ((retval = target->type->read_memory(target, address, 1, 1, &value)) != ERROR_OK)
  698. {
  699. DEBUG("failed: %i", retval);
  700. }
  701. return retval;
  702. }
  703. int target_register_user_commands(struct command_context_s *cmd_ctx)
  704. {
  705. register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, NULL);
  706. register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
  707. register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
  708. register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
  709. register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
  710. register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction");
  711. register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init|run_and_halt|run_and_init]");
  712. register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
  713. register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
  714. register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
  715. register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
  716. register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value>");
  717. register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value>");
  718. register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value>");
  719. register_command(cmd_ctx, NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
  720. register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
  721. register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
  722. register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
  723. register_command(cmd_ctx, NULL, "load_image", handle_load_image_command, COMMAND_EXEC, "load_image <file> <address> ['bin'|'ihex'|'elf'|'s19']");
  724. register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
  725. register_command(cmd_ctx, NULL, "load_binary", handle_load_image_command, COMMAND_EXEC, "[DEPRECATED] load_binary <file> <address>");
  726. register_command(cmd_ctx, NULL, "dump_binary", handle_dump_image_command, COMMAND_EXEC, "[DEPRECATED] dump_binary <file> <address> <size>");
  727. target_request_register_commands(cmd_ctx);
  728. return ERROR_OK;
  729. }
  730. int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  731. {
  732. target_t *target = targets;
  733. int count = 0;
  734. if (argc == 1)
  735. {
  736. int num = strtoul(args[0], NULL, 0);
  737. while (target)
  738. {
  739. count++;
  740. target = target->next;
  741. }
  742. if (num < count)
  743. cmd_ctx->current_target = num;
  744. else
  745. command_print(cmd_ctx, "%i is out of bounds, only %i targets are configured", num, count);
  746. return ERROR_OK;
  747. }
  748. while (target)
  749. {
  750. command_print(cmd_ctx, "%i: %s (%s), state: %s", count++, target->type->name, target_endianess_strings[target->endianness], target_state_strings[target->state]);
  751. target = target->next;
  752. }
  753. return ERROR_OK;
  754. }
  755. int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  756. {
  757. int i;
  758. int found = 0;
  759. if (argc < 3)
  760. {
  761. ERROR("target command requires at least three arguments: <type> <endianess> <reset_mode>");
  762. exit(-1);
  763. }
  764. /* search for the specified target */
  765. if (args[0] && (args[0][0] != 0))
  766. {
  767. for (i = 0; target_types[i]; i++)
  768. {
  769. if (strcmp(args[0], target_types[i]->name) == 0)
  770. {
  771. target_t **last_target_p = &targets;
  772. /* register target specific commands */
  773. if (target_types[i]->register_commands(cmd_ctx) != ERROR_OK)
  774. {
  775. ERROR("couldn't register '%s' commands", args[0]);
  776. exit(-1);
  777. }
  778. if (*last_target_p)
  779. {
  780. while ((*last_target_p)->next)
  781. last_target_p = &((*last_target_p)->next);
  782. last_target_p = &((*last_target_p)->next);
  783. }
  784. *last_target_p = malloc(sizeof(target_t));
  785. (*last_target_p)->type = target_types[i];
  786. if (strcmp(args[1], "big") == 0)
  787. (*last_target_p)->endianness = TARGET_BIG_ENDIAN;
  788. else if (strcmp(args[1], "little") == 0)
  789. (*last_target_p)->endianness = TARGET_LITTLE_ENDIAN;
  790. else
  791. {
  792. ERROR("endianness must be either 'little' or 'big', not '%s'", args[1]);
  793. exit(-1);
  794. }
  795. /* what to do on a target reset */
  796. if (strcmp(args[2], "reset_halt") == 0)
  797. (*last_target_p)->reset_mode = RESET_HALT;
  798. else if (strcmp(args[2], "reset_run") == 0)
  799. (*last_target_p)->reset_mode = RESET_RUN;
  800. else if (strcmp(args[2], "reset_init") == 0)
  801. (*last_target_p)->reset_mode = RESET_INIT;
  802. else if (strcmp(args[2], "run_and_halt") == 0)
  803. (*last_target_p)->reset_mode = RESET_RUN_AND_HALT;
  804. else if (strcmp(args[2], "run_and_init") == 0)
  805. (*last_target_p)->reset_mode = RESET_RUN_AND_INIT;
  806. else
  807. {
  808. ERROR("unknown target startup mode %s", args[2]);
  809. exit(-1);
  810. }
  811. (*last_target_p)->run_and_halt_time = 1000; /* default 1s */
  812. (*last_target_p)->reset_script = NULL;
  813. (*last_target_p)->post_halt_script = NULL;
  814. (*last_target_p)->pre_resume_script = NULL;
  815. (*last_target_p)->working_area = 0x0;
  816. (*last_target_p)->working_area_size = 0x0;
  817. (*last_target_p)->working_areas = NULL;
  818. (*last_target_p)->backup_working_area = 0;
  819. (*last_target_p)->state = TARGET_UNKNOWN;
  820. (*last_target_p)->reg_cache = NULL;
  821. (*last_target_p)->breakpoints = NULL;
  822. (*last_target_p)->watchpoints = NULL;
  823. (*last_target_p)->next = NULL;
  824. (*last_target_p)->arch_info = NULL;
  825. /* initialize trace information */
  826. (*last_target_p)->trace_info = malloc(sizeof(trace_t));
  827. (*last_target_p)->trace_info->num_trace_points = 0;
  828. (*last_target_p)->trace_info->trace_points = NULL;
  829. (*last_target_p)->trace_info->trace_history_size = 0;
  830. (*last_target_p)->trace_info->trace_history = NULL;
  831. (*last_target_p)->type->target_command(cmd_ctx, cmd, args, argc, *last_target_p);
  832. found = 1;
  833. break;
  834. }
  835. }
  836. }
  837. /* no matching target found */
  838. if (!found)
  839. {
  840. ERROR("target '%s' not found", args[0]);
  841. exit(-1);
  842. }
  843. return ERROR_OK;
  844. }
  845. /* usage: target_script <target#> <event> <script_file> */
  846. int handle_target_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  847. {
  848. target_t *target = NULL;
  849. if (argc < 3)
  850. {
  851. ERROR("incomplete target_script command");
  852. exit(-1);
  853. }
  854. target = get_target_by_num(strtoul(args[0], NULL, 0));
  855. if (!target)
  856. {
  857. ERROR("target number '%s' not defined", args[0]);
  858. exit(-1);
  859. }
  860. if (strcmp(args[1], "reset") == 0)
  861. {
  862. if (target->reset_script)
  863. free(target->reset_script);
  864. target->reset_script = strdup(args[2]);
  865. }
  866. else if (strcmp(args[1], "post_halt") == 0)
  867. {
  868. if (target->post_halt_script)
  869. free(target->post_halt_script);
  870. target->post_halt_script = strdup(args[2]);
  871. }
  872. else if (strcmp(args[1], "pre_resume") == 0)
  873. {
  874. if (target->pre_resume_script)
  875. free(target->pre_resume_script);
  876. target->pre_resume_script = strdup(args[2]);
  877. }
  878. else
  879. {
  880. ERROR("unknown event type: '%s", args[1]);
  881. exit(-1);
  882. }
  883. return ERROR_OK;
  884. }
  885. int handle_run_and_halt_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  886. {
  887. target_t *target = NULL;
  888. if (argc < 2)
  889. {
  890. ERROR("incomplete run_and_halt_time command");
  891. exit(-1);
  892. }
  893. target = get_target_by_num(strtoul(args[0], NULL, 0));
  894. if (!target)
  895. {
  896. ERROR("target number '%s' not defined", args[0]);
  897. exit(-1);
  898. }
  899. target->run_and_halt_time = strtoul(args[1], NULL, 0);
  900. return ERROR_OK;
  901. }
  902. int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  903. {
  904. target_t *target = NULL;
  905. if (argc < 4)
  906. {
  907. ERROR("incomplete working_area command. usage: working_area <target#> <address> <size> <'backup'|'nobackup'>");
  908. exit(-1);
  909. }
  910. target = get_target_by_num(strtoul(args[0], NULL, 0));
  911. if (!target)
  912. {
  913. ERROR("target number '%s' not defined", args[0]);
  914. exit(-1);
  915. }
  916. target->working_area = strtoul(args[1], NULL, 0);
  917. target->working_area_size = strtoul(args[2], NULL, 0);
  918. if (strcmp(args[3], "backup") == 0)
  919. {
  920. target->backup_working_area = 1;
  921. }
  922. else if (strcmp(args[3], "nobackup") == 0)
  923. {
  924. target->backup_working_area = 0;
  925. }
  926. else
  927. {
  928. ERROR("unrecognized <backup|nobackup> argument (%s)", args[3]);
  929. exit(-1);
  930. }
  931. return ERROR_OK;
  932. }
  933. /* process target state changes */
  934. int handle_target(void *priv)
  935. {
  936. int retval;
  937. target_t *target = targets;
  938. while (target)
  939. {
  940. /* only poll if target isn't already halted */
  941. if (target->state != TARGET_HALTED)
  942. {
  943. if (target_continous_poll)
  944. if ((retval = target->type->poll(target)) < 0)
  945. {
  946. ERROR("couldn't poll target, exiting");
  947. exit(-1);
  948. }
  949. }
  950. target = target->next;
  951. }
  952. return ERROR_OK;
  953. }
  954. int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  955. {
  956. target_t *target;
  957. reg_t *reg = NULL;
  958. int count = 0;
  959. char *value;
  960. DEBUG("-");
  961. target = get_current_target(cmd_ctx);
  962. /* list all available registers for the current target */
  963. if (argc == 0)
  964. {
  965. reg_cache_t *cache = target->reg_cache;
  966. count = 0;
  967. while(cache)
  968. {
  969. int i;
  970. for (i = 0; i < cache->num_regs; i++)
  971. {
  972. value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
  973. 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);
  974. free(value);
  975. }
  976. cache = cache->next;
  977. }
  978. return ERROR_OK;
  979. }
  980. /* access a single register by its ordinal number */
  981. if ((args[0][0] >= '0') && (args[0][0] <= '9'))
  982. {
  983. int num = strtoul(args[0], NULL, 0);
  984. reg_cache_t *cache = target->reg_cache;
  985. count = 0;
  986. while(cache)
  987. {
  988. int i;
  989. for (i = 0; i < cache->num_regs; i++)
  990. {
  991. if (count++ == num)
  992. {
  993. reg = &cache->reg_list[i];
  994. break;
  995. }
  996. }
  997. if (reg)
  998. break;
  999. cache = cache->next;
  1000. }
  1001. if (!reg)
  1002. {
  1003. command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
  1004. return ERROR_OK;
  1005. }
  1006. } else /* access a single register by its name */
  1007. {
  1008. reg = register_get_by_name(target->reg_cache, args[0], 1);
  1009. if (!reg)
  1010. {
  1011. command_print(cmd_ctx, "register %s not found in current target", args[0]);
  1012. return ERROR_OK;
  1013. }
  1014. }
  1015. /* display a register */
  1016. if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
  1017. {
  1018. if ((argc == 2) && (strcmp(args[1], "force") == 0))
  1019. reg->valid = 0;
  1020. if (reg->valid == 0)
  1021. {
  1022. reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
  1023. if (arch_type == NULL)
  1024. {
  1025. ERROR("BUG: encountered unregistered arch type");
  1026. return ERROR_OK;
  1027. }
  1028. arch_type->get(reg);
  1029. }
  1030. value = buf_to_str(reg->value, reg->size, 16);
  1031. command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
  1032. free(value);
  1033. return ERROR_OK;
  1034. }
  1035. /* set register value */
  1036. if (argc == 2)
  1037. {
  1038. u8 *buf = malloc(CEIL(reg->size, 8));
  1039. str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
  1040. reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
  1041. if (arch_type == NULL)
  1042. {
  1043. ERROR("BUG: encountered unregistered arch type");
  1044. return ERROR_OK;
  1045. }
  1046. arch_type->set(reg, buf);
  1047. value = buf_to_str(reg->value, reg->size, 16);
  1048. command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
  1049. free(value);
  1050. free(buf);
  1051. return ERROR_OK;
  1052. }
  1053. command_print(cmd_ctx, "usage: reg <#|name> [value]");
  1054. return ERROR_OK;
  1055. }
  1056. int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1057. {
  1058. target_t *target = get_current_target(cmd_ctx);
  1059. char buffer[512];
  1060. if (argc == 0)
  1061. {
  1062. command_print(cmd_ctx, "target state: %s", target_state_strings[target->type->poll(target)]);
  1063. if (target->state == TARGET_HALTED)
  1064. {
  1065. target->type->arch_state(target, buffer, 512);
  1066. buffer[511] = 0;
  1067. command_print(cmd_ctx, "%s", buffer);
  1068. }
  1069. }
  1070. else
  1071. {
  1072. if (strcmp(args[0], "on") == 0)
  1073. {
  1074. target_continous_poll = 1;
  1075. }
  1076. else if (strcmp(args[0], "off") == 0)
  1077. {
  1078. target_continous_poll = 0;
  1079. }
  1080. }
  1081. return ERROR_OK;
  1082. }
  1083. int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1084. {
  1085. target_t *target = get_current_target(cmd_ctx);
  1086. struct timeval timeout, now;
  1087. gettimeofday(&timeout, NULL);
  1088. if (!argc)
  1089. timeval_add_time(&timeout, 5, 0);
  1090. else {
  1091. char *end;
  1092. timeval_add_time(&timeout, strtoul(args[0], &end, 0), 0);
  1093. if (*end) {
  1094. command_print(cmd_ctx, "usage: wait_halt [seconds]");
  1095. return ERROR_OK;
  1096. }
  1097. }
  1098. command_print(cmd_ctx, "waiting for target halted...");
  1099. while(target->type->poll(target))
  1100. {
  1101. if (target->state == TARGET_HALTED)
  1102. {
  1103. command_print(cmd_ctx, "target halted");
  1104. break;
  1105. }
  1106. target_call_timer_callbacks();
  1107. gettimeofday(&now, NULL);
  1108. if ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))
  1109. {
  1110. command_print(cmd_ctx, "timed out while waiting for target halt");
  1111. ERROR("timed out while waiting for target halt");
  1112. break;
  1113. }
  1114. }
  1115. return ERROR_OK;
  1116. }
  1117. int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1118. {
  1119. int retval;
  1120. target_t *target = get_current_target(cmd_ctx);
  1121. DEBUG("-");
  1122. command_print(cmd_ctx, "requesting target halt...");
  1123. if ((retval = target->type->halt(target)) != ERROR_OK)
  1124. {
  1125. switch (retval)
  1126. {
  1127. case ERROR_TARGET_ALREADY_HALTED:
  1128. command_print(cmd_ctx, "target already halted");
  1129. break;
  1130. case ERROR_TARGET_TIMEOUT:
  1131. command_print(cmd_ctx, "target timed out... shutting down");
  1132. exit(-1);
  1133. default:
  1134. command_print(cmd_ctx, "unknown error... shutting down");
  1135. exit(-1);
  1136. }
  1137. }
  1138. return ERROR_OK;
  1139. }
  1140. /* what to do on daemon startup */
  1141. int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1142. {
  1143. if (argc == 1)
  1144. {
  1145. if (strcmp(args[0], "attach") == 0)
  1146. {
  1147. startup_mode = DAEMON_ATTACH;
  1148. return ERROR_OK;
  1149. }
  1150. else if (strcmp(args[0], "reset") == 0)
  1151. {
  1152. startup_mode = DAEMON_RESET;
  1153. return ERROR_OK;
  1154. }
  1155. }
  1156. WARNING("invalid daemon_startup configuration directive: %s", args[0]);
  1157. return ERROR_OK;
  1158. }
  1159. int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1160. {
  1161. target_t *target = get_current_target(cmd_ctx);
  1162. int retval;
  1163. command_print(cmd_ctx, "requesting target halt and executing a soft reset");
  1164. if ((retval = target->type->soft_reset_halt(target)) != ERROR_OK)
  1165. {
  1166. switch (retval)
  1167. {
  1168. case ERROR_TARGET_TIMEOUT:
  1169. command_print(cmd_ctx, "target timed out... shutting down");
  1170. exit(-1);
  1171. default:
  1172. command_print(cmd_ctx, "unknown error... shutting down");
  1173. exit(-1);
  1174. }
  1175. }
  1176. return ERROR_OK;
  1177. }
  1178. int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1179. {
  1180. target_t *target = get_current_target(cmd_ctx);
  1181. enum target_reset_mode reset_mode = RESET_RUN;
  1182. DEBUG("-");
  1183. if (argc >= 1)
  1184. {
  1185. if (strcmp("run", args[0]) == 0)
  1186. reset_mode = RESET_RUN;
  1187. else if (strcmp("halt", args[0]) == 0)
  1188. reset_mode = RESET_HALT;
  1189. else if (strcmp("init", args[0]) == 0)
  1190. reset_mode = RESET_INIT;
  1191. else if (strcmp("run_and_halt", args[0]) == 0)
  1192. {
  1193. reset_mode = RESET_RUN_AND_HALT;
  1194. if (argc >= 2)
  1195. {
  1196. target->run_and_halt_time = strtoul(args[1], NULL, 0);
  1197. }
  1198. }
  1199. else if (strcmp("run_and_init", args[0]) == 0)
  1200. {
  1201. reset_mode = RESET_RUN_AND_INIT;
  1202. if (argc >= 2)
  1203. {
  1204. target->run_and_halt_time = strtoul(args[1], NULL, 0);
  1205. }
  1206. }
  1207. else
  1208. {
  1209. command_print(cmd_ctx, "usage: reset ['run', 'halt', 'init', 'run_and_halt', 'run_and_init]");
  1210. return ERROR_OK;
  1211. }
  1212. target->reset_mode = reset_mode;
  1213. }
  1214. target_process_reset(cmd_ctx);
  1215. return ERROR_OK;
  1216. }
  1217. int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1218. {
  1219. int retval;
  1220. target_t *target = get_current_target(cmd_ctx);
  1221. DEBUG("-");
  1222. if (argc == 0)
  1223. retval = target->type->resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
  1224. else if (argc == 1)
  1225. retval = target->type->resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
  1226. else
  1227. {
  1228. command_print(cmd_ctx, "usage: resume [address]");
  1229. return ERROR_OK;
  1230. }
  1231. if (retval != ERROR_OK)
  1232. {
  1233. switch (retval)
  1234. {
  1235. case ERROR_TARGET_NOT_HALTED:
  1236. command_print(cmd_ctx, "target not halted");
  1237. break;
  1238. default:
  1239. command_print(cmd_ctx, "unknown error... shutting down");
  1240. exit(-1);
  1241. }
  1242. }
  1243. return ERROR_OK;
  1244. }
  1245. int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1246. {
  1247. target_t *target = get_current_target(cmd_ctx);
  1248. DEBUG("-");
  1249. if (argc == 0)
  1250. target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
  1251. if (argc == 1)
  1252. target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
  1253. return ERROR_OK;
  1254. }
  1255. int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1256. {
  1257. int count = 1;
  1258. int size = 4;
  1259. u32 address = 0;
  1260. int i;
  1261. char output[128];
  1262. int output_len;
  1263. int retval;
  1264. u8 *buffer;
  1265. target_t *target = get_current_target(cmd_ctx);
  1266. if (argc < 1)
  1267. return ERROR_OK;
  1268. if (argc == 2)
  1269. count = strtoul(args[1], NULL, 0);
  1270. address = strtoul(args[0], NULL, 0);
  1271. switch (cmd[2])
  1272. {
  1273. case 'w':
  1274. size = 4;
  1275. break;
  1276. case 'h':
  1277. size = 2;
  1278. break;
  1279. case 'b':
  1280. size = 1;
  1281. break;
  1282. default:
  1283. return ERROR_OK;
  1284. }
  1285. buffer = calloc(count, size);
  1286. if ((retval = target->type->read_memory(target, address, size, count, buffer)) != ERROR_OK)
  1287. {
  1288. switch (retval)
  1289. {
  1290. case ERROR_TARGET_UNALIGNED_ACCESS:
  1291. command_print(cmd_ctx, "error: address not aligned");
  1292. break;
  1293. case ERROR_TARGET_NOT_HALTED:
  1294. command_print(cmd_ctx, "error: target must be halted for memory accesses");
  1295. break;
  1296. case ERROR_TARGET_DATA_ABORT:
  1297. command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
  1298. break;
  1299. default:
  1300. command_print(cmd_ctx, "error: unknown error");
  1301. break;
  1302. }
  1303. return ERROR_OK;
  1304. }
  1305. output_len = 0;
  1306. for (i = 0; i < count; i++)
  1307. {
  1308. if (i%8 == 0)
  1309. output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
  1310. switch (size)
  1311. {
  1312. case 4:
  1313. output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
  1314. break;
  1315. case 2:
  1316. output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
  1317. break;
  1318. case 1:
  1319. output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
  1320. break;
  1321. }
  1322. if ((i%8 == 7) || (i == count - 1))
  1323. {
  1324. command_print(cmd_ctx, output);
  1325. output_len = 0;
  1326. }
  1327. }
  1328. free(buffer);
  1329. return ERROR_OK;
  1330. }
  1331. int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1332. {
  1333. u32 address = 0;
  1334. u32 value = 0;
  1335. int retval;
  1336. target_t *target = get_current_target(cmd_ctx);
  1337. u8 value_buf[4];
  1338. if (argc < 2)
  1339. return ERROR_OK;
  1340. address = strtoul(args[0], NULL, 0);
  1341. value = strtoul(args[1], NULL, 0);
  1342. switch (cmd[2])
  1343. {
  1344. case 'w':
  1345. target_buffer_set_u32(target, value_buf, value);
  1346. retval = target->type->write_memory(target, address, 4, 1, value_buf);
  1347. break;
  1348. case 'h':
  1349. target_buffer_set_u16(target, value_buf, value);
  1350. retval = target->type->write_memory(target, address, 2, 1, value_buf);
  1351. break;
  1352. case 'b':
  1353. value_buf[0] = value;
  1354. retval = target->type->write_memory(target, address, 1, 1, value_buf);
  1355. break;
  1356. default:
  1357. return ERROR_OK;
  1358. }
  1359. switch (retval)
  1360. {
  1361. case ERROR_TARGET_UNALIGNED_ACCESS:
  1362. command_print(cmd_ctx, "error: address not aligned");
  1363. break;
  1364. case ERROR_TARGET_DATA_ABORT:
  1365. command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
  1366. break;
  1367. case ERROR_TARGET_NOT_HALTED:
  1368. command_print(cmd_ctx, "error: target must be halted for memory accesses");
  1369. break;
  1370. case ERROR_OK:
  1371. break;
  1372. default:
  1373. command_print(cmd_ctx, "error: unknown error");
  1374. break;
  1375. }
  1376. return ERROR_OK;
  1377. }
  1378. int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1379. {
  1380. u8 *buffer;
  1381. u32 buf_cnt;
  1382. u32 image_size;
  1383. int i;
  1384. int retval;
  1385. image_t image;
  1386. duration_t duration;
  1387. char *duration_text;
  1388. target_t *target = get_current_target(cmd_ctx);
  1389. if (argc < 1)
  1390. {
  1391. command_print(cmd_ctx, "usage: load_image <filename> [address] [type]");
  1392. return ERROR_OK;
  1393. }
  1394. /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
  1395. if (argc >= 2)
  1396. {
  1397. image.base_address_set = 1;
  1398. image.base_address = strtoul(args[1], NULL, 0);
  1399. }
  1400. else
  1401. {
  1402. image.base_address_set = 0;
  1403. }
  1404. image.start_address_set = 0;
  1405. duration_start_measure(&duration);
  1406. if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
  1407. {
  1408. command_print(cmd_ctx, "load_image error: %s", image.error_str);
  1409. return ERROR_OK;
  1410. }
  1411. image_size = 0x0;
  1412. for (i = 0; i < image.num_sections; i++)
  1413. {
  1414. buffer = malloc(image.sections[i].size);
  1415. if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
  1416. {
  1417. ERROR("image_read_section failed with error code: %i", retval);
  1418. command_print(cmd_ctx, "image reading failed, download aborted");
  1419. free(buffer);
  1420. image_close(&image);
  1421. return ERROR_OK;
  1422. }
  1423. target_write_buffer(target, image.sections[i].base_address, buf_cnt, buffer);
  1424. image_size += buf_cnt;
  1425. command_print(cmd_ctx, "%u byte written at address 0x%8.8x", buf_cnt, image.sections[i].base_address);
  1426. free(buffer);
  1427. }
  1428. duration_stop_measure(&duration, &duration_text);
  1429. command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
  1430. free(duration_text);
  1431. image_close(&image);
  1432. return ERROR_OK;
  1433. }
  1434. int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1435. {
  1436. fileio_t fileio;
  1437. u32 address;
  1438. u32 size;
  1439. u8 buffer[560];
  1440. duration_t duration;
  1441. char *duration_text;
  1442. target_t *target = get_current_target(cmd_ctx);
  1443. if (argc != 3)
  1444. {
  1445. command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
  1446. return ERROR_OK;
  1447. }
  1448. address = strtoul(args[1], NULL, 0);
  1449. size = strtoul(args[2], NULL, 0);
  1450. if ((address & 3) || (size & 3))
  1451. {
  1452. command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
  1453. return ERROR_OK;
  1454. }
  1455. if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
  1456. {
  1457. command_print(cmd_ctx, "dump_image error: %s", fileio.error_str);
  1458. return ERROR_OK;
  1459. }
  1460. duration_start_measure(&duration);
  1461. while (size > 0)
  1462. {
  1463. u32 size_written;
  1464. u32 this_run_size = (size > 560) ? 560 : size;
  1465. target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
  1466. fileio_write(&fileio, this_run_size, buffer, &size_written);
  1467. size -= this_run_size;
  1468. address += this_run_size;
  1469. }
  1470. fileio_close(&fileio);
  1471. duration_stop_measure(&duration, &duration_text);
  1472. command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
  1473. free(duration_text);
  1474. return ERROR_OK;
  1475. }
  1476. int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1477. {
  1478. int retval;
  1479. target_t *target = get_current_target(cmd_ctx);
  1480. if (argc == 0)
  1481. {
  1482. breakpoint_t *breakpoint = target->breakpoints;
  1483. while (breakpoint)
  1484. {
  1485. if (breakpoint->type == BKPT_SOFT)
  1486. {
  1487. char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
  1488. command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
  1489. free(buf);
  1490. }
  1491. else
  1492. {
  1493. command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
  1494. }
  1495. breakpoint = breakpoint->next;
  1496. }
  1497. }
  1498. else if (argc >= 2)
  1499. {
  1500. int hw = BKPT_SOFT;
  1501. u32 length = 0;
  1502. length = strtoul(args[1], NULL, 0);
  1503. if (argc >= 3)
  1504. if (strcmp(args[2], "hw") == 0)
  1505. hw = BKPT_HARD;
  1506. if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
  1507. {
  1508. switch (retval)
  1509. {
  1510. case ERROR_TARGET_NOT_HALTED:
  1511. command_print(cmd_ctx, "target must be halted to set breakpoints");
  1512. break;
  1513. case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
  1514. command_print(cmd_ctx, "no more breakpoints available");
  1515. break;
  1516. default:
  1517. command_print(cmd_ctx, "unknown error, breakpoint not set");
  1518. break;
  1519. }
  1520. }
  1521. else
  1522. {
  1523. command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
  1524. }
  1525. }
  1526. else
  1527. {
  1528. command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
  1529. }
  1530. return ERROR_OK;
  1531. }
  1532. int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1533. {
  1534. target_t *target = get_current_target(cmd_ctx);
  1535. if (argc > 0)
  1536. breakpoint_remove(target, strtoul(args[0], NULL, 0));
  1537. return ERROR_OK;
  1538. }
  1539. int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1540. {
  1541. target_t *target = get_current_target(cmd_ctx);
  1542. int retval;
  1543. if (argc == 0)
  1544. {
  1545. watchpoint_t *watchpoint = target->watchpoints;
  1546. while (watchpoint)
  1547. {
  1548. 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);
  1549. watchpoint = watchpoint->next;
  1550. }
  1551. }
  1552. else if (argc >= 2)
  1553. {
  1554. enum watchpoint_rw type = WPT_ACCESS;
  1555. u32 data_value = 0x0;
  1556. u32 data_mask = 0xffffffff;
  1557. if (argc >= 3)
  1558. {
  1559. switch(args[2][0])
  1560. {
  1561. case 'r':
  1562. type = WPT_READ;
  1563. break;
  1564. case 'w':
  1565. type = WPT_WRITE;
  1566. break;
  1567. case 'a':
  1568. type = WPT_ACCESS;
  1569. break;
  1570. default:
  1571. command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
  1572. return ERROR_OK;
  1573. }
  1574. }
  1575. if (argc >= 4)
  1576. {
  1577. data_value = strtoul(args[3], NULL, 0);
  1578. }
  1579. if (argc >= 5)
  1580. {
  1581. data_mask = strtoul(args[4], NULL, 0);
  1582. }
  1583. if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
  1584. strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
  1585. {
  1586. switch (retval)
  1587. {
  1588. case ERROR_TARGET_NOT_HALTED:
  1589. command_print(cmd_ctx, "target must be halted to set watchpoints");
  1590. break;
  1591. case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
  1592. command_print(cmd_ctx, "no more watchpoints available");
  1593. break;
  1594. default:
  1595. command_print(cmd_ctx, "unknown error, watchpoint not set");
  1596. break;
  1597. }
  1598. }
  1599. }
  1600. else
  1601. {
  1602. command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
  1603. }
  1604. return ERROR_OK;
  1605. }
  1606. int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1607. {
  1608. target_t *target = get_current_target(cmd_ctx);
  1609. if (argc > 0)
  1610. watchpoint_remove(target, strtoul(args[0], NULL, 0));
  1611. return ERROR_OK;
  1612. }