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

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)