1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
33 #include "replacements.h"
35 #include "target_request.h"
38 #include "configuration.h"
39 #include "binarybuffer.h"
46 #include <sys/types.h>
54 #include <time_support.h>
59 static int USE_OLD_RESET = 0; // temp
61 int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv);
64 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
66 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
70 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
72 int handle_NEWreset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
77 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
78 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
79 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
80 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
81 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
82 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
83 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
84 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
85 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
86 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc);
87 int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
88 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
89 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
90 static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv);
92 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
93 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
98 extern target_type_t arm7tdmi_target;
99 extern target_type_t arm720t_target;
100 extern target_type_t arm9tdmi_target;
101 extern target_type_t arm920t_target;
102 extern target_type_t arm966e_target;
103 extern target_type_t arm926ejs_target;
104 extern target_type_t feroceon_target;
105 extern target_type_t xscale_target;
106 extern target_type_t cortexm3_target;
107 extern target_type_t arm11_target;
108 extern target_type_t mips_m4k_target;
110 target_type_t *target_types[] =
126 target_t *all_targets = NULL;
127 target_event_callback_t *target_event_callbacks = NULL;
128 target_timer_callback_t *target_timer_callbacks = NULL;
130 const Jim_Nvp nvp_assert[] = {
131 { .name = "assert", NVP_ASSERT },
132 { .name = "deassert", NVP_DEASSERT },
133 { .name = "T", NVP_ASSERT },
134 { .name = "F", NVP_DEASSERT },
135 { .name = "t", NVP_ASSERT },
136 { .name = "f", NVP_DEASSERT },
137 { .name = NULL, .value = -1 }
140 const Jim_Nvp nvp_error_target[] = {
141 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
142 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
143 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
144 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
145 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
146 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
147 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
148 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
149 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
150 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
151 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
152 { .value = -1, .name = NULL }
155 const char *target_strerror_safe( int err )
159 n = Jim_Nvp_value2name_simple( nvp_error_target, err );
160 if( n->name == NULL ){
167 const Jim_Nvp nvp_target_event[] = {
168 { .value = TARGET_EVENT_OLD_pre_reset , .name = "old-pre_reset" },
169 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
170 { .value = TARGET_EVENT_OLD_post_reset , .name = "old-post_reset" },
171 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
174 { .value = TARGET_EVENT_HALTED, .name = "halted" },
175 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
176 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
177 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
179 /* historical name */
181 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
183 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
184 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
185 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
186 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
187 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
188 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
189 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
190 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
191 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
192 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
198 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
199 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-end" },
202 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
203 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
205 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
206 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
209 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
210 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
212 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
213 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
215 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
216 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
217 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
219 { .name = NULL, .value = -1 }
222 const Jim_Nvp nvp_target_state[] = {
223 { .name = "unknown", .value = TARGET_UNKNOWN },
224 { .name = "running", .value = TARGET_RUNNING },
225 { .name = "halted", .value = TARGET_HALTED },
226 { .name = "reset", .value = TARGET_RESET },
227 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
228 { .name = NULL, .value = -1 },
232 const Jim_Nvp nvp_target_debug_reason [] = {
233 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
234 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
235 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
236 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
237 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
238 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
239 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
240 { .name = NULL, .value = -1 },
244 const Jim_Nvp nvp_target_endian[] = {
245 { .name = "big", .value = TARGET_BIG_ENDIAN },
246 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
247 { .name = "be", .value = TARGET_BIG_ENDIAN },
248 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
249 { .name = NULL, .value = -1 },
252 const Jim_Nvp nvp_reset_modes[] = {
253 { .name = "unknown", .value = RESET_UNKNOWN },
254 { .name = "run" , .value = RESET_RUN },
255 { .name = "halt" , .value = RESET_HALT },
256 { .name = "init" , .value = RESET_INIT },
257 { .name = NULL , .value = -1 },
261 max_target_number( void )
269 if( x < t->target_number ){
270 x = (t->target_number)+1;
277 /* determine the number of the new target */
279 new_target_number( void )
284 /* number is 0 based */
288 if( x < t->target_number ){
289 x = t->target_number;
296 static int target_continous_poll = 1;
298 /* read a u32 from a buffer in target memory endianness */
299 u32 target_buffer_get_u32(target_t *target, u8 *buffer)
301 if (target->endianness == TARGET_LITTLE_ENDIAN)
302 return le_to_h_u32(buffer);
304 return be_to_h_u32(buffer);
307 /* read a u16 from a buffer in target memory endianness */
308 u16 target_buffer_get_u16(target_t *target, u8 *buffer)
310 if (target->endianness == TARGET_LITTLE_ENDIAN)
311 return le_to_h_u16(buffer);
313 return be_to_h_u16(buffer);
316 /* read a u8 from a buffer in target memory endianness */
317 u8 target_buffer_get_u8(target_t *target, u8 *buffer)
319 return *buffer & 0x0ff;
322 /* write a u32 to a buffer in target memory endianness */
323 void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
325 if (target->endianness == TARGET_LITTLE_ENDIAN)
326 h_u32_to_le(buffer, value);
328 h_u32_to_be(buffer, value);
331 /* write a u16 to a buffer in target memory endianness */
332 void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
334 if (target->endianness == TARGET_LITTLE_ENDIAN)
335 h_u16_to_le(buffer, value);
337 h_u16_to_be(buffer, value);
340 /* write a u8 to a buffer in target memory endianness */
341 void target_buffer_set_u8(target_t *target, u8 *buffer, u8 value)
346 /* returns a pointer to the n-th configured target */
347 target_t* get_target_by_num(int num)
349 target_t *target = all_targets;
352 if( target->target_number == num ){
355 target = target->next;
361 int get_num_by_target(target_t *query_target)
363 return query_target->target_number;
366 target_t* get_current_target(command_context_t *cmd_ctx)
368 target_t *target = get_target_by_num(cmd_ctx->current_target);
372 LOG_ERROR("BUG: current_target out of bounds");
380 int target_poll(struct target_s *target)
382 /* We can't poll until after examine */
383 if (!target->type->examined)
385 /* Fail silently lest we pollute the log */
388 return target->type->poll(target);
391 int target_halt(struct target_s *target)
393 /* We can't poll until after examine */
394 if (!target->type->examined)
396 LOG_ERROR("Target not examined yet");
399 return target->type->halt(target);
402 int target_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
406 /* We can't poll until after examine */
407 if (!target->type->examined)
409 LOG_ERROR("Target not examined yet");
413 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
414 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
417 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
424 static int NEW_target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
428 n = Jim_Nvp_value2name_simple( nvp_reset_modes, reset_mode );
429 if( n->name == NULL ){
430 LOG_ERROR("invalid reset mode");
434 sprintf( buf, "ocd_process_reset %s", n->name );
435 Jim_Eval( interp, buf );
437 /* We want any events to be processed before the prompt */
438 target_call_timer_callbacks_now();
443 // Next patch - this turns into TCL...
444 static int OLD_target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
446 int retval = ERROR_OK;
449 target = all_targets;
451 target_all_handle_event( TARGET_EVENT_OLD_pre_reset );
453 if ((retval = jtag_init_reset(cmd_ctx)) != ERROR_OK)
456 keep_alive(); /* we might be running on a very slow JTAG clk */
458 /* First time this is executed after launching OpenOCD, it will read out
459 * the type of CPU, etc. and init Embedded ICE registers in host
462 * It will also set up ICE registers in the target.
464 * However, if we assert TRST later, we need to set up the registers again.
466 * For the "reset halt/init" case we must only set up the registers here.
468 if ((retval = target_examine()) != ERROR_OK)
471 keep_alive(); /* we might be running on a very slow JTAG clk */
473 target = all_targets;
476 /* we have no idea what state the target is in, so we
477 * have to drop working areas
479 target_free_all_working_areas_restore(target, 0);
480 target->reset_halt=((reset_mode==RESET_HALT)||(reset_mode==RESET_INIT));
481 if ((retval = target->type->assert_reset(target))!=ERROR_OK)
483 target = target->next;
486 target = all_targets;
489 if ((retval = target->type->deassert_reset(target))!=ERROR_OK)
491 target = target->next;
494 target = all_targets;
497 /* We can fail to bring the target into the halted state, try after reset has been deasserted */
498 if (target->reset_halt)
500 /* wait up to 1 second for halt. */
501 target_wait_state(target, TARGET_HALTED, 1000);
502 if (target->state != TARGET_HALTED)
504 LOG_WARNING("Failed to reset target into halted mode - issuing halt");
505 if ((retval = target->type->halt(target))!=ERROR_OK)
510 target = target->next;
514 LOG_DEBUG("Waiting for halted stated as appropriate");
516 if ((reset_mode == RESET_HALT) || (reset_mode == RESET_INIT))
518 target = all_targets;
521 /* Wait for reset to complete, maximum 5 seconds. */
522 if (((retval=target_wait_state(target, TARGET_HALTED, 5000)))==ERROR_OK)
524 if (reset_mode == RESET_INIT){
525 target_handle_event( target, TARGET_EVENT_OLD_post_reset );
529 target = target->next;
533 /* We want any events to be processed before the prompt */
534 target_call_timer_callbacks_now();
539 int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
542 return OLD_target_process_reset( cmd_ctx, reset_mode );
544 return NEW_target_process_reset( cmd_ctx, reset_mode );
549 static int default_virt2phys(struct target_s *target, u32 virtual, u32 *physical)
555 static int default_mmu(struct target_s *target, int *enabled)
561 static int default_examine(struct target_s *target)
563 target->type->examined = 1;
568 /* Targets that correctly implement init+examine, i.e.
569 * no communication with target during init:
573 int target_examine(void)
575 int retval = ERROR_OK;
576 target_t *target = all_targets;
579 if ((retval = target->type->examine(target))!=ERROR_OK)
581 target = target->next;
586 static int target_write_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
588 if (!target->type->examined)
590 LOG_ERROR("Target not examined yet");
593 return target->type->write_memory_imp(target, address, size, count, buffer);
596 static int target_read_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
598 if (!target->type->examined)
600 LOG_ERROR("Target not examined yet");
603 return target->type->read_memory_imp(target, address, size, count, buffer);
606 static int target_soft_reset_halt_imp(struct target_s *target)
608 if (!target->type->examined)
610 LOG_ERROR("Target not examined yet");
613 return target->type->soft_reset_halt_imp(target);
616 static int target_run_algorithm_imp(struct target_s *target, int num_mem_params, mem_param_t *mem_params, int num_reg_params, reg_param_t *reg_param, u32 entry_point, u32 exit_point, int timeout_ms, void *arch_info)
618 if (!target->type->examined)
620 LOG_ERROR("Target not examined yet");
623 return target->type->run_algorithm_imp(target, num_mem_params, mem_params, num_reg_params, reg_param, entry_point, exit_point, timeout_ms, arch_info);
626 int target_init(struct command_context_s *cmd_ctx)
628 target_t *target = all_targets;
632 target->type->examined = 0;
633 if (target->type->examine == NULL)
635 target->type->examine = default_examine;
638 if (target->type->init_target(cmd_ctx, target) != ERROR_OK)
640 LOG_ERROR("target '%s' init failed", target->type->name);
644 /* Set up default functions if none are provided by target */
645 if (target->type->virt2phys == NULL)
647 target->type->virt2phys = default_virt2phys;
649 target->type->virt2phys = default_virt2phys;
650 /* a non-invasive way(in terms of patches) to add some code that
651 * runs before the type->write/read_memory implementation
653 target->type->write_memory_imp = target->type->write_memory;
654 target->type->write_memory = target_write_memory_imp;
655 target->type->read_memory_imp = target->type->read_memory;
656 target->type->read_memory = target_read_memory_imp;
657 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
658 target->type->soft_reset_halt = target_soft_reset_halt_imp;
659 target->type->run_algorithm_imp = target->type->run_algorithm;
660 target->type->run_algorithm = target_run_algorithm_imp;
663 if (target->type->mmu == NULL)
665 target->type->mmu = default_mmu;
667 target = target->next;
672 target_register_user_commands(cmd_ctx);
673 target_register_timer_callback(handle_target, 100, 1, NULL);
679 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
681 target_event_callback_t **callbacks_p = &target_event_callbacks;
683 if (callback == NULL)
685 return ERROR_INVALID_ARGUMENTS;
690 while ((*callbacks_p)->next)
691 callbacks_p = &((*callbacks_p)->next);
692 callbacks_p = &((*callbacks_p)->next);
695 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
696 (*callbacks_p)->callback = callback;
697 (*callbacks_p)->priv = priv;
698 (*callbacks_p)->next = NULL;
703 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
705 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
708 if (callback == NULL)
710 return ERROR_INVALID_ARGUMENTS;
715 while ((*callbacks_p)->next)
716 callbacks_p = &((*callbacks_p)->next);
717 callbacks_p = &((*callbacks_p)->next);
720 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
721 (*callbacks_p)->callback = callback;
722 (*callbacks_p)->periodic = periodic;
723 (*callbacks_p)->time_ms = time_ms;
725 gettimeofday(&now, NULL);
726 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
727 time_ms -= (time_ms % 1000);
728 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
729 if ((*callbacks_p)->when.tv_usec > 1000000)
731 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
732 (*callbacks_p)->when.tv_sec += 1;
735 (*callbacks_p)->priv = priv;
736 (*callbacks_p)->next = NULL;
741 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
743 target_event_callback_t **p = &target_event_callbacks;
744 target_event_callback_t *c = target_event_callbacks;
746 if (callback == NULL)
748 return ERROR_INVALID_ARGUMENTS;
753 target_event_callback_t *next = c->next;
754 if ((c->callback == callback) && (c->priv == priv))
768 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
770 target_timer_callback_t **p = &target_timer_callbacks;
771 target_timer_callback_t *c = target_timer_callbacks;
773 if (callback == NULL)
775 return ERROR_INVALID_ARGUMENTS;
780 target_timer_callback_t *next = c->next;
781 if ((c->callback == callback) && (c->priv == priv))
795 int target_call_event_callbacks(target_t *target, enum target_event event)
797 target_event_callback_t *callback = target_event_callbacks;
798 target_event_callback_t *next_callback;
800 LOG_DEBUG("target event %i (%s)",
802 Jim_Nvp_value2name_simple( nvp_target_event, event )->name );
804 target_handle_event( target, event );
808 next_callback = callback->next;
809 callback->callback(target, event, callback->priv);
810 callback = next_callback;
816 static int target_call_timer_callbacks_check_time(int checktime)
818 target_timer_callback_t *callback = target_timer_callbacks;
819 target_timer_callback_t *next_callback;
824 gettimeofday(&now, NULL);
828 next_callback = callback->next;
830 if ((!checktime&&callback->periodic)||
831 (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
832 || (now.tv_sec > callback->when.tv_sec)))
834 if(callback->callback != NULL)
836 callback->callback(callback->priv);
837 if (callback->periodic)
839 int time_ms = callback->time_ms;
840 callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
841 time_ms -= (time_ms % 1000);
842 callback->when.tv_sec = now.tv_sec + time_ms / 1000;
843 if (callback->when.tv_usec > 1000000)
845 callback->when.tv_usec = callback->when.tv_usec - 1000000;
846 callback->when.tv_sec += 1;
850 target_unregister_timer_callback(callback->callback, callback->priv);
854 callback = next_callback;
860 int target_call_timer_callbacks(void)
862 return target_call_timer_callbacks_check_time(1);
865 /* invoke periodic callbacks immediately */
866 int target_call_timer_callbacks_now(void)
868 return target_call_timer_callbacks_check_time(0);
871 int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
873 working_area_t *c = target->working_areas;
874 working_area_t *new_wa = NULL;
876 /* Reevaluate working area address based on MMU state*/
877 if (target->working_areas == NULL)
881 retval = target->type->mmu(target, &enabled);
882 if (retval != ERROR_OK)
888 target->working_area = target->working_area_virt;
892 target->working_area = target->working_area_phys;
896 /* only allocate multiples of 4 byte */
899 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
900 size = CEIL(size, 4);
903 /* see if there's already a matching working area */
906 if ((c->free) && (c->size == size))
914 /* if not, allocate a new one */
917 working_area_t **p = &target->working_areas;
918 u32 first_free = target->working_area;
919 u32 free_size = target->working_area_size;
921 LOG_DEBUG("allocating new working area");
923 c = target->working_areas;
926 first_free += c->size;
927 free_size -= c->size;
932 if (free_size < size)
934 LOG_WARNING("not enough working area available(requested %d, free %d)", size, free_size);
935 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
938 new_wa = malloc(sizeof(working_area_t));
941 new_wa->address = first_free;
943 if (target->backup_working_area)
945 new_wa->backup = malloc(new_wa->size);
946 target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup);
950 new_wa->backup = NULL;
953 /* put new entry in list */
957 /* mark as used, and return the new (reused) area */
967 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
972 if (restore&&target->backup_working_area)
973 target->type->write_memory(target, area->address, 4, area->size / 4, area->backup);
977 /* mark user pointer invalid */
984 int target_free_working_area(struct target_s *target, working_area_t *area)
986 return target_free_working_area_restore(target, area, 1);
989 int target_free_all_working_areas_restore(struct target_s *target, int restore)
991 working_area_t *c = target->working_areas;
995 working_area_t *next = c->next;
996 target_free_working_area_restore(target, c, restore);
1006 target->working_areas = NULL;
1011 int target_free_all_working_areas(struct target_s *target)
1013 return target_free_all_working_areas_restore(target, 1);
1016 int target_register_commands(struct command_context_s *cmd_ctx)
1019 register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, NULL);
1020 register_command(cmd_ctx, NULL, "working_area", handle_working_area_command, COMMAND_ANY, "working_area <target#> <address> <size> <'backup'|'nobackup'> [virtual address]");
1021 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "virt2phys <virtual address>");
1022 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "PRELIMINARY! - profile <seconds> <gmon.out>");
1024 register_jim(cmd_ctx, "target", jim_target, "configure target" );
1027 /* script procedures */
1028 register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array, "read memory and return as a TCL array for script processing");
1029 register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem, "convert a TCL array to memory locations and write the values");
1033 int target_arch_state(struct target_s *target)
1038 LOG_USER("No target has been configured");
1042 LOG_USER("target state: %s",
1043 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name);
1045 if (target->state!=TARGET_HALTED)
1048 retval=target->type->arch_state(target);
1052 /* Single aligned words are guaranteed to use 16 or 32 bit access
1053 * mode respectively, otherwise data is handled as quickly as
1056 int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
1059 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
1061 if (!target->type->examined)
1063 LOG_ERROR("Target not examined yet");
1067 if ((address + size - 1) < address)
1069 /* GDB can request this when e.g. PC is 0xfffffffc*/
1070 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
1074 if (((address % 2) == 0) && (size == 2))
1076 return target->type->write_memory(target, address, 2, 1, buffer);
1079 /* handle unaligned head bytes */
1082 int unaligned = 4 - (address % 4);
1084 if (unaligned > size)
1087 if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1090 buffer += unaligned;
1091 address += unaligned;
1095 /* handle aligned words */
1098 int aligned = size - (size % 4);
1100 /* use bulk writes above a certain limit. This may have to be changed */
1103 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1108 if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1117 /* handle tail writes of less than 4 bytes */
1120 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1128 /* Single aligned words are guaranteed to use 16 or 32 bit access
1129 * mode respectively, otherwise data is handled as quickly as
1132 int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
1135 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
1137 if (!target->type->examined)
1139 LOG_ERROR("Target not examined yet");
1143 if ((address + size - 1) < address)
1145 /* GDB can request this when e.g. PC is 0xfffffffc*/
1146 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
1150 if (((address % 2) == 0) && (size == 2))
1152 return target->type->read_memory(target, address, 2, 1, buffer);
1155 /* handle unaligned head bytes */
1158 int unaligned = 4 - (address % 4);
1160 if (unaligned > size)
1163 if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1166 buffer += unaligned;
1167 address += unaligned;
1171 /* handle aligned words */
1174 int aligned = size - (size % 4);
1176 if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1184 /* handle tail writes of less than 4 bytes */
1187 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1194 int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* crc)
1200 if (!target->type->examined)
1202 LOG_ERROR("Target not examined yet");
1206 if ((retval = target->type->checksum_memory(target, address,
1207 size, &checksum)) == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1209 buffer = malloc(size);
1212 LOG_ERROR("error allocating buffer for section (%d bytes)", size);
1213 return ERROR_INVALID_ARGUMENTS;
1215 retval = target_read_buffer(target, address, size, buffer);
1216 if (retval != ERROR_OK)
1222 /* convert to target endianess */
1223 for (i = 0; i < (size/sizeof(u32)); i++)
1226 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(u32)]);
1227 target_buffer_set_u32(target, &buffer[i*sizeof(u32)], target_data);
1230 retval = image_calculate_checksum( buffer, size, &checksum );
1239 int target_blank_check_memory(struct target_s *target, u32 address, u32 size, u32* blank)
1242 if (!target->type->examined)
1244 LOG_ERROR("Target not examined yet");
1248 if (target->type->blank_check_memory == 0)
1249 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1251 retval = target->type->blank_check_memory(target, address, size, blank);
1256 int target_read_u32(struct target_s *target, u32 address, u32 *value)
1259 if (!target->type->examined)
1261 LOG_ERROR("Target not examined yet");
1265 int retval = target->type->read_memory(target, address, 4, 1, value_buf);
1267 if (retval == ERROR_OK)
1269 *value = target_buffer_get_u32(target, value_buf);
1270 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
1275 LOG_DEBUG("address: 0x%8.8x failed", address);
1281 int target_read_u16(struct target_s *target, u32 address, u16 *value)
1284 if (!target->type->examined)
1286 LOG_ERROR("Target not examined yet");
1290 int retval = target->type->read_memory(target, address, 2, 1, value_buf);
1292 if (retval == ERROR_OK)
1294 *value = target_buffer_get_u16(target, value_buf);
1295 LOG_DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
1300 LOG_DEBUG("address: 0x%8.8x failed", address);
1306 int target_read_u8(struct target_s *target, u32 address, u8 *value)
1308 int retval = target->type->read_memory(target, address, 1, 1, value);
1309 if (!target->type->examined)
1311 LOG_ERROR("Target not examined yet");
1315 if (retval == ERROR_OK)
1317 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
1322 LOG_DEBUG("address: 0x%8.8x failed", address);
1328 int target_write_u32(struct target_s *target, u32 address, u32 value)
1332 if (!target->type->examined)
1334 LOG_ERROR("Target not examined yet");
1338 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1340 target_buffer_set_u32(target, value_buf, value);
1341 if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1343 LOG_DEBUG("failed: %i", retval);
1349 int target_write_u16(struct target_s *target, u32 address, u16 value)
1353 if (!target->type->examined)
1355 LOG_ERROR("Target not examined yet");
1359 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1361 target_buffer_set_u16(target, value_buf, value);
1362 if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1364 LOG_DEBUG("failed: %i", retval);
1370 int target_write_u8(struct target_s *target, u32 address, u8 value)
1373 if (!target->type->examined)
1375 LOG_ERROR("Target not examined yet");
1379 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
1381 if ((retval = target->type->read_memory(target, address, 1, 1, &value)) != ERROR_OK)
1383 LOG_DEBUG("failed: %i", retval);
1389 int target_register_user_commands(struct command_context_s *cmd_ctx)
1391 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, NULL);
1392 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1393 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1394 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1395 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1396 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1397 register_command(cmd_ctx, NULL, "NEWreset", handle_NEWreset_command, COMMAND_EXEC, "reset target [run|halt|init] - default is run");
1398 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "OLDreset target [run|halt|init] - default is run");
1399 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1401 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
1402 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
1403 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
1405 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value> [count]");
1406 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value> [count]");
1407 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value> [count]");
1409 register_command(cmd_ctx, NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
1410 register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
1411 register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
1412 register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
1414 register_command(cmd_ctx, NULL, "load_image", handle_load_image_command, COMMAND_EXEC, "load_image <file> <address> ['bin'|'ihex'|'elf'|'s19'] [min_address] [max_length]");
1415 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1416 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1418 target_request_register_commands(cmd_ctx);
1419 trace_register_commands(cmd_ctx);
1424 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1427 target_t *target = all_targets;
1431 /* try as tcltarget name */
1432 for( target = all_targets ; target ; target++ ){
1433 if( target->cmd_name ){
1434 if( 0 == strcmp( args[0], target->cmd_name ) ){
1440 /* no match, try as number */
1442 int num = strtoul(args[0], &cp, 0 );
1444 /* then it was not a number */
1445 command_print( cmd_ctx, "Target: %s unknown, try one of:\n", args[0] );
1449 target = get_target_by_num( num );
1450 if( target == NULL ){
1451 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0] );
1455 cmd_ctx->current_target = target->target_number;
1460 command_print(cmd_ctx, " CmdName Type Endian ChainPos State ");
1461 command_print(cmd_ctx, "-- ---------- ---------- ---------- -------- ----------");
1464 /* XX: abcdefghij abcdefghij abcdefghij abcdefghij */
1465 command_print(cmd_ctx, "%2d: %-10s %-10s %-10s %8d %s",
1466 target->target_number,
1469 Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness )->name,
1470 target->chain_position,
1471 Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name );
1472 target = target->next;
1480 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1482 target_t *target = NULL;
1484 if ((argc < 4) || (argc > 5))
1486 return ERROR_COMMAND_SYNTAX_ERROR;
1489 target = get_target_by_num(strtoul(args[0], NULL, 0));
1492 return ERROR_COMMAND_SYNTAX_ERROR;
1494 target_free_all_working_areas(target);
1496 target->working_area_phys = target->working_area_virt = strtoul(args[1], NULL, 0);
1499 target->working_area_virt = strtoul(args[4], NULL, 0);
1501 target->working_area_size = strtoul(args[2], NULL, 0);
1503 if (strcmp(args[3], "backup") == 0)
1505 target->backup_working_area = 1;
1507 else if (strcmp(args[3], "nobackup") == 0)
1509 target->backup_working_area = 0;
1513 LOG_ERROR("unrecognized <backup|nobackup> argument (%s)", args[3]);
1514 return ERROR_COMMAND_SYNTAX_ERROR;
1521 /* process target state changes */
1522 int handle_target(void *priv)
1524 target_t *target = all_targets;
1528 if (target_continous_poll)
1530 /* polling may fail silently until the target has been examined */
1531 target_poll(target);
1534 target = target->next;
1540 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1549 target = get_current_target(cmd_ctx);
1551 /* list all available registers for the current target */
1554 reg_cache_t *cache = target->reg_cache;
1560 for (i = 0; i < cache->num_regs; i++)
1562 value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
1563 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);
1566 cache = cache->next;
1572 /* access a single register by its ordinal number */
1573 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1575 int num = strtoul(args[0], NULL, 0);
1576 reg_cache_t *cache = target->reg_cache;
1582 for (i = 0; i < cache->num_regs; i++)
1586 reg = &cache->reg_list[i];
1592 cache = cache->next;
1597 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1600 } else /* access a single register by its name */
1602 reg = register_get_by_name(target->reg_cache, args[0], 1);
1606 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1611 /* display a register */
1612 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1614 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1617 if (reg->valid == 0)
1619 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1620 if (arch_type == NULL)
1622 LOG_ERROR("BUG: encountered unregistered arch type");
1625 arch_type->get(reg);
1627 value = buf_to_str(reg->value, reg->size, 16);
1628 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1633 /* set register value */
1636 u8 *buf = malloc(CEIL(reg->size, 8));
1637 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1639 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1640 if (arch_type == NULL)
1642 LOG_ERROR("BUG: encountered unregistered arch type");
1646 arch_type->set(reg, buf);
1648 value = buf_to_str(reg->value, reg->size, 16);
1649 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1657 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1663 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1665 target_t *target = get_current_target(cmd_ctx);
1669 target_poll(target);
1670 target_arch_state(target);
1674 if (strcmp(args[0], "on") == 0)
1676 target_continous_poll = 1;
1678 else if (strcmp(args[0], "off") == 0)
1680 target_continous_poll = 0;
1684 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1692 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1700 ms = strtoul(args[0], &end, 0) * 1000;
1703 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1707 target_t *target = get_current_target(cmd_ctx);
1709 return target_wait_state(target, TARGET_HALTED, ms);
1712 int target_wait_state(target_t *target, enum target_state state, int ms)
1715 struct timeval timeout, now;
1717 gettimeofday(&timeout, NULL);
1718 timeval_add_time(&timeout, 0, ms * 1000);
1722 if ((retval=target_poll(target))!=ERROR_OK)
1725 if (target->state == state)
1732 LOG_DEBUG("waiting for target %s...",
1733 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1736 gettimeofday(&now, NULL);
1737 if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
1739 LOG_ERROR("timed out while waiting for target %s",
1740 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1748 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1751 target_t *target = get_current_target(cmd_ctx);
1755 if ((retval = target_halt(target)) != ERROR_OK)
1760 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1763 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1765 target_t *target = get_current_target(cmd_ctx);
1767 LOG_USER("requesting target halt and executing a soft reset");
1769 target->type->soft_reset_halt(target);
1774 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1777 enum target_reset_mode reset_mode = RESET_RUN;
1781 n = Jim_Nvp_name2value_simple( nvp_reset_modes, args[0] );
1782 if( (n->name == NULL) || (n->value == RESET_UNKNOWN) ){
1783 return ERROR_COMMAND_SYNTAX_ERROR;
1785 reset_mode = n->value;
1788 /* reset *all* targets */
1789 return target_process_reset(cmd_ctx, reset_mode);
1792 int handle_NEWreset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1798 x = strtol( args[0], &cp, 0 );
1800 command_print( cmd_ctx, "Not numeric: %s\n", args[0] );
1801 return ERROR_COMMAND_SYNTAX_ERROR;
1803 USE_OLD_RESET = !!x;
1805 command_print( cmd_ctx, "reset method: %d (%s)\n",
1807 USE_OLD_RESET ? "old-method" : "new-method" );
1811 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1814 target_t *target = get_current_target(cmd_ctx);
1816 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1819 retval = target_resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1821 retval = target_resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1824 retval = ERROR_COMMAND_SYNTAX_ERROR;
1830 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1832 target_t *target = get_current_target(cmd_ctx);
1837 target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1840 target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1845 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1847 const int line_bytecnt = 32;
1860 target_t *target = get_current_target(cmd_ctx);
1866 count = strtoul(args[1], NULL, 0);
1868 address = strtoul(args[0], NULL, 0);
1874 size = 4; line_modulo = line_bytecnt / 4;
1877 size = 2; line_modulo = line_bytecnt / 2;
1880 size = 1; line_modulo = line_bytecnt / 1;
1886 buffer = calloc(count, size);
1887 retval = target->type->read_memory(target, address, size, count, buffer);
1888 if (retval == ERROR_OK)
1892 for (i = 0; i < count; i++)
1894 if (i%line_modulo == 0)
1895 output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1900 output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
1903 output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
1906 output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
1910 if ((i%line_modulo == line_modulo-1) || (i == count - 1))
1912 command_print(cmd_ctx, output);
1923 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1930 target_t *target = get_current_target(cmd_ctx);
1933 if ((argc < 2) || (argc > 3))
1934 return ERROR_COMMAND_SYNTAX_ERROR;
1936 address = strtoul(args[0], NULL, 0);
1937 value = strtoul(args[1], NULL, 0);
1939 count = strtoul(args[2], NULL, 0);
1945 target_buffer_set_u32(target, value_buf, value);
1949 target_buffer_set_u16(target, value_buf, value);
1953 value_buf[0] = value;
1956 return ERROR_COMMAND_SYNTAX_ERROR;
1958 for (i=0; i<count; i++)
1964 retval = target->type->write_memory(target, address + i*wordsize, 4, 1, value_buf);
1967 retval = target->type->write_memory(target, address + i*wordsize, 2, 1, value_buf);
1970 retval = target->type->write_memory(target, address + i*wordsize, 1, 1, value_buf);
1975 if (retval!=ERROR_OK)
1985 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1991 u32 max_address=0xffffffff;
1997 duration_t duration;
1998 char *duration_text;
2000 target_t *target = get_current_target(cmd_ctx);
2002 if ((argc < 1)||(argc > 5))
2004 return ERROR_COMMAND_SYNTAX_ERROR;
2007 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
2010 image.base_address_set = 1;
2011 image.base_address = strtoul(args[1], NULL, 0);
2015 image.base_address_set = 0;
2019 image.start_address_set = 0;
2023 min_address=strtoul(args[3], NULL, 0);
2027 max_address=strtoul(args[4], NULL, 0)+min_address;
2030 if (min_address>max_address)
2032 return ERROR_COMMAND_SYNTAX_ERROR;
2036 duration_start_measure(&duration);
2038 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2045 for (i = 0; i < image.num_sections; i++)
2047 buffer = malloc(image.sections[i].size);
2050 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2054 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2064 /* DANGER!!! beware of unsigned comparision here!!! */
2066 if ((image.sections[i].base_address+buf_cnt>=min_address)&&
2067 (image.sections[i].base_address<max_address))
2069 if (image.sections[i].base_address<min_address)
2071 /* clip addresses below */
2072 offset+=min_address-image.sections[i].base_address;
2076 if (image.sections[i].base_address+buf_cnt>max_address)
2078 length-=(image.sections[i].base_address+buf_cnt)-max_address;
2081 if ((retval = target_write_buffer(target, image.sections[i].base_address+offset, length, buffer+offset)) != ERROR_OK)
2086 image_size += length;
2087 command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
2093 duration_stop_measure(&duration, &duration_text);
2094 if (retval==ERROR_OK)
2096 command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
2098 free(duration_text);
2100 image_close(&image);
2106 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2113 int retval=ERROR_OK;
2115 duration_t duration;
2116 char *duration_text;
2118 target_t *target = get_current_target(cmd_ctx);
2122 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2126 address = strtoul(args[1], NULL, 0);
2127 size = strtoul(args[2], NULL, 0);
2129 if ((address & 3) || (size & 3))
2131 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
2135 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2140 duration_start_measure(&duration);
2145 u32 this_run_size = (size > 560) ? 560 : size;
2147 retval = target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
2148 if (retval != ERROR_OK)
2153 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2154 if (retval != ERROR_OK)
2159 size -= this_run_size;
2160 address += this_run_size;
2163 fileio_close(&fileio);
2165 duration_stop_measure(&duration, &duration_text);
2166 if (retval==ERROR_OK)
2168 command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
2170 free(duration_text);
2175 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2183 u32 mem_checksum = 0;
2187 duration_t duration;
2188 char *duration_text;
2190 target_t *target = get_current_target(cmd_ctx);
2194 return ERROR_COMMAND_SYNTAX_ERROR;
2199 LOG_ERROR("no target selected");
2203 duration_start_measure(&duration);
2207 image.base_address_set = 1;
2208 image.base_address = strtoul(args[1], NULL, 0);
2212 image.base_address_set = 0;
2213 image.base_address = 0x0;
2216 image.start_address_set = 0;
2218 if ((retval=image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2225 for (i = 0; i < image.num_sections; i++)
2227 buffer = malloc(image.sections[i].size);
2230 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2233 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2239 /* calculate checksum of image */
2240 image_calculate_checksum( buffer, buf_cnt, &checksum );
2242 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2243 if( retval != ERROR_OK )
2249 if( checksum != mem_checksum )
2251 /* failed crc checksum, fall back to a binary compare */
2254 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2256 data = (u8*)malloc(buf_cnt);
2258 /* Can we use 32bit word accesses? */
2260 int count = buf_cnt;
2261 if ((count % 4) == 0)
2266 retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
2267 if (retval == ERROR_OK)
2270 for (t = 0; t < buf_cnt; t++)
2272 if (data[t] != buffer[t])
2274 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]);
2287 image_size += buf_cnt;
2290 duration_stop_measure(&duration, &duration_text);
2291 if (retval==ERROR_OK)
2293 command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
2295 free(duration_text);
2297 image_close(&image);
2302 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2305 target_t *target = get_current_target(cmd_ctx);
2309 breakpoint_t *breakpoint = target->breakpoints;
2313 if (breakpoint->type == BKPT_SOFT)
2315 char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
2316 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
2321 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
2323 breakpoint = breakpoint->next;
2331 length = strtoul(args[1], NULL, 0);
2334 if (strcmp(args[2], "hw") == 0)
2337 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
2339 LOG_ERROR("Failure setting breakpoints");
2343 command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
2348 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2354 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2356 target_t *target = get_current_target(cmd_ctx);
2359 breakpoint_remove(target, strtoul(args[0], NULL, 0));
2364 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2366 target_t *target = get_current_target(cmd_ctx);
2371 watchpoint_t *watchpoint = target->watchpoints;
2375 command_print(cmd_ctx, "address: 0x%8.8x, len: 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);
2376 watchpoint = watchpoint->next;
2381 enum watchpoint_rw type = WPT_ACCESS;
2382 u32 data_value = 0x0;
2383 u32 data_mask = 0xffffffff;
2399 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2405 data_value = strtoul(args[3], NULL, 0);
2409 data_mask = strtoul(args[4], NULL, 0);
2412 if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
2413 strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
2415 LOG_ERROR("Failure setting breakpoints");
2420 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2426 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2428 target_t *target = get_current_target(cmd_ctx);
2431 watchpoint_remove(target, strtoul(args[0], NULL, 0));
2436 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
2439 target_t *target = get_current_target(cmd_ctx);
2445 return ERROR_COMMAND_SYNTAX_ERROR;
2447 va = strtoul(args[0], NULL, 0);
2449 retval = target->type->virt2phys(target, va, &pa);
2450 if (retval == ERROR_OK)
2452 command_print(cmd_ctx, "Physical address 0x%08x", pa);
2456 /* lower levels will have logged a detailed error which is
2457 * forwarded to telnet/GDB session.
2462 static void writeLong(FILE *f, int l)
2467 char c=(l>>(i*8))&0xff;
2468 fwrite(&c, 1, 1, f);
2472 static void writeString(FILE *f, char *s)
2474 fwrite(s, 1, strlen(s), f);
2479 // Dump a gmon.out histogram file.
2480 static void writeGmon(u32 *samples, int sampleNum, char *filename)
2483 FILE *f=fopen(filename, "w");
2486 fwrite("gmon", 1, 4, f);
2487 writeLong(f, 0x00000001); // Version
2488 writeLong(f, 0); // padding
2489 writeLong(f, 0); // padding
2490 writeLong(f, 0); // padding
2492 fwrite("", 1, 1, f); // GMON_TAG_TIME_HIST
2494 // figure out bucket size
2497 for (i=0; i<sampleNum; i++)
2509 int addressSpace=(max-min+1);
2511 static int const maxBuckets=256*1024; // maximum buckets.
2512 int length=addressSpace;
2513 if (length > maxBuckets)
2517 int *buckets=malloc(sizeof(int)*length);
2523 memset(buckets, 0, sizeof(int)*length);
2524 for (i=0; i<sampleNum;i++)
2526 u32 address=samples[i];
2527 long long a=address-min;
2528 long long b=length-1;
2529 long long c=addressSpace-1;
2530 int index=(a*b)/c; // danger!!!! int32 overflows
2534 // append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr))
2535 writeLong(f, min); // low_pc
2536 writeLong(f, max); // high_pc
2537 writeLong(f, length); // # of samples
2538 writeLong(f, 64000000); // 64MHz
2539 writeString(f, "seconds");
2540 for (i=0; i<(15-strlen("seconds")); i++)
2542 fwrite("", 1, 1, f); // padding
2544 writeString(f, "s");
2546 // append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size)
2548 char *data=malloc(2*length);
2551 for (i=0; i<length;i++)
2560 data[i*2+1]=(val>>8)&0xff;
2563 fwrite(data, 1, length*2, f);
2573 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2574 int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2576 target_t *target = get_current_target(cmd_ctx);
2577 struct timeval timeout, now;
2579 gettimeofday(&timeout, NULL);
2582 return ERROR_COMMAND_SYNTAX_ERROR;
2585 timeval_add_time(&timeout, strtoul(args[0], &end, 0), 0);
2591 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
2593 static const int maxSample=10000;
2594 u32 *samples=malloc(sizeof(u32)*maxSample);
2599 int retval=ERROR_OK;
2600 // hopefully it is safe to cache! We want to stop/restart as quickly as possible.
2601 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
2605 target_poll(target);
2606 if (target->state == TARGET_HALTED)
2608 u32 t=*((u32 *)reg->value);
2609 samples[numSamples++]=t;
2610 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2611 target_poll(target);
2612 alive_sleep(10); // sleep 10ms, i.e. <100 samples/second.
2613 } else if (target->state == TARGET_RUNNING)
2615 // We want to quickly sample the PC.
2616 target_halt(target);
2619 command_print(cmd_ctx, "Target not halted or running");
2623 if (retval!=ERROR_OK)
2628 gettimeofday(&now, NULL);
2629 if ((numSamples>=maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
2631 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2632 target_poll(target);
2633 if (target->state == TARGET_HALTED)
2635 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2637 target_poll(target);
2638 writeGmon(samples, numSamples, args[1]);
2639 command_print(cmd_ctx, "Wrote %s", args[1]);
2648 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 val)
2651 Jim_Obj *nameObjPtr, *valObjPtr;
2654 namebuf = alloc_printf("%s(%d)", varname, idx);
2658 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2659 valObjPtr = Jim_NewIntObj(interp, val);
2660 if (!nameObjPtr || !valObjPtr)
2666 Jim_IncrRefCount(nameObjPtr);
2667 Jim_IncrRefCount(valObjPtr);
2668 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
2669 Jim_DecrRefCount(interp, nameObjPtr);
2670 Jim_DecrRefCount(interp, valObjPtr);
2672 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
2676 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2678 command_context_t *context;
2681 context = Jim_GetAssocData(interp, "context");
2682 if (context == NULL)
2684 LOG_ERROR("mem2array: no command context");
2687 target = get_current_target(context);
2690 LOG_ERROR("mem2array: no current target");
2694 return target_mem2array(interp, target, argc,argv);
2697 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
2705 const char *varname;
2707 int i, n, e, retval;
2709 /* argv[1] = name of array to receive the data
2710 * argv[2] = desired width
2711 * argv[3] = memory address
2712 * argv[4] = count of times to read
2715 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2718 varname = Jim_GetString(argv[1], &len);
2719 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2721 e = Jim_GetLong(interp, argv[2], &l);
2727 e = Jim_GetLong(interp, argv[3], &l);
2732 e = Jim_GetLong(interp, argv[4], &l);
2748 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2749 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2753 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2754 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
2757 if ((addr + (len * width)) < addr) {
2758 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2759 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
2762 /* absurd transfer size? */
2764 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2765 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
2770 ((width == 2) && ((addr & 1) == 0)) ||
2771 ((width == 4) && ((addr & 3) == 0))) {
2775 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2776 sprintf(buf, "mem2array address: 0x%08x is not aligned for %d byte reads", addr, width);
2777 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2788 /* Slurp... in buffer size chunks */
2790 count = len; /* in objects.. */
2791 if (count > (sizeof(buffer)/width)) {
2792 count = (sizeof(buffer)/width);
2795 retval = target->type->read_memory( target, addr, width, count, buffer );
2796 if (retval != ERROR_OK) {
2798 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
2799 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2800 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
2804 v = 0; /* shut up gcc */
2805 for (i = 0 ;i < count ;i++, n++) {
2808 v = target_buffer_get_u32(target, &buffer[i*width]);
2811 v = target_buffer_get_u16(target, &buffer[i*width]);
2814 v = buffer[i] & 0x0ff;
2817 new_int_array_element(interp, varname, n, v);
2823 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2828 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 *val)
2831 Jim_Obj *nameObjPtr, *valObjPtr;
2835 namebuf = alloc_printf("%s(%d)", varname, idx);
2839 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2846 Jim_IncrRefCount(nameObjPtr);
2847 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
2848 Jim_DecrRefCount(interp, nameObjPtr);
2850 if (valObjPtr == NULL)
2853 result = Jim_GetLong(interp, valObjPtr, &l);
2854 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
2859 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2861 command_context_t *context;
2864 context = Jim_GetAssocData(interp, "context");
2865 if (context == NULL){
2866 LOG_ERROR("array2mem: no command context");
2869 target = get_current_target(context);
2870 if (target == NULL){
2871 LOG_ERROR("array2mem: no current target");
2875 return target_array2mem( interp,target, argc, argv );
2879 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
2887 const char *varname;
2889 int i, n, e, retval;
2891 /* argv[1] = name of array to get the data
2892 * argv[2] = desired width
2893 * argv[3] = memory address
2894 * argv[4] = count to write
2897 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2900 varname = Jim_GetString(argv[1], &len);
2901 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2903 e = Jim_GetLong(interp, argv[2], &l);
2909 e = Jim_GetLong(interp, argv[3], &l);
2914 e = Jim_GetLong(interp, argv[4], &l);
2930 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2931 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2935 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2936 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
2939 if ((addr + (len * width)) < addr) {
2940 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2941 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
2944 /* absurd transfer size? */
2946 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2947 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
2952 ((width == 2) && ((addr & 1) == 0)) ||
2953 ((width == 4) && ((addr & 3) == 0))) {
2957 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2958 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads", addr, width);
2959 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2971 /* Slurp... in buffer size chunks */
2973 count = len; /* in objects.. */
2974 if (count > (sizeof(buffer)/width)) {
2975 count = (sizeof(buffer)/width);
2978 v = 0; /* shut up gcc */
2979 for (i = 0 ;i < count ;i++, n++) {
2980 get_int_array_element(interp, varname, n, &v);
2983 target_buffer_set_u32(target, &buffer[i*width], v);
2986 target_buffer_set_u16(target, &buffer[i*width], v);
2989 buffer[i] = v & 0x0ff;
2995 retval = target->type->write_memory(target, addr, width, count, buffer);
2996 if (retval != ERROR_OK) {
2998 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
2999 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3000 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3006 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3012 target_all_handle_event( enum target_event e )
3017 LOG_DEBUG( "**all*targets: event: %d, %s",
3019 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3021 target = all_targets;
3023 target_handle_event( target, e );
3024 target = target->next;
3029 target_handle_event( target_t *target, enum target_event e )
3031 target_event_action_t *teap;
3034 teap = target->event_action;
3038 if( teap->event == e ){
3040 LOG_DEBUG( "target: (%d) %s (%s) event: %d (%s) action: %s\n",
3041 target->target_number,
3045 Jim_Nvp_value2name_simple( nvp_target_event, e )->name,
3046 Jim_GetString( teap->body, NULL ) );
3047 Jim_EvalObj( interp, teap->body );
3052 LOG_DEBUG( "event: %d %s - no action",
3054 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3058 enum target_cfg_param {
3061 TCFG_WORK_AREA_VIRT,
3062 TCFG_WORK_AREA_PHYS,
3063 TCFG_WORK_AREA_SIZE,
3064 TCFG_WORK_AREA_BACKUP,
3067 TCFG_CHAIN_POSITION,
3071 static Jim_Nvp nvp_config_opts[] = {
3072 { .name = "-type", .value = TCFG_TYPE },
3073 { .name = "-event", .value = TCFG_EVENT },
3074 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3075 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3076 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3077 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3078 { .name = "-endian" , .value = TCFG_ENDIAN },
3079 { .name = "-variant", .value = TCFG_VARIANT },
3080 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3082 { .name = NULL, .value = -1 }
3087 target_configure( Jim_GetOptInfo *goi,
3097 /* parse config or cget options ... */
3098 while( goi->argc > 0 ){
3099 Jim_SetEmptyResult( goi->interp );
3100 //Jim_GetOpt_Debug( goi );
3102 if( target->type->target_jim_configure ){
3103 /* target defines a configure function */
3104 /* target gets first dibs on parameters */
3105 e = (*(target->type->target_jim_configure))( target, goi );
3114 /* otherwise we 'continue' below */
3116 e = Jim_GetOpt_Nvp( goi, nvp_config_opts, &n );
3118 Jim_GetOpt_NvpUnknown( goi, nvp_config_opts, 0 );
3124 if( goi->isconfigure ){
3125 Jim_SetResult_sprintf( goi->interp, "not setable: %s", n->name );
3129 if( goi->argc != 0 ){
3130 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "NO PARAMS");
3134 Jim_SetResultString( goi->interp, target->type->name, -1 );
3138 if( goi->argc == 0 ){
3139 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3143 e = Jim_GetOpt_Nvp( goi, nvp_target_event, &n );
3145 Jim_GetOpt_NvpUnknown( goi, nvp_target_event, 1 );
3149 if( goi->isconfigure ){
3150 if( goi->argc == 0 ){
3151 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3155 if( goi->argc != 0 ){
3156 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3163 target_event_action_t *teap;
3165 teap = target->event_action;
3166 /* replace existing? */
3168 if( teap->event == n->value ){
3174 if( goi->isconfigure ){
3177 teap = calloc( 1, sizeof(*teap) );
3179 teap->event = n->value;
3180 Jim_GetOpt_Obj( goi, &o );
3182 Jim_DecrRefCount( interp, teap->body );
3184 teap->body = Jim_DuplicateObj( goi->interp, o );
3187 * Tcl/TK - "tk events" have a nice feature.
3188 * See the "BIND" command.
3189 * We should support that here.
3190 * You can specify %X and %Y in the event code.
3191 * The idea is: %T - target name.
3192 * The idea is: %N - target number
3193 * The idea is: %E - event name.
3195 Jim_IncrRefCount( teap->body );
3197 /* add to head of event list */
3198 teap->next = target->event_action;
3199 target->event_action = teap;
3200 Jim_SetEmptyResult(goi->interp);
3204 Jim_SetEmptyResult( goi->interp );
3206 Jim_SetResult( goi->interp, Jim_DuplicateObj( goi->interp, teap->body ) );
3213 case TCFG_WORK_AREA_VIRT:
3214 if( goi->isconfigure ){
3215 target_free_all_working_areas(target);
3216 e = Jim_GetOpt_Wide( goi, &w );
3220 target->working_area_virt = w;
3222 if( goi->argc != 0 ){
3226 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_virt ) );
3230 case TCFG_WORK_AREA_PHYS:
3231 if( goi->isconfigure ){
3232 target_free_all_working_areas(target);
3233 e = Jim_GetOpt_Wide( goi, &w );
3237 target->working_area_phys = w;
3239 if( goi->argc != 0 ){
3243 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_phys ) );
3247 case TCFG_WORK_AREA_SIZE:
3248 if( goi->isconfigure ){
3249 target_free_all_working_areas(target);
3250 e = Jim_GetOpt_Wide( goi, &w );
3254 target->working_area_size = w;
3256 if( goi->argc != 0 ){
3260 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
3264 case TCFG_WORK_AREA_BACKUP:
3265 if( goi->isconfigure ){
3266 target_free_all_working_areas(target);
3267 e = Jim_GetOpt_Wide( goi, &w );
3271 /* make this exactly 1 or 0 */
3272 target->backup_working_area = (!!w);
3274 if( goi->argc != 0 ){
3278 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
3279 /* loop for more e*/
3283 if( goi->isconfigure ){
3284 e = Jim_GetOpt_Nvp( goi, nvp_target_endian, &n );
3286 Jim_GetOpt_NvpUnknown( goi, nvp_target_endian, 1 );
3289 target->endianness = n->value;
3291 if( goi->argc != 0 ){
3295 n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3296 if( n->name == NULL ){
3297 target->endianness = TARGET_LITTLE_ENDIAN;
3298 n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3300 Jim_SetResultString( goi->interp, n->name, -1 );
3305 if( goi->isconfigure ){
3306 if( goi->argc < 1 ){
3307 Jim_SetResult_sprintf( goi->interp,
3312 if( target->variant ){
3313 free((void *)(target->variant));
3315 e = Jim_GetOpt_String( goi, &cp, NULL );
3316 target->variant = strdup(cp);
3318 if( goi->argc != 0 ){
3322 Jim_SetResultString( goi->interp, target->variant,-1 );
3325 case TCFG_CHAIN_POSITION:
3326 if( goi->isconfigure ){
3327 target_free_all_working_areas(target);
3328 e = Jim_GetOpt_Wide( goi, &w );
3332 /* make this exactly 1 or 0 */
3333 target->chain_position = w;
3335 if( goi->argc != 0 ){
3339 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->chain_position ) );
3340 /* loop for more e*/
3343 } /* while( goi->argc ) */
3344 /* done - we return */
3349 /** this is the 'tcl' handler for the target specific command */
3351 tcl_target_func( Jim_Interp *interp,
3353 Jim_Obj *const *argv )
3361 struct command_context_s *cmd_ctx;
3369 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3370 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3371 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3372 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3380 TS_CMD_INVOKE_EVENT,
3383 static const Jim_Nvp target_options[] = {
3384 { .name = "configure", .value = TS_CMD_CONFIGURE },
3385 { .name = "cget", .value = TS_CMD_CGET },
3386 { .name = "mww", .value = TS_CMD_MWW },
3387 { .name = "mwh", .value = TS_CMD_MWH },
3388 { .name = "mwb", .value = TS_CMD_MWB },
3389 { .name = "mdw", .value = TS_CMD_MDW },
3390 { .name = "mdh", .value = TS_CMD_MDH },
3391 { .name = "mdb", .value = TS_CMD_MDB },
3392 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3393 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3394 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3395 { .name = "curstate", .value = TS_CMD_CURSTATE },
3397 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3398 { .name = "arp_poll", .value = TS_CMD_POLL },
3399 { .name = "arp_reset", .value = TS_CMD_RESET },
3400 { .name = "arp_halt", .value = TS_CMD_HALT },
3401 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3402 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3404 { .name = NULL, .value = -1 },
3408 /* go past the "command" */
3409 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
3411 target = Jim_CmdPrivData( goi.interp );
3412 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3414 /* commands here are in an NVP table */
3415 e = Jim_GetOpt_Nvp( &goi, target_options, &n );
3417 Jim_GetOpt_NvpUnknown( &goi, target_options, 0 );
3420 // Assume blank result
3421 Jim_SetEmptyResult( goi.interp );
3424 case TS_CMD_CONFIGURE:
3426 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3429 goi.isconfigure = 1;
3430 return target_configure( &goi, target );
3432 // some things take params
3434 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "missing: ?-option?");
3437 goi.isconfigure = 0;
3438 return target_configure( &goi, target );
3446 * argv[3] = optional count.
3449 if( (goi.argc == 3) || (goi.argc == 4) ){
3453 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR DATA [COUNT]", n->name );
3457 e = Jim_GetOpt_Wide( &goi, &a );
3462 e = Jim_GetOpt_Wide( &goi, &b );
3467 e = Jim_GetOpt_Wide( &goi, &c );
3477 target_buffer_set_u32( target, target_buf, b );
3481 target_buffer_set_u16( target, target_buf, b );
3485 target_buffer_set_u8( target, target_buf, b );
3489 for( x = 0 ; x < c ; x++ ){
3490 e = target->type->write_memory( target, a, b, 1, target_buf );
3491 if( e != ERROR_OK ){
3492 Jim_SetResult_sprintf( interp, "Error writing @ 0x%08x: %d\n", (int)(a), e );
3505 /* argv[0] = command
3507 * argv[2] = optional count
3509 if( (goi.argc == 2) || (goi.argc == 3) ){
3510 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR [COUNT]", n->name );
3513 e = Jim_GetOpt_Wide( &goi, &a );
3518 e = Jim_GetOpt_Wide( &goi, &c );
3525 b = 1; /* shut up gcc */
3538 /* convert to "bytes" */
3540 /* count is now in 'BYTES' */
3546 e = target->type->read_memory( target, a, b, y / b, target_buf );
3547 if( e != ERROR_OK ){
3548 Jim_SetResult_sprintf( interp, "error reading target @ 0x%08lx", (int)(a) );
3552 Jim_fprintf( interp, interp->cookie_stdout, "0x%08x ", (int)(a) );
3555 for( x = 0 ; (x < 16) && (x < y) ; x += 4 ){
3556 z = target_buffer_get_u32( target, &(target_buf[ x * 4 ]) );
3557 Jim_fprintf( interp, interp->cookie_stdout, "%08x ", (int)(z) );
3559 for( ; (x < 16) ; x += 4 ){
3560 Jim_fprintf( interp, interp->cookie_stdout, " " );
3564 for( x = 0 ; (x < 16) && (x < y) ; x += 2 ){
3565 z = target_buffer_get_u16( target, &(target_buf[ x * 2 ]) );
3566 Jim_fprintf( interp, interp->cookie_stdout, "%04x ", (int)(z) );
3568 for( ; (x < 16) ; x += 2 ){
3569 Jim_fprintf( interp, interp->cookie_stdout, " " );
3574 for( x = 0 ; (x < 16) && (x < y) ; x += 1 ){
3575 z = target_buffer_get_u8( target, &(target_buf[ x * 4 ]) );
3576 Jim_fprintf( interp, interp->cookie_stdout, "%02x ", (int)(z) );
3578 for( ; (x < 16) ; x += 1 ){
3579 Jim_fprintf( interp, interp->cookie_stdout, " " );
3583 /* ascii-ify the bytes */
3584 for( x = 0 ; x < y ; x++ ){
3585 if( (target_buf[x] >= 0x20) &&
3586 (target_buf[x] <= 0x7e) ){
3590 target_buf[x] = '.';
3595 target_buf[x] = ' ';
3600 /* print - with a newline */
3601 Jim_fprintf( interp, interp->cookie_stdout, "%s\n", target_buf );
3607 case TS_CMD_MEM2ARRAY:
3608 return target_mem2array( goi.interp, target, goi.argc, goi.argv );
3610 case TS_CMD_ARRAY2MEM:
3611 return target_array2mem( goi.interp, target, goi.argc, goi.argv );
3613 case TS_CMD_EXAMINE:
3615 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3618 e = target->type->examine( target );
3619 if( e != ERROR_OK ){
3620 Jim_SetResult_sprintf( interp, "examine-fails: %d", e );
3626 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3629 if( !(target->type->examined) ){
3630 e = ERROR_TARGET_NOT_EXAMINED;
3632 e = target->type->poll( target );
3634 if( e != ERROR_OK ){
3635 Jim_SetResult_sprintf( interp, "poll-fails: %d", e );
3642 if( goi.argc != 2 ){
3643 Jim_WrongNumArgs( interp, 2, argv, "t|f|assert|deassert BOOL");
3646 e = Jim_GetOpt_Nvp( &goi, nvp_assert, &n );
3648 Jim_GetOpt_NvpUnknown( &goi, nvp_assert, 1 );
3651 // the halt or not param
3652 e = Jim_GetOpt_Wide( &goi, &a);
3656 // determine if we should halt or not.
3657 target->reset_halt = !!a;
3658 // When this happens - all workareas are invalid.
3659 target_free_all_working_areas_restore(target, 0);
3662 if( n->value == NVP_ASSERT ){
3663 target->type->assert_reset( target );
3665 target->type->deassert_reset( target );
3670 Jim_WrongNumArgs( goi.interp, 0, argv, "halt [no parameters]");
3673 target->type->halt( target );
3675 case TS_CMD_WAITSTATE:
3676 // params: <name> statename timeoutmsecs
3677 if( goi.argc != 2 ){
3678 Jim_SetResult_sprintf( goi.interp, "%s STATENAME TIMEOUTMSECS", n->name );
3681 e = Jim_GetOpt_Nvp( &goi, nvp_target_state, &n );
3683 Jim_GetOpt_NvpUnknown( &goi, nvp_target_state,1 );
3686 e = Jim_GetOpt_Wide( &goi, &a );
3690 e = target_wait_state( target, n->value, a );
3691 if( e != ERROR_OK ){
3692 Jim_SetResult_sprintf( goi.interp,
3693 "target: %s wait %s fails (%d) %s",
3696 e, target_strerror_safe(e) );
3701 case TS_CMD_EVENTLIST:
3702 /* List for human, Events defined for this target.
3703 * scripts/programs should use 'name cget -event NAME'
3706 target_event_action_t *teap;
3707 teap = target->event_action;
3708 command_print( cmd_ctx, "Event actions for target (%d) %s\n",
3709 target->target_number,
3711 command_print( cmd_ctx, "%-25s | Body", "Event");
3712 command_print( cmd_ctx, "------------------------- | ----------------------------------------");
3714 command_print( cmd_ctx,
3716 Jim_Nvp_value2name_simple( nvp_target_event, teap->event )->name,
3717 Jim_GetString( teap->body, NULL ) );
3720 command_print( cmd_ctx, "***END***");
3723 case TS_CMD_CURSTATE:
3724 if( goi.argc != 0 ){
3725 Jim_WrongNumArgs( goi.interp, 0, argv, "[no parameters]");
3728 Jim_SetResultString( goi.interp,
3729 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name,-1);
3731 case TS_CMD_INVOKE_EVENT:
3732 if( goi.argc != 1 ){
3733 Jim_SetResult_sprintf( goi.interp, "%s ?EVENTNAME?",n->name);
3736 e = Jim_GetOpt_Nvp( &goi, nvp_target_event, &n );
3738 Jim_GetOpt_NvpUnknown( &goi, nvp_target_event, 1 );
3741 target_handle_event( target, n->value );
3749 target_create( Jim_GetOptInfo *goi )
3759 struct command_context_s *cmd_ctx;
3761 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
3762 if( goi->argc < 3 ){
3763 Jim_WrongNumArgs( goi->interp, 1, goi->argv, "?name? ?type? ..options...");
3768 Jim_GetOpt_Obj( goi, &new_cmd );
3769 /* does this command exist? */
3770 cmd = Jim_GetCommand( goi->interp, new_cmd, JIM_ERRMSG );
3772 cp = Jim_GetString( new_cmd, NULL );
3773 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
3778 e = Jim_GetOpt_String( goi, &cp2, NULL );
3780 /* now does target type exist */
3781 for( x = 0 ; target_types[x] ; x++ ){
3782 if( 0 == strcmp( cp, target_types[x]->name ) ){
3787 if( target_types[x] == NULL ){
3788 Jim_SetResult_sprintf( goi->interp, "Unknown target type %s, try one of ", cp );
3789 for( x = 0 ; target_types[x] ; x++ ){
3790 if( target_types[x+1] ){
3791 Jim_AppendStrings( goi->interp,
3792 Jim_GetResult(goi->interp),
3793 target_types[x]->name,
3796 Jim_AppendStrings( goi->interp,
3797 Jim_GetResult(goi->interp),
3799 target_types[x]->name,NULL );
3807 target = calloc(1,sizeof(target_t));
3808 /* set target number */
3809 target->target_number = new_target_number();
3811 /* allocate memory for each unique target type */
3812 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
3814 memcpy( target->type, target_types[x], sizeof(target_type_t));
3816 /* will be set by "-endian" */
3817 target->endianness = TARGET_ENDIAN_UNKNOWN;
3819 target->working_area = 0x0;
3820 target->working_area_size = 0x0;
3821 target->working_areas = NULL;
3822 target->backup_working_area = 0;
3824 target->state = TARGET_UNKNOWN;
3825 target->debug_reason = DBG_REASON_UNDEFINED;
3826 target->reg_cache = NULL;
3827 target->breakpoints = NULL;
3828 target->watchpoints = NULL;
3829 target->next = NULL;
3830 target->arch_info = NULL;
3832 /* initialize trace information */
3833 target->trace_info = malloc(sizeof(trace_t));
3834 target->trace_info->num_trace_points = 0;
3835 target->trace_info->trace_points_size = 0;
3836 target->trace_info->trace_points = NULL;
3837 target->trace_info->trace_history_size = 0;
3838 target->trace_info->trace_history = NULL;
3839 target->trace_info->trace_history_pos = 0;
3840 target->trace_info->trace_history_overflowed = 0;
3842 target->dbgmsg = NULL;
3843 target->dbg_msg_enabled = 0;
3845 target->endianness = TARGET_ENDIAN_UNKNOWN;
3847 /* Do the rest as "configure" options */
3848 goi->isconfigure = 1;
3849 e = target_configure( goi, target);
3851 free( target->type );
3856 if( target->endianness == TARGET_ENDIAN_UNKNOWN ){
3857 /* default endian to little if not specified */
3858 target->endianness = TARGET_LITTLE_ENDIAN;
3861 /* create the target specific commands */
3862 if( target->type->register_commands ){
3863 (*(target->type->register_commands))( cmd_ctx );
3865 if( target->type->target_create ){
3866 (*(target->type->target_create))( target, goi->interp );
3869 /* append to end of list */
3872 tpp = &(all_targets);
3874 tpp = &( (*tpp)->next );
3879 cp = Jim_GetString( new_cmd, NULL );
3880 target->cmd_name = strdup(cp);
3882 /* now - create the new target name command */
3883 e = Jim_CreateCommand( goi->interp,
3886 tcl_target_func, /* C function */
3887 target, /* private data */
3888 NULL ); /* no del proc */
3890 (*(target->type->target_create))( target, goi->interp );
3895 jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
3899 struct command_context_s *cmd_ctx;
3904 /* TG = target generic */
3912 const char *target_cmds[] = {
3913 "create", "types", "names", "current", "number",
3918 LOG_DEBUG("Target command params:");
3919 LOG_DEBUG(Jim_Debug_ArgvString( interp, argc, argv) );
3921 cmd_ctx = Jim_GetAssocData( interp, "context" );
3923 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
3925 if( goi.argc == 0 ){
3926 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
3930 /* is this old syntax? */
3931 /* To determine: We have to peek at argv[0]*/
3932 cp = Jim_GetString( goi.argv[0], NULL );
3933 for( x = 0 ; target_types[x] ; x++ ){
3934 if( 0 == strcmp(cp,target_types[x]->name) ){
3938 if( target_types[x] ){
3939 /* YES IT IS OLD SYNTAX */
3940 Jim_Obj *new_argv[10];
3943 /* target_old_syntax
3945 * argv[0] typename (above)
3947 * argv[2] reset method, deprecated/ignored
3948 * argv[3] = old param
3949 * argv[4] = old param
3951 * We will combine all "old params" into a single param.
3952 * Then later, split them again.
3955 Jim_WrongNumArgs( interp, 1, argv, "[OLDSYNTAX] ?TYPE? ?ENDIAN? ?RESET? ?old-params?");
3959 new_argv[0] = argv[0];
3960 new_argv[1] = Jim_NewStringObj( interp, "create", -1 );
3963 sprintf( buf, "target%d", new_target_number() );
3964 new_argv[2] = Jim_NewStringObj( interp, buf , -1 );
3966 new_argv[3] = goi.argv[0]; /* typename */
3967 new_argv[4] = Jim_NewStringObj( interp, "-endian", -1 );
3968 new_argv[5] = goi.argv[1];
3969 new_argv[6] = Jim_NewStringObj( interp, "-chain-position", -1 );
3970 new_argv[7] = goi.argv[2];
3971 new_argv[8] = Jim_NewStringObj( interp, "-variant", -1 );
3972 new_argv[9] = goi.argv[3];
3979 * argv[3] = typename
3980 * argv[4] = **FIRST** "configure" option.
3982 * Here, we make them:
3986 * argv[6] = -position
3988 * argv[8] = -variant
3989 * argv[9] = "somestring"
3992 /* don't let these be released */
3993 for( x = 0 ; x < new_argc ; x++ ){
3994 Jim_IncrRefCount( new_argv[x]);
3997 LOG_DEBUG("Target OLD SYNTAX - converted to new syntax");
3999 r = jim_target( goi.interp, new_argc, new_argv );
4001 /* release? these items */
4002 for( x = 0 ; x < new_argc ; x++ ){
4003 Jim_DecrRefCount( interp, new_argv[x] );
4008 //Jim_GetOpt_Debug( &goi );
4009 r = Jim_GetOpt_Enum( &goi, target_cmds, &x );
4016 Jim_Panic(goi.interp,"Why am I here?");
4018 case TG_CMD_CURRENT:
4019 if( goi.argc != 0 ){
4020 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters");
4023 Jim_SetResultString( goi.interp, get_current_target( cmd_ctx )->cmd_name, -1 );
4026 if( goi.argc != 0 ){
4027 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
4030 Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
4031 for( x = 0 ; target_types[x] ; x++ ){
4032 Jim_ListAppendElement( goi.interp,
4033 Jim_GetResult(goi.interp),
4034 Jim_NewStringObj( goi.interp, target_types[x]->name, -1 ) );
4038 if( goi.argc != 0 ){
4039 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
4042 Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
4043 target = all_targets;
4045 Jim_ListAppendElement( goi.interp,
4046 Jim_GetResult(goi.interp),
4047 Jim_NewStringObj( goi.interp, target->cmd_name, -1 ) );
4048 target = target->next;
4053 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4056 return target_create( &goi );
4059 if( goi.argc != 1 ){
4060 Jim_SetResult_sprintf( goi.interp, "expected: target number ?NUMBER?");
4063 e = Jim_GetOpt_Wide( &goi, &w );
4069 t = get_target_by_num(w);
4071 Jim_SetResult_sprintf( goi.interp,"Target: number %d does not exist", (int)(w));
4074 Jim_SetResultString( goi.interp, t->cmd_name, -1 );
4078 if( goi.argc != 0 ){
4079 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "<no parameters>");
4082 Jim_SetResult( goi.interp,
4083 Jim_NewIntObj( goi.interp, max_target_number()));