- added myself to copyright on files i remember adding large contributions for over...
[openocd.git] / src / target / target.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
10 * *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
13 * *
14 * 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. *
18 * *
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. *
23 * *
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 ***************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "replacements.h"
34 #include "target.h"
35 #include "target_request.h"
36
37 #include "log.h"
38 #include "configuration.h"
39 #include "binarybuffer.h"
40 #include "jtag.h"
41
42 #include <string.h>
43 #include <stdlib.h>
44 #include <inttypes.h>
45
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <unistd.h>
49 #include <errno.h>
50
51 #include <sys/time.h>
52 #include <time.h>
53
54 #include <time_support.h>
55
56 #include <fileio.h>
57 #include <image.h>
58
59 static int USE_OLD_RESET = 0; // temp
60
61 int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv);
62
63
64 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
65
66 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
67
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);
91
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);
94
95
96
97 /* targets */
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;
109
110 target_type_t *target_types[] =
111 {
112 &arm7tdmi_target,
113 &arm9tdmi_target,
114 &arm920t_target,
115 &arm720t_target,
116 &arm966e_target,
117 &arm926ejs_target,
118 &feroceon_target,
119 &xscale_target,
120 &cortexm3_target,
121 &arm11_target,
122 &mips_m4k_target,
123 NULL,
124 };
125
126 target_t *all_targets = NULL;
127 target_event_callback_t *target_event_callbacks = NULL;
128 target_timer_callback_t *target_timer_callbacks = NULL;
129
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 }
138 };
139
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 }
153 };
154
155 const char *target_strerror_safe( int err )
156 {
157 const Jim_Nvp *n;
158
159 n = Jim_Nvp_value2name_simple( nvp_error_target, err );
160 if( n->name == NULL ){
161 return "unknown";
162 } else {
163 return n->name;
164 }
165 }
166
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" },
172
173
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" },
178
179 /* historical name */
180
181 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
182
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" },
193
194
195
196
197
198 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
199 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-end" },
200
201
202 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
203 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
204
205 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
206 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
207
208
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" },
211
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" },
214
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" },
218
219 { .name = NULL, .value = -1 }
220 };
221
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 },
229 };
230
231
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 },
241 };
242
243
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 },
250 };
251
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 },
258 };
259
260 static int
261 max_target_number( void )
262 {
263 target_t *t;
264 int x;
265
266 x = -1;
267 t = all_targets;
268 while( t ){
269 if( x < t->target_number ){
270 x = (t->target_number)+1;
271 }
272 t = t->next;
273 }
274 return x;
275 }
276
277 /* determine the number of the new target */
278 static int
279 new_target_number( void )
280 {
281 target_t *t;
282 int x;
283
284 /* number is 0 based */
285 x = -1;
286 t = all_targets;
287 while(t){
288 if( x < t->target_number ){
289 x = t->target_number;
290 }
291 t = t->next;
292 }
293 return x+1;
294 }
295
296 static int target_continous_poll = 1;
297
298 /* read a u32 from a buffer in target memory endianness */
299 u32 target_buffer_get_u32(target_t *target, u8 *buffer)
300 {
301 if (target->endianness == TARGET_LITTLE_ENDIAN)
302 return le_to_h_u32(buffer);
303 else
304 return be_to_h_u32(buffer);
305 }
306
307 /* read a u16 from a buffer in target memory endianness */
308 u16 target_buffer_get_u16(target_t *target, u8 *buffer)
309 {
310 if (target->endianness == TARGET_LITTLE_ENDIAN)
311 return le_to_h_u16(buffer);
312 else
313 return be_to_h_u16(buffer);
314 }
315
316 /* read a u8 from a buffer in target memory endianness */
317 u8 target_buffer_get_u8(target_t *target, u8 *buffer)
318 {
319 return *buffer & 0x0ff;
320 }
321
322 /* write a u32 to a buffer in target memory endianness */
323 void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
324 {
325 if (target->endianness == TARGET_LITTLE_ENDIAN)
326 h_u32_to_le(buffer, value);
327 else
328 h_u32_to_be(buffer, value);
329 }
330
331 /* write a u16 to a buffer in target memory endianness */
332 void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
333 {
334 if (target->endianness == TARGET_LITTLE_ENDIAN)
335 h_u16_to_le(buffer, value);
336 else
337 h_u16_to_be(buffer, value);
338 }
339
340 /* write a u8 to a buffer in target memory endianness */
341 void target_buffer_set_u8(target_t *target, u8 *buffer, u8 value)
342 {
343 *buffer = value;
344 }
345
346 /* returns a pointer to the n-th configured target */
347 target_t* get_target_by_num(int num)
348 {
349 target_t *target = all_targets;
350
351 while (target){
352 if( target->target_number == num ){
353 return target;
354 }
355 target = target->next;
356 }
357
358 return NULL;
359 }
360
361 int get_num_by_target(target_t *query_target)
362 {
363 return query_target->target_number;
364 }
365
366 target_t* get_current_target(command_context_t *cmd_ctx)
367 {
368 target_t *target = get_target_by_num(cmd_ctx->current_target);
369
370 if (target == NULL)
371 {
372 LOG_ERROR("BUG: current_target out of bounds");
373 exit(-1);
374 }
375
376 return target;
377 }
378
379
380 int target_poll(struct target_s *target)
381 {
382 /* We can't poll until after examine */
383 if (!target->type->examined)
384 {
385 /* Fail silently lest we pollute the log */
386 return ERROR_FAIL;
387 }
388 return target->type->poll(target);
389 }
390
391 int target_halt(struct target_s *target)
392 {
393 /* We can't poll until after examine */
394 if (!target->type->examined)
395 {
396 LOG_ERROR("Target not examined yet");
397 return ERROR_FAIL;
398 }
399 return target->type->halt(target);
400 }
401
402 int target_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
403 {
404 int retval;
405
406 /* We can't poll until after examine */
407 if (!target->type->examined)
408 {
409 LOG_ERROR("Target not examined yet");
410 return ERROR_FAIL;
411 }
412
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?)
415 * the application.
416 */
417 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
418 return retval;
419
420 return retval;
421 }
422
423
424 static int NEW_target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
425 {
426 char buf[100];
427 Jim_Nvp *n;
428 n = Jim_Nvp_value2name_simple( nvp_reset_modes, reset_mode );
429 if( n->name == NULL ){
430 LOG_ERROR("invalid reset mode");
431 return ERROR_FAIL;
432 }
433
434 sprintf( buf, "ocd_process_reset %s", n->name );
435 Jim_Eval( interp, buf );
436
437 /* We want any events to be processed before the prompt */
438 target_call_timer_callbacks_now();
439
440 return ERROR_OK;
441 }
442
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)
445 {
446 int retval = ERROR_OK;
447 target_t *target;
448
449 target = all_targets;
450
451 target_all_handle_event( TARGET_EVENT_OLD_pre_reset );
452
453 if ((retval = jtag_init_reset(cmd_ctx)) != ERROR_OK)
454 return retval;
455
456 keep_alive(); /* we might be running on a very slow JTAG clk */
457
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
460 * memory.
461 *
462 * It will also set up ICE registers in the target.
463 *
464 * However, if we assert TRST later, we need to set up the registers again.
465 *
466 * For the "reset halt/init" case we must only set up the registers here.
467 */
468 if ((retval = target_examine()) != ERROR_OK)
469 return retval;
470
471 keep_alive(); /* we might be running on a very slow JTAG clk */
472
473 target = all_targets;
474 while (target)
475 {
476 /* we have no idea what state the target is in, so we
477 * have to drop working areas
478 */
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)
482 return retval;
483 target = target->next;
484 }
485
486 target = all_targets;
487 while (target)
488 {
489 if ((retval = target->type->deassert_reset(target))!=ERROR_OK)
490 return retval;
491 target = target->next;
492 }
493
494 target = all_targets;
495 while (target)
496 {
497 /* We can fail to bring the target into the halted state, try after reset has been deasserted */
498 if (target->reset_halt)
499 {
500 /* wait up to 1 second for halt. */
501 target_wait_state(target, TARGET_HALTED, 1000);
502 if (target->state != TARGET_HALTED)
503 {
504 LOG_WARNING("Failed to reset target into halted mode - issuing halt");
505 if ((retval = target->type->halt(target))!=ERROR_OK)
506 return retval;
507 }
508 }
509
510 target = target->next;
511 }
512
513
514 LOG_DEBUG("Waiting for halted stated as appropriate");
515
516 if ((reset_mode == RESET_HALT) || (reset_mode == RESET_INIT))
517 {
518 target = all_targets;
519 while (target)
520 {
521 /* Wait for reset to complete, maximum 5 seconds. */
522 if (((retval=target_wait_state(target, TARGET_HALTED, 5000)))==ERROR_OK)
523 {
524 if (reset_mode == RESET_INIT){
525 target_handle_event( target, TARGET_EVENT_OLD_post_reset );
526 }
527
528 }
529 target = target->next;
530 }
531 }
532
533 /* We want any events to be processed before the prompt */
534 target_call_timer_callbacks_now();
535
536 return retval;
537 }
538
539 int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
540 {
541 if( USE_OLD_RESET ){
542 return OLD_target_process_reset( cmd_ctx, reset_mode );
543 } else {
544 return NEW_target_process_reset( cmd_ctx, reset_mode );
545 }
546 }
547
548
549 static int default_virt2phys(struct target_s *target, u32 virtual, u32 *physical)
550 {
551 *physical = virtual;
552 return ERROR_OK;
553 }
554
555 static int default_mmu(struct target_s *target, int *enabled)
556 {
557 *enabled = 0;
558 return ERROR_OK;
559 }
560
561 static int default_examine(struct target_s *target)
562 {
563 target->type->examined = 1;
564 return ERROR_OK;
565 }
566
567
568 /* Targets that correctly implement init+examine, i.e.
569 * no communication with target during init:
570 *
571 * XScale
572 */
573 int target_examine(void)
574 {
575 int retval = ERROR_OK;
576 target_t *target = all_targets;
577 while (target)
578 {
579 if ((retval = target->type->examine(target))!=ERROR_OK)
580 return retval;
581 target = target->next;
582 }
583 return retval;
584 }
585
586 static int target_write_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
587 {
588 if (!target->type->examined)
589 {
590 LOG_ERROR("Target not examined yet");
591 return ERROR_FAIL;
592 }
593 return target->type->write_memory_imp(target, address, size, count, buffer);
594 }
595
596 static int target_read_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
597 {
598 if (!target->type->examined)
599 {
600 LOG_ERROR("Target not examined yet");
601 return ERROR_FAIL;
602 }
603 return target->type->read_memory_imp(target, address, size, count, buffer);
604 }
605
606 static int target_soft_reset_halt_imp(struct target_s *target)
607 {
608 if (!target->type->examined)
609 {
610 LOG_ERROR("Target not examined yet");
611 return ERROR_FAIL;
612 }
613 return target->type->soft_reset_halt_imp(target);
614 }
615
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)
617 {
618 if (!target->type->examined)
619 {
620 LOG_ERROR("Target not examined yet");
621 return ERROR_FAIL;
622 }
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);
624 }
625
626 int target_init(struct command_context_s *cmd_ctx)
627 {
628 target_t *target = all_targets;
629
630 while (target)
631 {
632 target->type->examined = 0;
633 if (target->type->examine == NULL)
634 {
635 target->type->examine = default_examine;
636 }
637
638 if (target->type->init_target(cmd_ctx, target) != ERROR_OK)
639 {
640 LOG_ERROR("target '%s' init failed", target->type->name);
641 exit(-1);
642 }
643
644 /* Set up default functions if none are provided by target */
645 if (target->type->virt2phys == NULL)
646 {
647 target->type->virt2phys = default_virt2phys;
648 }
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
652 */
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;
661
662
663 if (target->type->mmu == NULL)
664 {
665 target->type->mmu = default_mmu;
666 }
667 target = target->next;
668 }
669
670 if (all_targets)
671 {
672 target_register_user_commands(cmd_ctx);
673 target_register_timer_callback(handle_target, 100, 1, NULL);
674 }
675
676 return ERROR_OK;
677 }
678
679 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
680 {
681 target_event_callback_t **callbacks_p = &target_event_callbacks;
682
683 if (callback == NULL)
684 {
685 return ERROR_INVALID_ARGUMENTS;
686 }
687
688 if (*callbacks_p)
689 {
690 while ((*callbacks_p)->next)
691 callbacks_p = &((*callbacks_p)->next);
692 callbacks_p = &((*callbacks_p)->next);
693 }
694
695 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
696 (*callbacks_p)->callback = callback;
697 (*callbacks_p)->priv = priv;
698 (*callbacks_p)->next = NULL;
699
700 return ERROR_OK;
701 }
702
703 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
704 {
705 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
706 struct timeval now;
707
708 if (callback == NULL)
709 {
710 return ERROR_INVALID_ARGUMENTS;
711 }
712
713 if (*callbacks_p)
714 {
715 while ((*callbacks_p)->next)
716 callbacks_p = &((*callbacks_p)->next);
717 callbacks_p = &((*callbacks_p)->next);
718 }
719
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;
724
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)
730 {
731 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
732 (*callbacks_p)->when.tv_sec += 1;
733 }
734
735 (*callbacks_p)->priv = priv;
736 (*callbacks_p)->next = NULL;
737
738 return ERROR_OK;
739 }
740
741 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
742 {
743 target_event_callback_t **p = &target_event_callbacks;
744 target_event_callback_t *c = target_event_callbacks;
745
746 if (callback == NULL)
747 {
748 return ERROR_INVALID_ARGUMENTS;
749 }
750
751 while (c)
752 {
753 target_event_callback_t *next = c->next;
754 if ((c->callback == callback) && (c->priv == priv))
755 {
756 *p = next;
757 free(c);
758 return ERROR_OK;
759 }
760 else
761 p = &(c->next);
762 c = next;
763 }
764
765 return ERROR_OK;
766 }
767
768 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
769 {
770 target_timer_callback_t **p = &target_timer_callbacks;
771 target_timer_callback_t *c = target_timer_callbacks;
772
773 if (callback == NULL)
774 {
775 return ERROR_INVALID_ARGUMENTS;
776 }
777
778 while (c)
779 {
780 target_timer_callback_t *next = c->next;
781 if ((c->callback == callback) && (c->priv == priv))
782 {
783 *p = next;
784 free(c);
785 return ERROR_OK;
786 }
787 else
788 p = &(c->next);
789 c = next;
790 }
791
792 return ERROR_OK;
793 }
794
795 int target_call_event_callbacks(target_t *target, enum target_event event)
796 {
797 target_event_callback_t *callback = target_event_callbacks;
798 target_event_callback_t *next_callback;
799
800 LOG_DEBUG("target event %i (%s)",
801 event,
802 Jim_Nvp_value2name_simple( nvp_target_event, event )->name );
803
804 target_handle_event( target, event );
805
806 while (callback)
807 {
808 next_callback = callback->next;
809 callback->callback(target, event, callback->priv);
810 callback = next_callback;
811 }
812
813 return ERROR_OK;
814 }
815
816 static int target_call_timer_callbacks_check_time(int checktime)
817 {
818 target_timer_callback_t *callback = target_timer_callbacks;
819 target_timer_callback_t *next_callback;
820 struct timeval now;
821
822 keep_alive();
823
824 gettimeofday(&now, NULL);
825
826 while (callback)
827 {
828 next_callback = callback->next;
829
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)))
833 {
834 if(callback->callback != NULL)
835 {
836 callback->callback(callback->priv);
837 if (callback->periodic)
838 {
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)
844 {
845 callback->when.tv_usec = callback->when.tv_usec - 1000000;
846 callback->when.tv_sec += 1;
847 }
848 }
849 else
850 target_unregister_timer_callback(callback->callback, callback->priv);
851 }
852 }
853
854 callback = next_callback;
855 }
856
857 return ERROR_OK;
858 }
859
860 int target_call_timer_callbacks(void)
861 {
862 return target_call_timer_callbacks_check_time(1);
863 }
864
865 /* invoke periodic callbacks immediately */
866 int target_call_timer_callbacks_now(void)
867 {
868 return target_call_timer_callbacks_check_time(0);
869 }
870
871 int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
872 {
873 working_area_t *c = target->working_areas;
874 working_area_t *new_wa = NULL;
875
876 /* Reevaluate working area address based on MMU state*/
877 if (target->working_areas == NULL)
878 {
879 int retval;
880 int enabled;
881 retval = target->type->mmu(target, &enabled);
882 if (retval != ERROR_OK)
883 {
884 return retval;
885 }
886 if (enabled)
887 {
888 target->working_area = target->working_area_virt;
889 }
890 else
891 {
892 target->working_area = target->working_area_phys;
893 }
894 }
895
896 /* only allocate multiples of 4 byte */
897 if (size % 4)
898 {
899 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
900 size = CEIL(size, 4);
901 }
902
903 /* see if there's already a matching working area */
904 while (c)
905 {
906 if ((c->free) && (c->size == size))
907 {
908 new_wa = c;
909 break;
910 }
911 c = c->next;
912 }
913
914 /* if not, allocate a new one */
915 if (!new_wa)
916 {
917 working_area_t **p = &target->working_areas;
918 u32 first_free = target->working_area;
919 u32 free_size = target->working_area_size;
920
921 LOG_DEBUG("allocating new working area");
922
923 c = target->working_areas;
924 while (c)
925 {
926 first_free += c->size;
927 free_size -= c->size;
928 p = &c->next;
929 c = c->next;
930 }
931
932 if (free_size < size)
933 {
934 LOG_WARNING("not enough working area available(requested %d, free %d)", size, free_size);
935 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
936 }
937
938 new_wa = malloc(sizeof(working_area_t));
939 new_wa->next = NULL;
940 new_wa->size = size;
941 new_wa->address = first_free;
942
943 if (target->backup_working_area)
944 {
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);
947 }
948 else
949 {
950 new_wa->backup = NULL;
951 }
952
953 /* put new entry in list */
954 *p = new_wa;
955 }
956
957 /* mark as used, and return the new (reused) area */
958 new_wa->free = 0;
959 *area = new_wa;
960
961 /* user pointer */
962 new_wa->user = area;
963
964 return ERROR_OK;
965 }
966
967 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
968 {
969 if (area->free)
970 return ERROR_OK;
971
972 if (restore&&target->backup_working_area)
973 target->type->write_memory(target, area->address, 4, area->size / 4, area->backup);
974
975 area->free = 1;
976
977 /* mark user pointer invalid */
978 *area->user = NULL;
979 area->user = NULL;
980
981 return ERROR_OK;
982 }
983
984 int target_free_working_area(struct target_s *target, working_area_t *area)
985 {
986 return target_free_working_area_restore(target, area, 1);
987 }
988
989 int target_free_all_working_areas_restore(struct target_s *target, int restore)
990 {
991 working_area_t *c = target->working_areas;
992
993 while (c)
994 {
995 working_area_t *next = c->next;
996 target_free_working_area_restore(target, c, restore);
997
998 if (c->backup)
999 free(c->backup);
1000
1001 free(c);
1002
1003 c = next;
1004 }
1005
1006 target->working_areas = NULL;
1007
1008 return ERROR_OK;
1009 }
1010
1011 int target_free_all_working_areas(struct target_s *target)
1012 {
1013 return target_free_all_working_areas_restore(target, 1);
1014 }
1015
1016 int target_register_commands(struct command_context_s *cmd_ctx)
1017 {
1018
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>");
1023
1024 register_jim(cmd_ctx, "target", jim_target, "configure target" );
1025
1026
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");
1030 return ERROR_OK;
1031 }
1032
1033 int target_arch_state(struct target_s *target)
1034 {
1035 int retval;
1036 if (target==NULL)
1037 {
1038 LOG_USER("No target has been configured");
1039 return ERROR_OK;
1040 }
1041
1042 LOG_USER("target state: %s",
1043 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name);
1044
1045 if (target->state!=TARGET_HALTED)
1046 return ERROR_OK;
1047
1048 retval=target->type->arch_state(target);
1049 return retval;
1050 }
1051
1052 /* Single aligned words are guaranteed to use 16 or 32 bit access
1053 * mode respectively, otherwise data is handled as quickly as
1054 * possible
1055 */
1056 int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
1057 {
1058 int retval;
1059 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
1060
1061 if (!target->type->examined)
1062 {
1063 LOG_ERROR("Target not examined yet");
1064 return ERROR_FAIL;
1065 }
1066
1067 if ((address + size - 1) < address)
1068 {
1069 /* GDB can request this when e.g. PC is 0xfffffffc*/
1070 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
1071 return ERROR_FAIL;
1072 }
1073
1074 if (((address % 2) == 0) && (size == 2))
1075 {
1076 return target->type->write_memory(target, address, 2, 1, buffer);
1077 }
1078
1079 /* handle unaligned head bytes */
1080 if (address % 4)
1081 {
1082 int unaligned = 4 - (address % 4);
1083
1084 if (unaligned > size)
1085 unaligned = size;
1086
1087 if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1088 return retval;
1089
1090 buffer += unaligned;
1091 address += unaligned;
1092 size -= unaligned;
1093 }
1094
1095 /* handle aligned words */
1096 if (size >= 4)
1097 {
1098 int aligned = size - (size % 4);
1099
1100 /* use bulk writes above a certain limit. This may have to be changed */
1101 if (aligned > 128)
1102 {
1103 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1104 return retval;
1105 }
1106 else
1107 {
1108 if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1109 return retval;
1110 }
1111
1112 buffer += aligned;
1113 address += aligned;
1114 size -= aligned;
1115 }
1116
1117 /* handle tail writes of less than 4 bytes */
1118 if (size > 0)
1119 {
1120 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1121 return retval;
1122 }
1123
1124 return ERROR_OK;
1125 }
1126
1127
1128 /* Single aligned words are guaranteed to use 16 or 32 bit access
1129 * mode respectively, otherwise data is handled as quickly as
1130 * possible
1131 */
1132 int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
1133 {
1134 int retval;
1135 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
1136
1137 if (!target->type->examined)
1138 {
1139 LOG_ERROR("Target not examined yet");
1140 return ERROR_FAIL;
1141 }
1142
1143 if ((address + size - 1) < address)
1144 {
1145 /* GDB can request this when e.g. PC is 0xfffffffc*/
1146 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
1147 return ERROR_FAIL;
1148 }
1149
1150 if (((address % 2) == 0) && (size == 2))
1151 {
1152 return target->type->read_memory(target, address, 2, 1, buffer);
1153 }
1154
1155 /* handle unaligned head bytes */
1156 if (address % 4)
1157 {
1158 int unaligned = 4 - (address % 4);
1159
1160 if (unaligned > size)
1161 unaligned = size;
1162
1163 if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1164 return retval;
1165
1166 buffer += unaligned;
1167 address += unaligned;
1168 size -= unaligned;
1169 }
1170
1171 /* handle aligned words */
1172 if (size >= 4)
1173 {
1174 int aligned = size - (size % 4);
1175
1176 if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1177 return retval;
1178
1179 buffer += aligned;
1180 address += aligned;
1181 size -= aligned;
1182 }
1183
1184 /* handle tail writes of less than 4 bytes */
1185 if (size > 0)
1186 {
1187 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1188 return retval;
1189 }
1190
1191 return ERROR_OK;
1192 }
1193
1194 int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* crc)
1195 {
1196 u8 *buffer;
1197 int retval;
1198 int i;
1199 u32 checksum = 0;
1200 if (!target->type->examined)
1201 {
1202 LOG_ERROR("Target not examined yet");
1203 return ERROR_FAIL;
1204 }
1205
1206 if ((retval = target->type->checksum_memory(target, address,
1207 size, &checksum)) == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1208 {
1209 buffer = malloc(size);
1210 if (buffer == NULL)
1211 {
1212 LOG_ERROR("error allocating buffer for section (%d bytes)", size);
1213 return ERROR_INVALID_ARGUMENTS;
1214 }
1215 retval = target_read_buffer(target, address, size, buffer);
1216 if (retval != ERROR_OK)
1217 {
1218 free(buffer);
1219 return retval;
1220 }
1221
1222 /* convert to target endianess */
1223 for (i = 0; i < (size/sizeof(u32)); i++)
1224 {
1225 u32 target_data;
1226 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(u32)]);
1227 target_buffer_set_u32(target, &buffer[i*sizeof(u32)], target_data);
1228 }
1229
1230 retval = image_calculate_checksum( buffer, size, &checksum );
1231 free(buffer);
1232 }
1233
1234 *crc = checksum;
1235
1236 return retval;
1237 }
1238
1239 int target_blank_check_memory(struct target_s *target, u32 address, u32 size, u32* blank)
1240 {
1241 int retval;
1242 if (!target->type->examined)
1243 {
1244 LOG_ERROR("Target not examined yet");
1245 return ERROR_FAIL;
1246 }
1247
1248 if (target->type->blank_check_memory == 0)
1249 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1250
1251 retval = target->type->blank_check_memory(target, address, size, blank);
1252
1253 return retval;
1254 }
1255
1256 int target_read_u32(struct target_s *target, u32 address, u32 *value)
1257 {
1258 u8 value_buf[4];
1259 if (!target->type->examined)
1260 {
1261 LOG_ERROR("Target not examined yet");
1262 return ERROR_FAIL;
1263 }
1264
1265 int retval = target->type->read_memory(target, address, 4, 1, value_buf);
1266
1267 if (retval == ERROR_OK)
1268 {
1269 *value = target_buffer_get_u32(target, value_buf);
1270 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
1271 }
1272 else
1273 {
1274 *value = 0x0;
1275 LOG_DEBUG("address: 0x%8.8x failed", address);
1276 }
1277
1278 return retval;
1279 }
1280
1281 int target_read_u16(struct target_s *target, u32 address, u16 *value)
1282 {
1283 u8 value_buf[2];
1284 if (!target->type->examined)
1285 {
1286 LOG_ERROR("Target not examined yet");
1287 return ERROR_FAIL;
1288 }
1289
1290 int retval = target->type->read_memory(target, address, 2, 1, value_buf);
1291
1292 if (retval == ERROR_OK)
1293 {
1294 *value = target_buffer_get_u16(target, value_buf);
1295 LOG_DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
1296 }
1297 else
1298 {
1299 *value = 0x0;
1300 LOG_DEBUG("address: 0x%8.8x failed", address);
1301 }
1302
1303 return retval;
1304 }
1305
1306 int target_read_u8(struct target_s *target, u32 address, u8 *value)
1307 {
1308 int retval = target->type->read_memory(target, address, 1, 1, value);
1309 if (!target->type->examined)
1310 {
1311 LOG_ERROR("Target not examined yet");
1312 return ERROR_FAIL;
1313 }
1314
1315 if (retval == ERROR_OK)
1316 {
1317 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
1318 }
1319 else
1320 {
1321 *value = 0x0;
1322 LOG_DEBUG("address: 0x%8.8x failed", address);
1323 }
1324
1325 return retval;
1326 }
1327
1328 int target_write_u32(struct target_s *target, u32 address, u32 value)
1329 {
1330 int retval;
1331 u8 value_buf[4];
1332 if (!target->type->examined)
1333 {
1334 LOG_ERROR("Target not examined yet");
1335 return ERROR_FAIL;
1336 }
1337
1338 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1339
1340 target_buffer_set_u32(target, value_buf, value);
1341 if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1342 {
1343 LOG_DEBUG("failed: %i", retval);
1344 }
1345
1346 return retval;
1347 }
1348
1349 int target_write_u16(struct target_s *target, u32 address, u16 value)
1350 {
1351 int retval;
1352 u8 value_buf[2];
1353 if (!target->type->examined)
1354 {
1355 LOG_ERROR("Target not examined yet");
1356 return ERROR_FAIL;
1357 }
1358
1359 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1360
1361 target_buffer_set_u16(target, value_buf, value);
1362 if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1363 {
1364 LOG_DEBUG("failed: %i", retval);
1365 }
1366
1367 return retval;
1368 }
1369
1370 int target_write_u8(struct target_s *target, u32 address, u8 value)
1371 {
1372 int retval;
1373 if (!target->type->examined)
1374 {
1375 LOG_ERROR("Target not examined yet");
1376 return ERROR_FAIL;
1377 }
1378
1379 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
1380
1381 if ((retval = target->type->read_memory(target, address, 1, 1, &value)) != ERROR_OK)
1382 {
1383 LOG_DEBUG("failed: %i", retval);
1384 }
1385
1386 return retval;
1387 }
1388
1389 int target_register_user_commands(struct command_context_s *cmd_ctx)
1390 {
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");
1400
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]");
1404
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]");
1408
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>");
1413
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]");
1417
1418 target_request_register_commands(cmd_ctx);
1419 trace_register_commands(cmd_ctx);
1420
1421 return ERROR_OK;
1422 }
1423
1424 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1425 {
1426 char *cp;
1427 target_t *target = all_targets;
1428
1429 if (argc == 1)
1430 {
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 ) ){
1435 /* MATCH */
1436 goto Match;
1437 }
1438 }
1439 }
1440 /* no match, try as number */
1441
1442 int num = strtoul(args[0], &cp, 0 );
1443 if( *cp != 0 ){
1444 /* then it was not a number */
1445 command_print( cmd_ctx, "Target: %s unknown, try one of:\n", args[0] );
1446 goto DumpTargets;
1447 }
1448
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] );
1452 goto DumpTargets;
1453 }
1454 Match:
1455 cmd_ctx->current_target = target->target_number;
1456 return ERROR_OK;
1457 }
1458 DumpTargets:
1459
1460 command_print(cmd_ctx, " CmdName Type Endian ChainPos State ");
1461 command_print(cmd_ctx, "-- ---------- ---------- ---------- -------- ----------");
1462 while (target)
1463 {
1464 /* XX: abcdefghij abcdefghij abcdefghij abcdefghij */
1465 command_print(cmd_ctx, "%2d: %-10s %-10s %-10s %8d %s",
1466 target->target_number,
1467 target->cmd_name,
1468 target->type->name,
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;
1473 }
1474
1475 return ERROR_OK;
1476 }
1477
1478
1479
1480 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1481 {
1482 target_t *target = NULL;
1483
1484 if ((argc < 4) || (argc > 5))
1485 {
1486 return ERROR_COMMAND_SYNTAX_ERROR;
1487 }
1488
1489 target = get_target_by_num(strtoul(args[0], NULL, 0));
1490 if (!target)
1491 {
1492 return ERROR_COMMAND_SYNTAX_ERROR;
1493 }
1494 target_free_all_working_areas(target);
1495
1496 target->working_area_phys = target->working_area_virt = strtoul(args[1], NULL, 0);
1497 if (argc == 5)
1498 {
1499 target->working_area_virt = strtoul(args[4], NULL, 0);
1500 }
1501 target->working_area_size = strtoul(args[2], NULL, 0);
1502
1503 if (strcmp(args[3], "backup") == 0)
1504 {
1505 target->backup_working_area = 1;
1506 }
1507 else if (strcmp(args[3], "nobackup") == 0)
1508 {
1509 target->backup_working_area = 0;
1510 }
1511 else
1512 {
1513 LOG_ERROR("unrecognized <backup|nobackup> argument (%s)", args[3]);
1514 return ERROR_COMMAND_SYNTAX_ERROR;
1515 }
1516
1517 return ERROR_OK;
1518 }
1519
1520
1521 /* process target state changes */
1522 int handle_target(void *priv)
1523 {
1524 target_t *target = all_targets;
1525
1526 while (target)
1527 {
1528 if (target_continous_poll)
1529 {
1530 /* polling may fail silently until the target has been examined */
1531 target_poll(target);
1532 }
1533
1534 target = target->next;
1535 }
1536
1537 return ERROR_OK;
1538 }
1539
1540 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1541 {
1542 target_t *target;
1543 reg_t *reg = NULL;
1544 int count = 0;
1545 char *value;
1546
1547 LOG_DEBUG("-");
1548
1549 target = get_current_target(cmd_ctx);
1550
1551 /* list all available registers for the current target */
1552 if (argc == 0)
1553 {
1554 reg_cache_t *cache = target->reg_cache;
1555
1556 count = 0;
1557 while(cache)
1558 {
1559 int i;
1560 for (i = 0; i < cache->num_regs; i++)
1561 {
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);
1564 free(value);
1565 }
1566 cache = cache->next;
1567 }
1568
1569 return ERROR_OK;
1570 }
1571
1572 /* access a single register by its ordinal number */
1573 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1574 {
1575 int num = strtoul(args[0], NULL, 0);
1576 reg_cache_t *cache = target->reg_cache;
1577
1578 count = 0;
1579 while(cache)
1580 {
1581 int i;
1582 for (i = 0; i < cache->num_regs; i++)
1583 {
1584 if (count++ == num)
1585 {
1586 reg = &cache->reg_list[i];
1587 break;
1588 }
1589 }
1590 if (reg)
1591 break;
1592 cache = cache->next;
1593 }
1594
1595 if (!reg)
1596 {
1597 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1598 return ERROR_OK;
1599 }
1600 } else /* access a single register by its name */
1601 {
1602 reg = register_get_by_name(target->reg_cache, args[0], 1);
1603
1604 if (!reg)
1605 {
1606 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1607 return ERROR_OK;
1608 }
1609 }
1610
1611 /* display a register */
1612 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1613 {
1614 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1615 reg->valid = 0;
1616
1617 if (reg->valid == 0)
1618 {
1619 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1620 if (arch_type == NULL)
1621 {
1622 LOG_ERROR("BUG: encountered unregistered arch type");
1623 return ERROR_OK;
1624 }
1625 arch_type->get(reg);
1626 }
1627 value = buf_to_str(reg->value, reg->size, 16);
1628 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1629 free(value);
1630 return ERROR_OK;
1631 }
1632
1633 /* set register value */
1634 if (argc == 2)
1635 {
1636 u8 *buf = malloc(CEIL(reg->size, 8));
1637 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1638
1639 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1640 if (arch_type == NULL)
1641 {
1642 LOG_ERROR("BUG: encountered unregistered arch type");
1643 return ERROR_OK;
1644 }
1645
1646 arch_type->set(reg, buf);
1647
1648 value = buf_to_str(reg->value, reg->size, 16);
1649 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1650 free(value);
1651
1652 free(buf);
1653
1654 return ERROR_OK;
1655 }
1656
1657 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1658
1659 return ERROR_OK;
1660 }
1661
1662
1663 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1664 {
1665 target_t *target = get_current_target(cmd_ctx);
1666
1667 if (argc == 0)
1668 {
1669 target_poll(target);
1670 target_arch_state(target);
1671 }
1672 else
1673 {
1674 if (strcmp(args[0], "on") == 0)
1675 {
1676 target_continous_poll = 1;
1677 }
1678 else if (strcmp(args[0], "off") == 0)
1679 {
1680 target_continous_poll = 0;
1681 }
1682 else
1683 {
1684 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1685 }
1686 }
1687
1688
1689 return ERROR_OK;
1690 }
1691
1692 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1693 {
1694 int ms = 5000;
1695
1696 if (argc > 0)
1697 {
1698 char *end;
1699
1700 ms = strtoul(args[0], &end, 0) * 1000;
1701 if (*end)
1702 {
1703 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1704 return ERROR_OK;
1705 }
1706 }
1707 target_t *target = get_current_target(cmd_ctx);
1708
1709 return target_wait_state(target, TARGET_HALTED, ms);
1710 }
1711
1712 int target_wait_state(target_t *target, enum target_state state, int ms)
1713 {
1714 int retval;
1715 struct timeval timeout, now;
1716 int once=1;
1717 gettimeofday(&timeout, NULL);
1718 timeval_add_time(&timeout, 0, ms * 1000);
1719
1720 for (;;)
1721 {
1722 if ((retval=target_poll(target))!=ERROR_OK)
1723 return retval;
1724 keep_alive();
1725 if (target->state == state)
1726 {
1727 break;
1728 }
1729 if (once)
1730 {
1731 once=0;
1732 LOG_DEBUG("waiting for target %s...",
1733 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1734 }
1735
1736 gettimeofday(&now, NULL);
1737 if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
1738 {
1739 LOG_ERROR("timed out while waiting for target %s",
1740 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1741 return ERROR_FAIL;
1742 }
1743 }
1744
1745 return ERROR_OK;
1746 }
1747
1748 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1749 {
1750 int retval;
1751 target_t *target = get_current_target(cmd_ctx);
1752
1753 LOG_DEBUG("-");
1754
1755 if ((retval = target_halt(target)) != ERROR_OK)
1756 {
1757 return retval;
1758 }
1759
1760 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1761 }
1762
1763 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1764 {
1765 target_t *target = get_current_target(cmd_ctx);
1766
1767 LOG_USER("requesting target halt and executing a soft reset");
1768
1769 target->type->soft_reset_halt(target);
1770
1771 return ERROR_OK;
1772 }
1773
1774 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1775 {
1776 const Jim_Nvp *n;
1777 enum target_reset_mode reset_mode = RESET_RUN;
1778
1779 if (argc >= 1)
1780 {
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;
1784 }
1785 reset_mode = n->value;
1786 }
1787
1788 /* reset *all* targets */
1789 return target_process_reset(cmd_ctx, reset_mode);
1790 }
1791
1792 int handle_NEWreset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1793 {
1794 int x;
1795 char *cp;
1796
1797 if (argc >= 1){
1798 x = strtol( args[0], &cp, 0 );
1799 if( *cp != 0 ){
1800 command_print( cmd_ctx, "Not numeric: %s\n", args[0] );
1801 return ERROR_COMMAND_SYNTAX_ERROR;
1802 }
1803 USE_OLD_RESET = !!x;
1804 }
1805 command_print( cmd_ctx, "reset method: %d (%s)\n",
1806 USE_OLD_RESET,
1807 USE_OLD_RESET ? "old-method" : "new-method" );
1808 return ERROR_OK;
1809 }
1810
1811 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1812 {
1813 int retval;
1814 target_t *target = get_current_target(cmd_ctx);
1815
1816 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1817
1818 if (argc == 0)
1819 retval = target_resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1820 else if (argc == 1)
1821 retval = target_resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1822 else
1823 {
1824 retval = ERROR_COMMAND_SYNTAX_ERROR;
1825 }
1826
1827 return retval;
1828 }
1829
1830 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1831 {
1832 target_t *target = get_current_target(cmd_ctx);
1833
1834 LOG_DEBUG("-");
1835
1836 if (argc == 0)
1837 target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1838
1839 if (argc == 1)
1840 target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1841
1842 return ERROR_OK;
1843 }
1844
1845 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1846 {
1847 const int line_bytecnt = 32;
1848 int count = 1;
1849 int size = 4;
1850 u32 address = 0;
1851 int line_modulo;
1852 int i;
1853
1854 char output[128];
1855 int output_len;
1856
1857 int retval;
1858
1859 u8 *buffer;
1860 target_t *target = get_current_target(cmd_ctx);
1861
1862 if (argc < 1)
1863 return ERROR_OK;
1864
1865 if (argc == 2)
1866 count = strtoul(args[1], NULL, 0);
1867
1868 address = strtoul(args[0], NULL, 0);
1869
1870
1871 switch (cmd[2])
1872 {
1873 case 'w':
1874 size = 4; line_modulo = line_bytecnt / 4;
1875 break;
1876 case 'h':
1877 size = 2; line_modulo = line_bytecnt / 2;
1878 break;
1879 case 'b':
1880 size = 1; line_modulo = line_bytecnt / 1;
1881 break;
1882 default:
1883 return ERROR_OK;
1884 }
1885
1886 buffer = calloc(count, size);
1887 retval = target->type->read_memory(target, address, size, count, buffer);
1888 if (retval == ERROR_OK)
1889 {
1890 output_len = 0;
1891
1892 for (i = 0; i < count; i++)
1893 {
1894 if (i%line_modulo == 0)
1895 output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1896
1897 switch (size)
1898 {
1899 case 4:
1900 output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
1901 break;
1902 case 2:
1903 output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
1904 break;
1905 case 1:
1906 output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
1907 break;
1908 }
1909
1910 if ((i%line_modulo == line_modulo-1) || (i == count - 1))
1911 {
1912 command_print(cmd_ctx, output);
1913 output_len = 0;
1914 }
1915 }
1916 }
1917
1918 free(buffer);
1919
1920 return retval;
1921 }
1922
1923 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1924 {
1925 u32 address = 0;
1926 u32 value = 0;
1927 int count = 1;
1928 int i;
1929 int wordsize;
1930 target_t *target = get_current_target(cmd_ctx);
1931 u8 value_buf[4];
1932
1933 if ((argc < 2) || (argc > 3))
1934 return ERROR_COMMAND_SYNTAX_ERROR;
1935
1936 address = strtoul(args[0], NULL, 0);
1937 value = strtoul(args[1], NULL, 0);
1938 if (argc == 3)
1939 count = strtoul(args[2], NULL, 0);
1940
1941 switch (cmd[2])
1942 {
1943 case 'w':
1944 wordsize = 4;
1945 target_buffer_set_u32(target, value_buf, value);
1946 break;
1947 case 'h':
1948 wordsize = 2;
1949 target_buffer_set_u16(target, value_buf, value);
1950 break;
1951 case 'b':
1952 wordsize = 1;
1953 value_buf[0] = value;
1954 break;
1955 default:
1956 return ERROR_COMMAND_SYNTAX_ERROR;
1957 }
1958 for (i=0; i<count; i++)
1959 {
1960 int retval;
1961 switch (wordsize)
1962 {
1963 case 4:
1964 retval = target->type->write_memory(target, address + i*wordsize, 4, 1, value_buf);
1965 break;
1966 case 2:
1967 retval = target->type->write_memory(target, address + i*wordsize, 2, 1, value_buf);
1968 break;
1969 case 1:
1970 retval = target->type->write_memory(target, address + i*wordsize, 1, 1, value_buf);
1971 break;
1972 default:
1973 return ERROR_OK;
1974 }
1975 if (retval!=ERROR_OK)
1976 {
1977 return retval;
1978 }
1979 }
1980
1981 return ERROR_OK;
1982
1983 }
1984
1985 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1986 {
1987 u8 *buffer;
1988 u32 buf_cnt;
1989 u32 image_size;
1990 u32 min_address=0;
1991 u32 max_address=0xffffffff;
1992 int i;
1993 int retval;
1994
1995 image_t image;
1996
1997 duration_t duration;
1998 char *duration_text;
1999
2000 target_t *target = get_current_target(cmd_ctx);
2001
2002 if ((argc < 1)||(argc > 5))
2003 {
2004 return ERROR_COMMAND_SYNTAX_ERROR;
2005 }
2006
2007 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
2008 if (argc >= 2)
2009 {
2010 image.base_address_set = 1;
2011 image.base_address = strtoul(args[1], NULL, 0);
2012 }
2013 else
2014 {
2015 image.base_address_set = 0;
2016 }
2017
2018
2019 image.start_address_set = 0;
2020
2021 if (argc>=4)
2022 {
2023 min_address=strtoul(args[3], NULL, 0);
2024 }
2025 if (argc>=5)
2026 {
2027 max_address=strtoul(args[4], NULL, 0)+min_address;
2028 }
2029
2030 if (min_address>max_address)
2031 {
2032 return ERROR_COMMAND_SYNTAX_ERROR;
2033 }
2034
2035
2036 duration_start_measure(&duration);
2037
2038 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2039 {
2040 return ERROR_OK;
2041 }
2042
2043 image_size = 0x0;
2044 retval = ERROR_OK;
2045 for (i = 0; i < image.num_sections; i++)
2046 {
2047 buffer = malloc(image.sections[i].size);
2048 if (buffer == NULL)
2049 {
2050 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2051 break;
2052 }
2053
2054 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2055 {
2056 free(buffer);
2057 break;
2058 }
2059
2060 u32 offset=0;
2061 u32 length=buf_cnt;
2062
2063
2064 /* DANGER!!! beware of unsigned comparision here!!! */
2065
2066 if ((image.sections[i].base_address+buf_cnt>=min_address)&&
2067 (image.sections[i].base_address<max_address))
2068 {
2069 if (image.sections[i].base_address<min_address)
2070 {
2071 /* clip addresses below */
2072 offset+=min_address-image.sections[i].base_address;
2073 length-=offset;
2074 }
2075
2076 if (image.sections[i].base_address+buf_cnt>max_address)
2077 {
2078 length-=(image.sections[i].base_address+buf_cnt)-max_address;
2079 }
2080
2081 if ((retval = target_write_buffer(target, image.sections[i].base_address+offset, length, buffer+offset)) != ERROR_OK)
2082 {
2083 free(buffer);
2084 break;
2085 }
2086 image_size += length;
2087 command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
2088 }
2089
2090 free(buffer);
2091 }
2092
2093 duration_stop_measure(&duration, &duration_text);
2094 if (retval==ERROR_OK)
2095 {
2096 command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
2097 }
2098 free(duration_text);
2099
2100 image_close(&image);
2101
2102 return retval;
2103
2104 }
2105
2106 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2107 {
2108 fileio_t fileio;
2109
2110 u32 address;
2111 u32 size;
2112 u8 buffer[560];
2113 int retval=ERROR_OK;
2114
2115 duration_t duration;
2116 char *duration_text;
2117
2118 target_t *target = get_current_target(cmd_ctx);
2119
2120 if (argc != 3)
2121 {
2122 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2123 return ERROR_OK;
2124 }
2125
2126 address = strtoul(args[1], NULL, 0);
2127 size = strtoul(args[2], NULL, 0);
2128
2129 if ((address & 3) || (size & 3))
2130 {
2131 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
2132 return ERROR_OK;
2133 }
2134
2135 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2136 {
2137 return ERROR_OK;
2138 }
2139
2140 duration_start_measure(&duration);
2141
2142 while (size > 0)
2143 {
2144 u32 size_written;
2145 u32 this_run_size = (size > 560) ? 560 : size;
2146
2147 retval = target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
2148 if (retval != ERROR_OK)
2149 {
2150 break;
2151 }
2152
2153 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2154 if (retval != ERROR_OK)
2155 {
2156 break;
2157 }
2158
2159 size -= this_run_size;
2160 address += this_run_size;
2161 }
2162
2163 fileio_close(&fileio);
2164
2165 duration_stop_measure(&duration, &duration_text);
2166 if (retval==ERROR_OK)
2167 {
2168 command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
2169 }
2170 free(duration_text);
2171
2172 return ERROR_OK;
2173 }
2174
2175 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2176 {
2177 u8 *buffer;
2178 u32 buf_cnt;
2179 u32 image_size;
2180 int i;
2181 int retval;
2182 u32 checksum = 0;
2183 u32 mem_checksum = 0;
2184
2185 image_t image;
2186
2187 duration_t duration;
2188 char *duration_text;
2189
2190 target_t *target = get_current_target(cmd_ctx);
2191
2192 if (argc < 1)
2193 {
2194 return ERROR_COMMAND_SYNTAX_ERROR;
2195 }
2196
2197 if (!target)
2198 {
2199 LOG_ERROR("no target selected");
2200 return ERROR_FAIL;
2201 }
2202
2203 duration_start_measure(&duration);
2204
2205 if (argc >= 2)
2206 {
2207 image.base_address_set = 1;
2208 image.base_address = strtoul(args[1], NULL, 0);
2209 }
2210 else
2211 {
2212 image.base_address_set = 0;
2213 image.base_address = 0x0;
2214 }
2215
2216 image.start_address_set = 0;
2217
2218 if ((retval=image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2219 {
2220 return retval;
2221 }
2222
2223 image_size = 0x0;
2224 retval=ERROR_OK;
2225 for (i = 0; i < image.num_sections; i++)
2226 {
2227 buffer = malloc(image.sections[i].size);
2228 if (buffer == NULL)
2229 {
2230 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2231 break;
2232 }
2233 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2234 {
2235 free(buffer);
2236 break;
2237 }
2238
2239 /* calculate checksum of image */
2240 image_calculate_checksum( buffer, buf_cnt, &checksum );
2241
2242 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2243 if( retval != ERROR_OK )
2244 {
2245 free(buffer);
2246 break;
2247 }
2248
2249 if( checksum != mem_checksum )
2250 {
2251 /* failed crc checksum, fall back to a binary compare */
2252 u8 *data;
2253
2254 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2255
2256 data = (u8*)malloc(buf_cnt);
2257
2258 /* Can we use 32bit word accesses? */
2259 int size = 1;
2260 int count = buf_cnt;
2261 if ((count % 4) == 0)
2262 {
2263 size *= 4;
2264 count /= 4;
2265 }
2266 retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
2267 if (retval == ERROR_OK)
2268 {
2269 int t;
2270 for (t = 0; t < buf_cnt; t++)
2271 {
2272 if (data[t] != buffer[t])
2273 {
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]);
2275 free(data);
2276 free(buffer);
2277 retval=ERROR_FAIL;
2278 goto done;
2279 }
2280 }
2281 }
2282
2283 free(data);
2284 }
2285
2286 free(buffer);
2287 image_size += buf_cnt;
2288 }
2289 done:
2290 duration_stop_measure(&duration, &duration_text);
2291 if (retval==ERROR_OK)
2292 {
2293 command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
2294 }
2295 free(duration_text);
2296
2297 image_close(&image);
2298
2299 return retval;
2300 }
2301
2302 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2303 {
2304 int retval;
2305 target_t *target = get_current_target(cmd_ctx);
2306
2307 if (argc == 0)
2308 {
2309 breakpoint_t *breakpoint = target->breakpoints;
2310
2311 while (breakpoint)
2312 {
2313 if (breakpoint->type == BKPT_SOFT)
2314 {
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);
2317 free(buf);
2318 }
2319 else
2320 {
2321 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
2322 }
2323 breakpoint = breakpoint->next;
2324 }
2325 }
2326 else if (argc >= 2)
2327 {
2328 int hw = BKPT_SOFT;
2329 u32 length = 0;
2330
2331 length = strtoul(args[1], NULL, 0);
2332
2333 if (argc >= 3)
2334 if (strcmp(args[2], "hw") == 0)
2335 hw = BKPT_HARD;
2336
2337 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
2338 {
2339 LOG_ERROR("Failure setting breakpoints");
2340 }
2341 else
2342 {
2343 command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
2344 }
2345 }
2346 else
2347 {
2348 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2349 }
2350
2351 return ERROR_OK;
2352 }
2353
2354 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2355 {
2356 target_t *target = get_current_target(cmd_ctx);
2357
2358 if (argc > 0)
2359 breakpoint_remove(target, strtoul(args[0], NULL, 0));
2360
2361 return ERROR_OK;
2362 }
2363
2364 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2365 {
2366 target_t *target = get_current_target(cmd_ctx);
2367 int retval;
2368
2369 if (argc == 0)
2370 {
2371 watchpoint_t *watchpoint = target->watchpoints;
2372
2373 while (watchpoint)
2374 {
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;
2377 }
2378 }
2379 else if (argc >= 2)
2380 {
2381 enum watchpoint_rw type = WPT_ACCESS;
2382 u32 data_value = 0x0;
2383 u32 data_mask = 0xffffffff;
2384
2385 if (argc >= 3)
2386 {
2387 switch(args[2][0])
2388 {
2389 case 'r':
2390 type = WPT_READ;
2391 break;
2392 case 'w':
2393 type = WPT_WRITE;
2394 break;
2395 case 'a':
2396 type = WPT_ACCESS;
2397 break;
2398 default:
2399 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2400 return ERROR_OK;
2401 }
2402 }
2403 if (argc >= 4)
2404 {
2405 data_value = strtoul(args[3], NULL, 0);
2406 }
2407 if (argc >= 5)
2408 {
2409 data_mask = strtoul(args[4], NULL, 0);
2410 }
2411
2412 if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
2413 strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
2414 {
2415 LOG_ERROR("Failure setting breakpoints");
2416 }
2417 }
2418 else
2419 {
2420 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2421 }
2422
2423 return ERROR_OK;
2424 }
2425
2426 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2427 {
2428 target_t *target = get_current_target(cmd_ctx);
2429
2430 if (argc > 0)
2431 watchpoint_remove(target, strtoul(args[0], NULL, 0));
2432
2433 return ERROR_OK;
2434 }
2435
2436 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
2437 {
2438 int retval;
2439 target_t *target = get_current_target(cmd_ctx);
2440 u32 va;
2441 u32 pa;
2442
2443 if (argc != 1)
2444 {
2445 return ERROR_COMMAND_SYNTAX_ERROR;
2446 }
2447 va = strtoul(args[0], NULL, 0);
2448
2449 retval = target->type->virt2phys(target, va, &pa);
2450 if (retval == ERROR_OK)
2451 {
2452 command_print(cmd_ctx, "Physical address 0x%08x", pa);
2453 }
2454 else
2455 {
2456 /* lower levels will have logged a detailed error which is
2457 * forwarded to telnet/GDB session.
2458 */
2459 }
2460 return retval;
2461 }
2462 static void writeLong(FILE *f, int l)
2463 {
2464 int i;
2465 for (i=0; i<4; i++)
2466 {
2467 char c=(l>>(i*8))&0xff;
2468 fwrite(&c, 1, 1, f);
2469 }
2470
2471 }
2472 static void writeString(FILE *f, char *s)
2473 {
2474 fwrite(s, 1, strlen(s), f);
2475 }
2476
2477
2478
2479 // Dump a gmon.out histogram file.
2480 static void writeGmon(u32 *samples, int sampleNum, char *filename)
2481 {
2482 int i;
2483 FILE *f=fopen(filename, "w");
2484 if (f==NULL)
2485 return;
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
2491
2492 fwrite("", 1, 1, f); // GMON_TAG_TIME_HIST
2493
2494 // figure out bucket size
2495 u32 min=samples[0];
2496 u32 max=samples[0];
2497 for (i=0; i<sampleNum; i++)
2498 {
2499 if (min>samples[i])
2500 {
2501 min=samples[i];
2502 }
2503 if (max<samples[i])
2504 {
2505 max=samples[i];
2506 }
2507 }
2508
2509 int addressSpace=(max-min+1);
2510
2511 static int const maxBuckets=256*1024; // maximum buckets.
2512 int length=addressSpace;
2513 if (length > maxBuckets)
2514 {
2515 length=maxBuckets;
2516 }
2517 int *buckets=malloc(sizeof(int)*length);
2518 if (buckets==NULL)
2519 {
2520 fclose(f);
2521 return;
2522 }
2523 memset(buckets, 0, sizeof(int)*length);
2524 for (i=0; i<sampleNum;i++)
2525 {
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
2531 buckets[index]++;
2532 }
2533
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++)
2541 {
2542 fwrite("", 1, 1, f); // padding
2543 }
2544 writeString(f, "s");
2545
2546 // append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size)
2547
2548 char *data=malloc(2*length);
2549 if (data!=NULL)
2550 {
2551 for (i=0; i<length;i++)
2552 {
2553 int val;
2554 val=buckets[i];
2555 if (val>65535)
2556 {
2557 val=65535;
2558 }
2559 data[i*2]=val&0xff;
2560 data[i*2+1]=(val>>8)&0xff;
2561 }
2562 free(buckets);
2563 fwrite(data, 1, length*2, f);
2564 free(data);
2565 } else
2566 {
2567 free(buckets);
2568 }
2569
2570 fclose(f);
2571 }
2572
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)
2575 {
2576 target_t *target = get_current_target(cmd_ctx);
2577 struct timeval timeout, now;
2578
2579 gettimeofday(&timeout, NULL);
2580 if (argc!=2)
2581 {
2582 return ERROR_COMMAND_SYNTAX_ERROR;
2583 }
2584 char *end;
2585 timeval_add_time(&timeout, strtoul(args[0], &end, 0), 0);
2586 if (*end)
2587 {
2588 return ERROR_OK;
2589 }
2590
2591 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
2592
2593 static const int maxSample=10000;
2594 u32 *samples=malloc(sizeof(u32)*maxSample);
2595 if (samples==NULL)
2596 return ERROR_OK;
2597
2598 int numSamples=0;
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);
2602
2603 for (;;)
2604 {
2605 target_poll(target);
2606 if (target->state == TARGET_HALTED)
2607 {
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)
2614 {
2615 // We want to quickly sample the PC.
2616 target_halt(target);
2617 } else
2618 {
2619 command_print(cmd_ctx, "Target not halted or running");
2620 retval=ERROR_OK;
2621 break;
2622 }
2623 if (retval!=ERROR_OK)
2624 {
2625 break;
2626 }
2627
2628 gettimeofday(&now, NULL);
2629 if ((numSamples>=maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
2630 {
2631 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2632 target_poll(target);
2633 if (target->state == TARGET_HALTED)
2634 {
2635 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2636 }
2637 target_poll(target);
2638 writeGmon(samples, numSamples, args[1]);
2639 command_print(cmd_ctx, "Wrote %s", args[1]);
2640 break;
2641 }
2642 }
2643 free(samples);
2644
2645 return ERROR_OK;
2646 }
2647
2648 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 val)
2649 {
2650 char *namebuf;
2651 Jim_Obj *nameObjPtr, *valObjPtr;
2652 int result;
2653
2654 namebuf = alloc_printf("%s(%d)", varname, idx);
2655 if (!namebuf)
2656 return JIM_ERR;
2657
2658 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2659 valObjPtr = Jim_NewIntObj(interp, val);
2660 if (!nameObjPtr || !valObjPtr)
2661 {
2662 free(namebuf);
2663 return JIM_ERR;
2664 }
2665
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);
2671 free(namebuf);
2672 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
2673 return result;
2674 }
2675
2676 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2677 {
2678 command_context_t *context;
2679 target_t *target;
2680
2681 context = Jim_GetAssocData(interp, "context");
2682 if (context == NULL)
2683 {
2684 LOG_ERROR("mem2array: no command context");
2685 return JIM_ERR;
2686 }
2687 target = get_current_target(context);
2688 if (target == NULL)
2689 {
2690 LOG_ERROR("mem2array: no current target");
2691 return JIM_ERR;
2692 }
2693
2694 return target_mem2array(interp, target, argc,argv);
2695 }
2696
2697 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
2698 {
2699 long l;
2700 u32 width;
2701 u32 len;
2702 u32 addr;
2703 u32 count;
2704 u32 v;
2705 const char *varname;
2706 u8 buffer[4096];
2707 int i, n, e, retval;
2708
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
2713 */
2714 if (argc != 5) {
2715 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2716 return JIM_ERR;
2717 }
2718 varname = Jim_GetString(argv[1], &len);
2719 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2720
2721 e = Jim_GetLong(interp, argv[2], &l);
2722 width = l;
2723 if (e != JIM_OK) {
2724 return e;
2725 }
2726
2727 e = Jim_GetLong(interp, argv[3], &l);
2728 addr = l;
2729 if (e != JIM_OK) {
2730 return e;
2731 }
2732 e = Jim_GetLong(interp, argv[4], &l);
2733 len = l;
2734 if (e != JIM_OK) {
2735 return e;
2736 }
2737 switch (width) {
2738 case 8:
2739 width = 1;
2740 break;
2741 case 16:
2742 width = 2;
2743 break;
2744 case 32:
2745 width = 4;
2746 break;
2747 default:
2748 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2749 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2750 return JIM_ERR;
2751 }
2752 if (len == 0) {
2753 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2754 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
2755 return JIM_ERR;
2756 }
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);
2760 return JIM_ERR;
2761 }
2762 /* absurd transfer size? */
2763 if (len > 65536) {
2764 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2765 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
2766 return JIM_ERR;
2767 }
2768
2769 if ((width == 1) ||
2770 ((width == 2) && ((addr & 1) == 0)) ||
2771 ((width == 4) && ((addr & 3) == 0))) {
2772 /* all is well */
2773 } else {
2774 char buf[100];
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);
2778 return JIM_ERR;
2779 }
2780
2781 /* Transfer loop */
2782
2783 /* index counter */
2784 n = 0;
2785 /* assume ok */
2786 e = JIM_OK;
2787 while (len) {
2788 /* Slurp... in buffer size chunks */
2789
2790 count = len; /* in objects.. */
2791 if (count > (sizeof(buffer)/width)) {
2792 count = (sizeof(buffer)/width);
2793 }
2794
2795 retval = target->type->read_memory( target, addr, width, count, buffer );
2796 if (retval != ERROR_OK) {
2797 /* BOO !*/
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);
2801 e = JIM_ERR;
2802 len = 0;
2803 } else {
2804 v = 0; /* shut up gcc */
2805 for (i = 0 ;i < count ;i++, n++) {
2806 switch (width) {
2807 case 4:
2808 v = target_buffer_get_u32(target, &buffer[i*width]);
2809 break;
2810 case 2:
2811 v = target_buffer_get_u16(target, &buffer[i*width]);
2812 break;
2813 case 1:
2814 v = buffer[i] & 0x0ff;
2815 break;
2816 }
2817 new_int_array_element(interp, varname, n, v);
2818 }
2819 len -= count;
2820 }
2821 }
2822
2823 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2824
2825 return JIM_OK;
2826 }
2827
2828 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 *val)
2829 {
2830 char *namebuf;
2831 Jim_Obj *nameObjPtr, *valObjPtr;
2832 int result;
2833 long l;
2834
2835 namebuf = alloc_printf("%s(%d)", varname, idx);
2836 if (!namebuf)
2837 return JIM_ERR;
2838
2839 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2840 if (!nameObjPtr)
2841 {
2842 free(namebuf);
2843 return JIM_ERR;
2844 }
2845
2846 Jim_IncrRefCount(nameObjPtr);
2847 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
2848 Jim_DecrRefCount(interp, nameObjPtr);
2849 free(namebuf);
2850 if (valObjPtr == NULL)
2851 return JIM_ERR;
2852
2853 result = Jim_GetLong(interp, valObjPtr, &l);
2854 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
2855 *val = l;
2856 return result;
2857 }
2858
2859 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2860 {
2861 command_context_t *context;
2862 target_t *target;
2863
2864 context = Jim_GetAssocData(interp, "context");
2865 if (context == NULL){
2866 LOG_ERROR("array2mem: no command context");
2867 return JIM_ERR;
2868 }
2869 target = get_current_target(context);
2870 if (target == NULL){
2871 LOG_ERROR("array2mem: no current target");
2872 return JIM_ERR;
2873 }
2874
2875 return target_array2mem( interp,target, argc, argv );
2876 }
2877
2878
2879 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
2880 {
2881 long l;
2882 u32 width;
2883 u32 len;
2884 u32 addr;
2885 u32 count;
2886 u32 v;
2887 const char *varname;
2888 u8 buffer[4096];
2889 int i, n, e, retval;
2890
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
2895 */
2896 if (argc != 5) {
2897 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2898 return JIM_ERR;
2899 }
2900 varname = Jim_GetString(argv[1], &len);
2901 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2902
2903 e = Jim_GetLong(interp, argv[2], &l);
2904 width = l;
2905 if (e != JIM_OK) {
2906 return e;
2907 }
2908
2909 e = Jim_GetLong(interp, argv[3], &l);
2910 addr = l;
2911 if (e != JIM_OK) {
2912 return e;
2913 }
2914 e = Jim_GetLong(interp, argv[4], &l);
2915 len = l;
2916 if (e != JIM_OK) {
2917 return e;
2918 }
2919 switch (width) {
2920 case 8:
2921 width = 1;
2922 break;
2923 case 16:
2924 width = 2;
2925 break;
2926 case 32:
2927 width = 4;
2928 break;
2929 default:
2930 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2931 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2932 return JIM_ERR;
2933 }
2934 if (len == 0) {
2935 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2936 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
2937 return JIM_ERR;
2938 }
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);
2942 return JIM_ERR;
2943 }
2944 /* absurd transfer size? */
2945 if (len > 65536) {
2946 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2947 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
2948 return JIM_ERR;
2949 }
2950
2951 if ((width == 1) ||
2952 ((width == 2) && ((addr & 1) == 0)) ||
2953 ((width == 4) && ((addr & 3) == 0))) {
2954 /* all is well */
2955 } else {
2956 char buf[100];
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);
2960 return JIM_ERR;
2961 }
2962
2963
2964 /* Transfer loop */
2965
2966 /* index counter */
2967 n = 0;
2968 /* assume ok */
2969 e = JIM_OK;
2970 while (len) {
2971 /* Slurp... in buffer size chunks */
2972
2973 count = len; /* in objects.. */
2974 if (count > (sizeof(buffer)/width)) {
2975 count = (sizeof(buffer)/width);
2976 }
2977
2978 v = 0; /* shut up gcc */
2979 for (i = 0 ;i < count ;i++, n++) {
2980 get_int_array_element(interp, varname, n, &v);
2981 switch (width) {
2982 case 4:
2983 target_buffer_set_u32(target, &buffer[i*width], v);
2984 break;
2985 case 2:
2986 target_buffer_set_u16(target, &buffer[i*width], v);
2987 break;
2988 case 1:
2989 buffer[i] = v & 0x0ff;
2990 break;
2991 }
2992 }
2993 len -= count;
2994
2995 retval = target->type->write_memory(target, addr, width, count, buffer);
2996 if (retval != ERROR_OK) {
2997 /* BOO !*/
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);
3001 e = JIM_ERR;
3002 len = 0;
3003 }
3004 }
3005
3006 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3007
3008 return JIM_OK;
3009 }
3010
3011 void
3012 target_all_handle_event( enum target_event e )
3013 {
3014 target_t *target;
3015
3016
3017 LOG_DEBUG( "**all*targets: event: %d, %s",
3018 e,
3019 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3020
3021 target = all_targets;
3022 while (target){
3023 target_handle_event( target, e );
3024 target = target->next;
3025 }
3026 }
3027
3028 void
3029 target_handle_event( target_t *target, enum target_event e )
3030 {
3031 target_event_action_t *teap;
3032 int done;
3033
3034 teap = target->event_action;
3035
3036 done = 0;
3037 while( teap ){
3038 if( teap->event == e ){
3039 done = 1;
3040 LOG_DEBUG( "target: (%d) %s (%s) event: %d (%s) action: %s\n",
3041 target->target_number,
3042 target->cmd_name,
3043 target->type->name,
3044 e,
3045 Jim_Nvp_value2name_simple( nvp_target_event, e )->name,
3046 Jim_GetString( teap->body, NULL ) );
3047 Jim_EvalObj( interp, teap->body );
3048 }
3049 teap = teap->next;
3050 }
3051 if( !done ){
3052 LOG_DEBUG( "event: %d %s - no action",
3053 e,
3054 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3055 }
3056 }
3057
3058 enum target_cfg_param {
3059 TCFG_TYPE,
3060 TCFG_EVENT,
3061 TCFG_WORK_AREA_VIRT,
3062 TCFG_WORK_AREA_PHYS,
3063 TCFG_WORK_AREA_SIZE,
3064 TCFG_WORK_AREA_BACKUP,
3065 TCFG_ENDIAN,
3066 TCFG_VARIANT,
3067 TCFG_CHAIN_POSITION,
3068 };
3069
3070
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 },
3081
3082 { .name = NULL, .value = -1 }
3083 };
3084
3085
3086 static int
3087 target_configure( Jim_GetOptInfo *goi,
3088 target_t *target )
3089 {
3090 Jim_Nvp *n;
3091 Jim_Obj *o;
3092 jim_wide w;
3093 char *cp;
3094 int e;
3095
3096
3097 /* parse config or cget options ... */
3098 while( goi->argc > 0 ){
3099 Jim_SetEmptyResult( goi->interp );
3100 //Jim_GetOpt_Debug( goi );
3101
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 );
3106 if( e == JIM_OK ){
3107 /* more? */
3108 continue;
3109 }
3110 if( e == JIM_ERR ){
3111 /* An error */
3112 return e;
3113 }
3114 /* otherwise we 'continue' below */
3115 }
3116 e = Jim_GetOpt_Nvp( goi, nvp_config_opts, &n );
3117 if( e != JIM_OK ){
3118 Jim_GetOpt_NvpUnknown( goi, nvp_config_opts, 0 );
3119 return e;
3120 }
3121 switch( n->value ){
3122 case TCFG_TYPE:
3123 /* not setable */
3124 if( goi->isconfigure ){
3125 Jim_SetResult_sprintf( goi->interp, "not setable: %s", n->name );
3126 return JIM_ERR;
3127 } else {
3128 no_params:
3129 if( goi->argc != 0 ){
3130 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "NO PARAMS");
3131 return JIM_ERR;
3132 }
3133 }
3134 Jim_SetResultString( goi->interp, target->type->name, -1 );
3135 /* loop for more */
3136 break;
3137 case TCFG_EVENT:
3138 if( goi->argc == 0 ){
3139 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3140 return JIM_ERR;
3141 }
3142
3143 e = Jim_GetOpt_Nvp( goi, nvp_target_event, &n );
3144 if( e != JIM_OK ){
3145 Jim_GetOpt_NvpUnknown( goi, nvp_target_event, 1 );
3146 return e;
3147 }
3148
3149 if( goi->isconfigure ){
3150 if( goi->argc == 0 ){
3151 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3152 return JIM_ERR;
3153 }
3154 } else {
3155 if( goi->argc != 0 ){
3156 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3157 return JIM_ERR;
3158 }
3159 }
3160
3161
3162 {
3163 target_event_action_t *teap;
3164
3165 teap = target->event_action;
3166 /* replace existing? */
3167 while( teap ){
3168 if( teap->event == n->value ){
3169 break;
3170 }
3171 teap = teap->next;
3172 }
3173
3174 if( goi->isconfigure ){
3175 if( teap == NULL ){
3176 /* create new */
3177 teap = calloc( 1, sizeof(*teap) );
3178 }
3179 teap->event = n->value;
3180 Jim_GetOpt_Obj( goi, &o );
3181 if( teap->body ){
3182 Jim_DecrRefCount( interp, teap->body );
3183 }
3184 teap->body = Jim_DuplicateObj( goi->interp, o );
3185 /*
3186 * FIXME:
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.
3194 */
3195 Jim_IncrRefCount( teap->body );
3196
3197 /* add to head of event list */
3198 teap->next = target->event_action;
3199 target->event_action = teap;
3200 Jim_SetEmptyResult(goi->interp);
3201 } else {
3202 /* get */
3203 if( teap == NULL ){
3204 Jim_SetEmptyResult( goi->interp );
3205 } else {
3206 Jim_SetResult( goi->interp, Jim_DuplicateObj( goi->interp, teap->body ) );
3207 }
3208 }
3209 }
3210 /* loop for more */
3211 break;
3212
3213 case TCFG_WORK_AREA_VIRT:
3214 if( goi->isconfigure ){
3215 target_free_all_working_areas(target);
3216 e = Jim_GetOpt_Wide( goi, &w );
3217 if( e != JIM_OK ){
3218 return e;
3219 }
3220 target->working_area_virt = w;
3221 } else {
3222 if( goi->argc != 0 ){
3223 goto no_params;
3224 }
3225 }
3226 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_virt ) );
3227 /* loop for more */
3228 break;
3229
3230 case TCFG_WORK_AREA_PHYS:
3231 if( goi->isconfigure ){
3232 target_free_all_working_areas(target);
3233 e = Jim_GetOpt_Wide( goi, &w );
3234 if( e != JIM_OK ){
3235 return e;
3236 }
3237 target->working_area_phys = w;
3238 } else {
3239 if( goi->argc != 0 ){
3240 goto no_params;
3241 }
3242 }
3243 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_phys ) );
3244 /* loop for more */
3245 break;
3246
3247 case TCFG_WORK_AREA_SIZE:
3248 if( goi->isconfigure ){
3249 target_free_all_working_areas(target);
3250 e = Jim_GetOpt_Wide( goi, &w );
3251 if( e != JIM_OK ){
3252 return e;
3253 }
3254 target->working_area_size = w;
3255 } else {
3256 if( goi->argc != 0 ){
3257 goto no_params;
3258 }
3259 }
3260 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
3261 /* loop for more */
3262 break;
3263
3264 case TCFG_WORK_AREA_BACKUP:
3265 if( goi->isconfigure ){
3266 target_free_all_working_areas(target);
3267 e = Jim_GetOpt_Wide( goi, &w );
3268 if( e != JIM_OK ){
3269 return e;
3270 }
3271 /* make this exactly 1 or 0 */
3272 target->backup_working_area = (!!w);
3273 } else {
3274 if( goi->argc != 0 ){
3275 goto no_params;
3276 }
3277 }
3278 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
3279 /* loop for more e*/
3280 break;
3281
3282 case TCFG_ENDIAN:
3283 if( goi->isconfigure ){
3284 e = Jim_GetOpt_Nvp( goi, nvp_target_endian, &n );
3285 if( e != JIM_OK ){
3286 Jim_GetOpt_NvpUnknown( goi, nvp_target_endian, 1 );
3287 return e;
3288 }
3289 target->endianness = n->value;
3290 } else {
3291 if( goi->argc != 0 ){
3292 goto no_params;
3293 }
3294 }
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 );
3299 }
3300 Jim_SetResultString( goi->interp, n->name, -1 );
3301 /* loop for more */
3302 break;
3303
3304 case TCFG_VARIANT:
3305 if( goi->isconfigure ){
3306 if( goi->argc < 1 ){
3307 Jim_SetResult_sprintf( goi->interp,
3308 "%s ?STRING?",
3309 n->name );
3310 return JIM_ERR;
3311 }
3312 if( target->variant ){
3313 free((void *)(target->variant));
3314 }
3315 e = Jim_GetOpt_String( goi, &cp, NULL );
3316 target->variant = strdup(cp);
3317 } else {
3318 if( goi->argc != 0 ){
3319 goto no_params;
3320 }
3321 }
3322 Jim_SetResultString( goi->interp, target->variant,-1 );
3323 /* loop for more */
3324 break;
3325 case TCFG_CHAIN_POSITION:
3326 if( goi->isconfigure ){
3327 target_free_all_working_areas(target);
3328 e = Jim_GetOpt_Wide( goi, &w );
3329 if( e != JIM_OK ){
3330 return e;
3331 }
3332 /* make this exactly 1 or 0 */
3333 target->chain_position = w;
3334 } else {
3335 if( goi->argc != 0 ){
3336 goto no_params;
3337 }
3338 }
3339 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->chain_position ) );
3340 /* loop for more e*/
3341 break;
3342 }
3343 } /* while( goi->argc ) */
3344 /* done - we return */
3345 return JIM_OK;
3346 }
3347
3348
3349 /** this is the 'tcl' handler for the target specific command */
3350 static int
3351 tcl_target_func( Jim_Interp *interp,
3352 int argc,
3353 Jim_Obj *const *argv )
3354 {
3355 Jim_GetOptInfo goi;
3356 jim_wide a,b,c;
3357 int x,y,z;
3358 u8 target_buf[32];
3359 Jim_Nvp *n;
3360 target_t *target;
3361 struct command_context_s *cmd_ctx;
3362 int e;
3363
3364
3365 enum {
3366 TS_CMD_CONFIGURE,
3367 TS_CMD_CGET,
3368
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,
3373 TS_CMD_EXAMINE,
3374 TS_CMD_POLL,
3375 TS_CMD_RESET,
3376 TS_CMD_HALT,
3377 TS_CMD_WAITSTATE,
3378 TS_CMD_EVENTLIST,
3379 TS_CMD_CURSTATE,
3380 TS_CMD_INVOKE_EVENT,
3381 };
3382
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 },
3396
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 },
3403
3404 { .name = NULL, .value = -1 },
3405 };
3406
3407
3408 /* go past the "command" */
3409 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
3410
3411 target = Jim_CmdPrivData( goi.interp );
3412 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3413
3414 /* commands here are in an NVP table */
3415 e = Jim_GetOpt_Nvp( &goi, target_options, &n );
3416 if( e != JIM_OK ){
3417 Jim_GetOpt_NvpUnknown( &goi, target_options, 0 );
3418 return e;
3419 }
3420 // Assume blank result
3421 Jim_SetEmptyResult( goi.interp );
3422
3423 switch( n->value ){
3424 case TS_CMD_CONFIGURE:
3425 if( goi.argc < 2 ){
3426 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3427 return JIM_ERR;
3428 }
3429 goi.isconfigure = 1;
3430 return target_configure( &goi, target );
3431 case TS_CMD_CGET:
3432 // some things take params
3433 if( goi.argc < 1 ){
3434 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "missing: ?-option?");
3435 return JIM_ERR;
3436 }
3437 goi.isconfigure = 0;
3438 return target_configure( &goi, target );
3439 break;
3440 case TS_CMD_MWW:
3441 case TS_CMD_MWH:
3442 case TS_CMD_MWB:
3443 /* argv[0] = cmd
3444 * argv[1] = address
3445 * argv[2] = data
3446 * argv[3] = optional count.
3447 */
3448
3449 if( (goi.argc == 3) || (goi.argc == 4) ){
3450 /* all is well */
3451 } else {
3452 mwx_error:
3453 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR DATA [COUNT]", n->name );
3454 return JIM_ERR;
3455 }
3456
3457 e = Jim_GetOpt_Wide( &goi, &a );
3458 if( e != JIM_OK ){
3459 goto mwx_error;
3460 }
3461
3462 e = Jim_GetOpt_Wide( &goi, &b );
3463 if( e != JIM_OK ){
3464 goto mwx_error;
3465 }
3466 if( goi.argc ){
3467 e = Jim_GetOpt_Wide( &goi, &c );
3468 if( e != JIM_OK ){
3469 goto mwx_error;
3470 }
3471 } else {
3472 c = 1;
3473 }
3474
3475 switch( n->value ){
3476 case TS_CMD_MWW:
3477 target_buffer_set_u32( target, target_buf, b );
3478 b = 4;
3479 break;
3480 case TS_CMD_MWH:
3481 target_buffer_set_u16( target, target_buf, b );
3482 b = 2;
3483 break;
3484 case TS_CMD_MWB:
3485 target_buffer_set_u8( target, target_buf, b );
3486 b = 1;
3487 break;
3488 }
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 );
3493 return JIM_ERR;
3494 }
3495 /* b = width */
3496 a = a + b;
3497 }
3498 return JIM_OK;
3499 break;
3500
3501 /* display */
3502 case TS_CMD_MDW:
3503 case TS_CMD_MDH:
3504 case TS_CMD_MDB:
3505 /* argv[0] = command
3506 * argv[1] = address
3507 * argv[2] = optional count
3508 */
3509 if( (goi.argc == 2) || (goi.argc == 3) ){
3510 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR [COUNT]", n->name );
3511 return JIM_ERR;
3512 }
3513 e = Jim_GetOpt_Wide( &goi, &a );
3514 if( e != JIM_OK ){
3515 return JIM_ERR;
3516 }
3517 if( goi.argc ){
3518 e = Jim_GetOpt_Wide( &goi, &c );
3519 if( e != JIM_OK ){
3520 return JIM_ERR;
3521 }
3522 } else {
3523 c = 1;
3524 }
3525 b = 1; /* shut up gcc */
3526 switch( n->value ){
3527 case TS_CMD_MDW:
3528 b = 4;
3529 break;
3530 case TS_CMD_MDH:
3531 b = 2;
3532 break;
3533 case TS_CMD_MDB:
3534 b = 1;
3535 break;
3536 }
3537
3538 /* convert to "bytes" */
3539 c = c * b;
3540 /* count is now in 'BYTES' */
3541 while( c > 0 ){
3542 y = c;
3543 if( y > 16 ){
3544 y = 16;
3545 }
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) );
3549 return JIM_ERR;
3550 }
3551
3552 Jim_fprintf( interp, interp->cookie_stdout, "0x%08x ", (int)(a) );
3553 switch( b ){
3554 case 4:
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) );
3558 }
3559 for( ; (x < 16) ; x += 4 ){
3560 Jim_fprintf( interp, interp->cookie_stdout, " " );
3561 }
3562 break;
3563 case 2:
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) );
3567 }
3568 for( ; (x < 16) ; x += 2 ){
3569 Jim_fprintf( interp, interp->cookie_stdout, " " );
3570 }
3571 break;
3572 case 1:
3573 default:
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) );
3577 }
3578 for( ; (x < 16) ; x += 1 ){
3579 Jim_fprintf( interp, interp->cookie_stdout, " " );
3580 }
3581 break;
3582 }
3583 /* ascii-ify the bytes */
3584 for( x = 0 ; x < y ; x++ ){
3585 if( (target_buf[x] >= 0x20) &&
3586 (target_buf[x] <= 0x7e) ){
3587 /* good */
3588 } else {
3589 /* smack it */
3590 target_buf[x] = '.';
3591 }
3592 }
3593 /* space pad */
3594 while( x < 16 ){
3595 target_buf[x] = ' ';
3596 x++;
3597 }
3598 /* terminate */
3599 target_buf[16] = 0;
3600 /* print - with a newline */
3601 Jim_fprintf( interp, interp->cookie_stdout, "%s\n", target_buf );
3602 /* NEXT... */
3603 c -= 16;
3604 a += 16;
3605 }
3606 return JIM_OK;
3607 case TS_CMD_MEM2ARRAY:
3608 return target_mem2array( goi.interp, target, goi.argc, goi.argv );
3609 break;
3610 case TS_CMD_ARRAY2MEM:
3611 return target_array2mem( goi.interp, target, goi.argc, goi.argv );
3612 break;
3613 case TS_CMD_EXAMINE:
3614 if( goi.argc ){
3615 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3616 return JIM_ERR;
3617 }
3618 e = target->type->examine( target );
3619 if( e != ERROR_OK ){
3620 Jim_SetResult_sprintf( interp, "examine-fails: %d", e );
3621 return JIM_ERR;
3622 }
3623 return JIM_OK;
3624 case TS_CMD_POLL:
3625 if( goi.argc ){
3626 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3627 return JIM_ERR;
3628 }
3629 if( !(target->type->examined) ){
3630 e = ERROR_TARGET_NOT_EXAMINED;
3631 } else {
3632 e = target->type->poll( target );
3633 }
3634 if( e != ERROR_OK ){
3635 Jim_SetResult_sprintf( interp, "poll-fails: %d", e );
3636 return JIM_ERR;
3637 } else {
3638 return JIM_OK;
3639 }
3640 break;
3641 case TS_CMD_RESET:
3642 if( goi.argc != 2 ){
3643 Jim_WrongNumArgs( interp, 2, argv, "t|f|assert|deassert BOOL");
3644 return JIM_ERR;
3645 }
3646 e = Jim_GetOpt_Nvp( &goi, nvp_assert, &n );
3647 if( e != JIM_OK ){
3648 Jim_GetOpt_NvpUnknown( &goi, nvp_assert, 1 );
3649 return e;
3650 }
3651 // the halt or not param
3652 e = Jim_GetOpt_Wide( &goi, &a);
3653 if( e != JIM_OK ){
3654 return e;
3655 }
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);
3660
3661 // do the assert
3662 if( n->value == NVP_ASSERT ){
3663 target->type->assert_reset( target );
3664 } else {
3665 target->type->deassert_reset( target );
3666 }
3667 return JIM_OK;
3668 case TS_CMD_HALT:
3669 if( goi.argc ){
3670 Jim_WrongNumArgs( goi.interp, 0, argv, "halt [no parameters]");
3671 return JIM_ERR;
3672 }
3673 target->type->halt( target );
3674 return JIM_OK;
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 );
3679 return JIM_ERR;
3680 }
3681 e = Jim_GetOpt_Nvp( &goi, nvp_target_state, &n );
3682 if( e != JIM_OK ){
3683 Jim_GetOpt_NvpUnknown( &goi, nvp_target_state,1 );
3684 return e;
3685 }
3686 e = Jim_GetOpt_Wide( &goi, &a );
3687 if( e != JIM_OK ){
3688 return e;
3689 }
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",
3694 target->cmd_name,
3695 n->name,
3696 e, target_strerror_safe(e) );
3697 return JIM_ERR;
3698 } else {
3699 return JIM_OK;
3700 }
3701 case TS_CMD_EVENTLIST:
3702 /* List for human, Events defined for this target.
3703 * scripts/programs should use 'name cget -event NAME'
3704 */
3705 {
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,
3710 target->cmd_name );
3711 command_print( cmd_ctx, "%-25s | Body", "Event");
3712 command_print( cmd_ctx, "------------------------- | ----------------------------------------");
3713 while( teap ){
3714 command_print( cmd_ctx,
3715 "%-25s | %s",
3716 Jim_Nvp_value2name_simple( nvp_target_event, teap->event )->name,
3717 Jim_GetString( teap->body, NULL ) );
3718 teap = teap->next;
3719 }
3720 command_print( cmd_ctx, "***END***");
3721 return JIM_OK;
3722 }
3723 case TS_CMD_CURSTATE:
3724 if( goi.argc != 0 ){
3725 Jim_WrongNumArgs( goi.interp, 0, argv, "[no parameters]");
3726 return JIM_ERR;
3727 }
3728 Jim_SetResultString( goi.interp,
3729 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name,-1);
3730 return JIM_OK;
3731 case TS_CMD_INVOKE_EVENT:
3732 if( goi.argc != 1 ){
3733 Jim_SetResult_sprintf( goi.interp, "%s ?EVENTNAME?",n->name);
3734 return JIM_ERR;
3735 }
3736 e = Jim_GetOpt_Nvp( &goi, nvp_target_event, &n );
3737 if( e != JIM_OK ){
3738 Jim_GetOpt_NvpUnknown( &goi, nvp_target_event, 1 );
3739 return e;
3740 }
3741 target_handle_event( target, n->value );
3742 return JIM_OK;
3743 }
3744 return JIM_ERR;
3745 }
3746
3747
3748 static int
3749 target_create( Jim_GetOptInfo *goi )
3750 {
3751
3752 Jim_Obj *new_cmd;
3753 Jim_Cmd *cmd;
3754 const char *cp;
3755 char *cp2;
3756 int e;
3757 int x;
3758 target_t *target;
3759 struct command_context_s *cmd_ctx;
3760
3761 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
3762 if( goi->argc < 3 ){
3763 Jim_WrongNumArgs( goi->interp, 1, goi->argv, "?name? ?type? ..options...");
3764 return JIM_ERR;
3765 }
3766
3767 /* COMMAND */
3768 Jim_GetOpt_Obj( goi, &new_cmd );
3769 /* does this command exist? */
3770 cmd = Jim_GetCommand( goi->interp, new_cmd, JIM_ERRMSG );
3771 if( cmd ){
3772 cp = Jim_GetString( new_cmd, NULL );
3773 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
3774 return JIM_ERR;
3775 }
3776
3777 /* TYPE */
3778 e = Jim_GetOpt_String( goi, &cp2, NULL );
3779 cp = cp2;
3780 /* now does target type exist */
3781 for( x = 0 ; target_types[x] ; x++ ){
3782 if( 0 == strcmp( cp, target_types[x]->name ) ){
3783 /* found */
3784 break;
3785 }
3786 }
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,
3794 ", ", NULL);
3795 } else {
3796 Jim_AppendStrings( goi->interp,
3797 Jim_GetResult(goi->interp),
3798 " or ",
3799 target_types[x]->name,NULL );
3800 }
3801 }
3802 return JIM_ERR;
3803 }
3804
3805
3806 /* Create it */
3807 target = calloc(1,sizeof(target_t));
3808 /* set target number */
3809 target->target_number = new_target_number();
3810
3811 /* allocate memory for each unique target type */
3812 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
3813
3814 memcpy( target->type, target_types[x], sizeof(target_type_t));
3815
3816 /* will be set by "-endian" */
3817 target->endianness = TARGET_ENDIAN_UNKNOWN;
3818
3819 target->working_area = 0x0;
3820 target->working_area_size = 0x0;
3821 target->working_areas = NULL;
3822 target->backup_working_area = 0;
3823
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;
3831
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;
3841
3842 target->dbgmsg = NULL;
3843 target->dbg_msg_enabled = 0;
3844
3845 target->endianness = TARGET_ENDIAN_UNKNOWN;
3846
3847 /* Do the rest as "configure" options */
3848 goi->isconfigure = 1;
3849 e = target_configure( goi, target);
3850 if( e != JIM_OK ){
3851 free( target->type );
3852 free( target );
3853 return e;
3854 }
3855
3856 if( target->endianness == TARGET_ENDIAN_UNKNOWN ){
3857 /* default endian to little if not specified */
3858 target->endianness = TARGET_LITTLE_ENDIAN;
3859 }
3860
3861 /* create the target specific commands */
3862 if( target->type->register_commands ){
3863 (*(target->type->register_commands))( cmd_ctx );
3864 }
3865 if( target->type->target_create ){
3866 (*(target->type->target_create))( target, goi->interp );
3867 }
3868
3869 /* append to end of list */
3870 {
3871 target_t **tpp;
3872 tpp = &(all_targets);
3873 while( *tpp ){
3874 tpp = &( (*tpp)->next );
3875 }
3876 *tpp = target;
3877 }
3878
3879 cp = Jim_GetString( new_cmd, NULL );
3880 target->cmd_name = strdup(cp);
3881
3882 /* now - create the new target name command */
3883 e = Jim_CreateCommand( goi->interp,
3884 /* name */
3885 cp,
3886 tcl_target_func, /* C function */
3887 target, /* private data */
3888 NULL ); /* no del proc */
3889
3890 (*(target->type->target_create))( target, goi->interp );
3891 return e;
3892 }
3893
3894 static int
3895 jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
3896 {
3897 int x,r,e;
3898 jim_wide w;
3899 struct command_context_s *cmd_ctx;
3900 const char *cp;
3901 target_t *target;
3902 Jim_GetOptInfo goi;
3903 enum tcmd {
3904 /* TG = target generic */
3905 TG_CMD_CREATE,
3906 TG_CMD_TYPES,
3907 TG_CMD_NAMES,
3908 TG_CMD_CURRENT,
3909 TG_CMD_NUMBER,
3910 TG_CMD_COUNT,
3911 };
3912 const char *target_cmds[] = {
3913 "create", "types", "names", "current", "number",
3914 "count",
3915 NULL // terminate
3916 };
3917
3918 LOG_DEBUG("Target command params:");
3919 LOG_DEBUG(Jim_Debug_ArgvString( interp, argc, argv) );
3920
3921 cmd_ctx = Jim_GetAssocData( interp, "context" );
3922
3923 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
3924
3925 if( goi.argc == 0 ){
3926 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
3927 return JIM_ERR;
3928 }
3929
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) ){
3935 break;
3936 }
3937 }
3938 if( target_types[x] ){
3939 /* YES IT IS OLD SYNTAX */
3940 Jim_Obj *new_argv[10];
3941 int new_argc;
3942
3943 /* target_old_syntax
3944 *
3945 * argv[0] typename (above)
3946 * argv[1] endian
3947 * argv[2] reset method, deprecated/ignored
3948 * argv[3] = old param
3949 * argv[4] = old param
3950 *
3951 * We will combine all "old params" into a single param.
3952 * Then later, split them again.
3953 */
3954 if( argc < 4 ){
3955 Jim_WrongNumArgs( interp, 1, argv, "[OLDSYNTAX] ?TYPE? ?ENDIAN? ?RESET? ?old-params?");
3956 return JIM_ERR;
3957 }
3958 /* the command */
3959 new_argv[0] = argv[0];
3960 new_argv[1] = Jim_NewStringObj( interp, "create", -1 );
3961 {
3962 char buf[ 30 ];
3963 sprintf( buf, "target%d", new_target_number() );
3964 new_argv[2] = Jim_NewStringObj( interp, buf , -1 );
3965 }
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];
3973 new_argc = 10;
3974 /*
3975 * new arg syntax:
3976 * argv[0] = command
3977 * argv[1] = create
3978 * argv[2] = cmdname
3979 * argv[3] = typename
3980 * argv[4] = **FIRST** "configure" option.
3981 *
3982 * Here, we make them:
3983 *
3984 * argv[4] = -endian
3985 * argv[5] = little
3986 * argv[6] = -position
3987 * argv[7] = NUMBER
3988 * argv[8] = -variant
3989 * argv[9] = "somestring"
3990 */
3991
3992 /* don't let these be released */
3993 for( x = 0 ; x < new_argc ; x++ ){
3994 Jim_IncrRefCount( new_argv[x]);
3995 }
3996 /* call our self */
3997 LOG_DEBUG("Target OLD SYNTAX - converted to new syntax");
3998
3999 r = jim_target( goi.interp, new_argc, new_argv );
4000
4001 /* release? these items */
4002 for( x = 0 ; x < new_argc ; x++ ){
4003 Jim_DecrRefCount( interp, new_argv[x] );
4004 }
4005 return r;
4006 }
4007
4008 //Jim_GetOpt_Debug( &goi );
4009 r = Jim_GetOpt_Enum( &goi, target_cmds, &x );
4010 if( r != JIM_OK ){
4011 return r;
4012 }
4013
4014 switch(x){
4015 default:
4016 Jim_Panic(goi.interp,"Why am I here?");
4017 return JIM_ERR;
4018 case TG_CMD_CURRENT:
4019 if( goi.argc != 0 ){
4020 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters");
4021 return JIM_ERR;
4022 }
4023 Jim_SetResultString( goi.interp, get_current_target( cmd_ctx )->cmd_name, -1 );
4024 return JIM_OK;
4025 case TG_CMD_TYPES:
4026 if( goi.argc != 0 ){
4027 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
4028 return JIM_ERR;
4029 }
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 ) );
4035 }
4036 return JIM_OK;
4037 case TG_CMD_NAMES:
4038 if( goi.argc != 0 ){
4039 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
4040 return JIM_ERR;
4041 }
4042 Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
4043 target = all_targets;
4044 while( target ){
4045 Jim_ListAppendElement( goi.interp,
4046 Jim_GetResult(goi.interp),
4047 Jim_NewStringObj( goi.interp, target->cmd_name, -1 ) );
4048 target = target->next;
4049 }
4050 return JIM_OK;
4051 case TG_CMD_CREATE:
4052 if( goi.argc < 3 ){
4053 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4054 return JIM_ERR;
4055 }
4056 return target_create( &goi );
4057 break;
4058 case TG_CMD_NUMBER:
4059 if( goi.argc != 1 ){
4060 Jim_SetResult_sprintf( goi.interp, "expected: target number ?NUMBER?");
4061 return JIM_ERR;
4062 }
4063 e = Jim_GetOpt_Wide( &goi, &w );
4064 if( e != JIM_OK ){
4065 return JIM_ERR;
4066 }
4067 {
4068 target_t *t;
4069 t = get_target_by_num(w);
4070 if( t == NULL ){
4071 Jim_SetResult_sprintf( goi.interp,"Target: number %d does not exist", (int)(w));
4072 return JIM_ERR;
4073 }
4074 Jim_SetResultString( goi.interp, t->cmd_name, -1 );
4075 return JIM_OK;
4076 }
4077 case TG_CMD_COUNT:
4078 if( goi.argc != 0 ){
4079 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "<no parameters>");
4080 return JIM_ERR;
4081 }
4082 Jim_SetResult( goi.interp,
4083 Jim_NewIntObj( goi.interp, max_target_number()));
4084 return JIM_OK;
4085 }
4086
4087 return JIM_ERR;
4088 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)