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

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)