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

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)