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

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)