- add ability for openocd to communicate to gdb using pipes (stdin/stdout).
[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 int target_wait_state(target_t *target, enum target_state state, int ms)
1704 {
1705 int retval;
1706 struct timeval timeout, now;
1707 int once=1;
1708 gettimeofday(&timeout, NULL);
1709 timeval_add_time(&timeout, 0, ms * 1000);
1710
1711 for (;;)
1712 {
1713 if ((retval=target_poll(target))!=ERROR_OK)
1714 return retval;
1715 keep_alive();
1716 if (target->state == state)
1717 {
1718 break;
1719 }
1720 if (once)
1721 {
1722 once=0;
1723 LOG_DEBUG("waiting for target %s...",
1724 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1725 }
1726
1727 gettimeofday(&now, NULL);
1728 if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
1729 {
1730 LOG_ERROR("timed out while waiting for target %s",
1731 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1732 return ERROR_FAIL;
1733 }
1734 }
1735
1736 return ERROR_OK;
1737 }
1738
1739 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1740 {
1741 int retval;
1742 target_t *target = get_current_target(cmd_ctx);
1743
1744 LOG_DEBUG("-");
1745
1746 if ((retval = target_halt(target)) != ERROR_OK)
1747 {
1748 return retval;
1749 }
1750
1751 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1752 }
1753
1754 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1755 {
1756 target_t *target = get_current_target(cmd_ctx);
1757
1758 LOG_USER("requesting target halt and executing a soft reset");
1759
1760 target->type->soft_reset_halt(target);
1761
1762 return ERROR_OK;
1763 }
1764
1765 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1766 {
1767 const Jim_Nvp *n;
1768 enum target_reset_mode reset_mode = RESET_RUN;
1769
1770 if (argc >= 1)
1771 {
1772 n = Jim_Nvp_name2value_simple( nvp_reset_modes, args[0] );
1773 if( (n->name == NULL) || (n->value == RESET_UNKNOWN) ){
1774 return ERROR_COMMAND_SYNTAX_ERROR;
1775 }
1776 reset_mode = n->value;
1777 }
1778
1779 /* reset *all* targets */
1780 return target_process_reset(cmd_ctx, reset_mode);
1781 }
1782
1783
1784 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1785 {
1786 int retval;
1787 target_t *target = get_current_target(cmd_ctx);
1788
1789 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1790
1791 if (argc == 0)
1792 retval = target_resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1793 else if (argc == 1)
1794 retval = target_resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1795 else
1796 {
1797 retval = ERROR_COMMAND_SYNTAX_ERROR;
1798 }
1799
1800 return retval;
1801 }
1802
1803 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1804 {
1805 target_t *target = get_current_target(cmd_ctx);
1806
1807 LOG_DEBUG("-");
1808
1809 if (argc == 0)
1810 return target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1811
1812 if (argc == 1)
1813 return target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1814
1815 return ERROR_OK;
1816 }
1817
1818 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1819 {
1820 const int line_bytecnt = 32;
1821 int count = 1;
1822 int size = 4;
1823 u32 address = 0;
1824 int line_modulo;
1825 int i;
1826
1827 char output[128];
1828 int output_len;
1829
1830 int retval;
1831
1832 u8 *buffer;
1833 target_t *target = get_current_target(cmd_ctx);
1834
1835 if (argc < 1)
1836 return ERROR_OK;
1837
1838 if (argc == 2)
1839 count = strtoul(args[1], NULL, 0);
1840
1841 address = strtoul(args[0], NULL, 0);
1842
1843 switch (cmd[2])
1844 {
1845 case 'w':
1846 size = 4; line_modulo = line_bytecnt / 4;
1847 break;
1848 case 'h':
1849 size = 2; line_modulo = line_bytecnt / 2;
1850 break;
1851 case 'b':
1852 size = 1; line_modulo = line_bytecnt / 1;
1853 break;
1854 default:
1855 return ERROR_OK;
1856 }
1857
1858 buffer = calloc(count, size);
1859 retval = target->type->read_memory(target, address, size, count, buffer);
1860 if (retval == ERROR_OK)
1861 {
1862 output_len = 0;
1863
1864 for (i = 0; i < count; i++)
1865 {
1866 if (i%line_modulo == 0)
1867 output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1868
1869 switch (size)
1870 {
1871 case 4:
1872 output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
1873 break;
1874 case 2:
1875 output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
1876 break;
1877 case 1:
1878 output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
1879 break;
1880 }
1881
1882 if ((i%line_modulo == line_modulo-1) || (i == count - 1))
1883 {
1884 command_print(cmd_ctx, output);
1885 output_len = 0;
1886 }
1887 }
1888 }
1889
1890 free(buffer);
1891
1892 return retval;
1893 }
1894
1895 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1896 {
1897 u32 address = 0;
1898 u32 value = 0;
1899 int count = 1;
1900 int i;
1901 int wordsize;
1902 target_t *target = get_current_target(cmd_ctx);
1903 u8 value_buf[4];
1904
1905 if ((argc < 2) || (argc > 3))
1906 return ERROR_COMMAND_SYNTAX_ERROR;
1907
1908 address = strtoul(args[0], NULL, 0);
1909 value = strtoul(args[1], NULL, 0);
1910 if (argc == 3)
1911 count = strtoul(args[2], NULL, 0);
1912
1913 switch (cmd[2])
1914 {
1915 case 'w':
1916 wordsize = 4;
1917 target_buffer_set_u32(target, value_buf, value);
1918 break;
1919 case 'h':
1920 wordsize = 2;
1921 target_buffer_set_u16(target, value_buf, value);
1922 break;
1923 case 'b':
1924 wordsize = 1;
1925 value_buf[0] = value;
1926 break;
1927 default:
1928 return ERROR_COMMAND_SYNTAX_ERROR;
1929 }
1930 for (i=0; i<count; i++)
1931 {
1932 int retval;
1933 switch (wordsize)
1934 {
1935 case 4:
1936 retval = target->type->write_memory(target, address + i*wordsize, 4, 1, value_buf);
1937 break;
1938 case 2:
1939 retval = target->type->write_memory(target, address + i*wordsize, 2, 1, value_buf);
1940 break;
1941 case 1:
1942 retval = target->type->write_memory(target, address + i*wordsize, 1, 1, value_buf);
1943 break;
1944 default:
1945 return ERROR_OK;
1946 }
1947 keep_alive();
1948
1949 if (retval!=ERROR_OK)
1950 {
1951 return retval;
1952 }
1953 }
1954
1955 return ERROR_OK;
1956
1957 }
1958
1959 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1960 {
1961 u8 *buffer;
1962 u32 buf_cnt;
1963 u32 image_size;
1964 u32 min_address=0;
1965 u32 max_address=0xffffffff;
1966 int i;
1967 int retval, retvaltemp;
1968
1969 image_t image;
1970
1971 duration_t duration;
1972 char *duration_text;
1973
1974 target_t *target = get_current_target(cmd_ctx);
1975
1976 if ((argc < 1)||(argc > 5))
1977 {
1978 return ERROR_COMMAND_SYNTAX_ERROR;
1979 }
1980
1981 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
1982 if (argc >= 2)
1983 {
1984 image.base_address_set = 1;
1985 image.base_address = strtoul(args[1], NULL, 0);
1986 }
1987 else
1988 {
1989 image.base_address_set = 0;
1990 }
1991
1992
1993 image.start_address_set = 0;
1994
1995 if (argc>=4)
1996 {
1997 min_address=strtoul(args[3], NULL, 0);
1998 }
1999 if (argc>=5)
2000 {
2001 max_address=strtoul(args[4], NULL, 0)+min_address;
2002 }
2003
2004 if (min_address>max_address)
2005 {
2006 return ERROR_COMMAND_SYNTAX_ERROR;
2007 }
2008
2009 duration_start_measure(&duration);
2010
2011 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2012 {
2013 return ERROR_OK;
2014 }
2015
2016 image_size = 0x0;
2017 retval = ERROR_OK;
2018 for (i = 0; i < image.num_sections; i++)
2019 {
2020 buffer = malloc(image.sections[i].size);
2021 if (buffer == NULL)
2022 {
2023 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2024 break;
2025 }
2026
2027 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2028 {
2029 free(buffer);
2030 break;
2031 }
2032
2033 u32 offset=0;
2034 u32 length=buf_cnt;
2035
2036 /* DANGER!!! beware of unsigned comparision here!!! */
2037
2038 if ((image.sections[i].base_address+buf_cnt>=min_address)&&
2039 (image.sections[i].base_address<max_address))
2040 {
2041 if (image.sections[i].base_address<min_address)
2042 {
2043 /* clip addresses below */
2044 offset+=min_address-image.sections[i].base_address;
2045 length-=offset;
2046 }
2047
2048 if (image.sections[i].base_address+buf_cnt>max_address)
2049 {
2050 length-=(image.sections[i].base_address+buf_cnt)-max_address;
2051 }
2052
2053 if ((retval = target_write_buffer(target, image.sections[i].base_address+offset, length, buffer+offset)) != ERROR_OK)
2054 {
2055 free(buffer);
2056 break;
2057 }
2058 image_size += length;
2059 command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
2060 }
2061
2062 free(buffer);
2063 }
2064
2065 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2066 {
2067 image_close(&image);
2068 return retvaltemp;
2069 }
2070
2071 if (retval==ERROR_OK)
2072 {
2073 command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
2074 }
2075 free(duration_text);
2076
2077 image_close(&image);
2078
2079 return retval;
2080
2081 }
2082
2083 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2084 {
2085 fileio_t fileio;
2086
2087 u32 address;
2088 u32 size;
2089 u8 buffer[560];
2090 int retval=ERROR_OK, retvaltemp;
2091
2092 duration_t duration;
2093 char *duration_text;
2094
2095 target_t *target = get_current_target(cmd_ctx);
2096
2097 if (argc != 3)
2098 {
2099 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2100 return ERROR_OK;
2101 }
2102
2103 address = strtoul(args[1], NULL, 0);
2104 size = strtoul(args[2], NULL, 0);
2105
2106 if ((address & 3) || (size & 3))
2107 {
2108 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
2109 return ERROR_OK;
2110 }
2111
2112 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2113 {
2114 return ERROR_OK;
2115 }
2116
2117 duration_start_measure(&duration);
2118
2119 while (size > 0)
2120 {
2121 u32 size_written;
2122 u32 this_run_size = (size > 560) ? 560 : size;
2123
2124 retval = target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
2125 if (retval != ERROR_OK)
2126 {
2127 break;
2128 }
2129
2130 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2131 if (retval != ERROR_OK)
2132 {
2133 break;
2134 }
2135
2136 size -= this_run_size;
2137 address += this_run_size;
2138 }
2139
2140 if((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2141 return retvaltemp;
2142
2143 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2144 return retvaltemp;
2145
2146 if (retval==ERROR_OK)
2147 {
2148 command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
2149 }
2150 free(duration_text);
2151
2152 return ERROR_OK;
2153 }
2154
2155 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2156 {
2157 u8 *buffer;
2158 u32 buf_cnt;
2159 u32 image_size;
2160 int i;
2161 int retval, retvaltemp;
2162 u32 checksum = 0;
2163 u32 mem_checksum = 0;
2164
2165 image_t image;
2166
2167 duration_t duration;
2168 char *duration_text;
2169
2170 target_t *target = get_current_target(cmd_ctx);
2171
2172 if (argc < 1)
2173 {
2174 return ERROR_COMMAND_SYNTAX_ERROR;
2175 }
2176
2177 if (!target)
2178 {
2179 LOG_ERROR("no target selected");
2180 return ERROR_FAIL;
2181 }
2182
2183 duration_start_measure(&duration);
2184
2185 if (argc >= 2)
2186 {
2187 image.base_address_set = 1;
2188 image.base_address = strtoul(args[1], NULL, 0);
2189 }
2190 else
2191 {
2192 image.base_address_set = 0;
2193 image.base_address = 0x0;
2194 }
2195
2196 image.start_address_set = 0;
2197
2198 if ((retval=image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2199 {
2200 return retval;
2201 }
2202
2203 image_size = 0x0;
2204 retval=ERROR_OK;
2205 for (i = 0; i < image.num_sections; i++)
2206 {
2207 buffer = malloc(image.sections[i].size);
2208 if (buffer == NULL)
2209 {
2210 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2211 break;
2212 }
2213 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2214 {
2215 free(buffer);
2216 break;
2217 }
2218
2219 /* calculate checksum of image */
2220 image_calculate_checksum( buffer, buf_cnt, &checksum );
2221
2222 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2223 if( retval != ERROR_OK )
2224 {
2225 free(buffer);
2226 break;
2227 }
2228
2229 if( checksum != mem_checksum )
2230 {
2231 /* failed crc checksum, fall back to a binary compare */
2232 u8 *data;
2233
2234 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2235
2236 data = (u8*)malloc(buf_cnt);
2237
2238 /* Can we use 32bit word accesses? */
2239 int size = 1;
2240 int count = buf_cnt;
2241 if ((count % 4) == 0)
2242 {
2243 size *= 4;
2244 count /= 4;
2245 }
2246 retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
2247 if (retval == ERROR_OK)
2248 {
2249 int t;
2250 for (t = 0; t < buf_cnt; t++)
2251 {
2252 if (data[t] != buffer[t])
2253 {
2254 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]);
2255 free(data);
2256 free(buffer);
2257 retval=ERROR_FAIL;
2258 goto done;
2259 }
2260 if ((t%16384)==0)
2261 {
2262 keep_alive();
2263 }
2264 }
2265 }
2266
2267 free(data);
2268 }
2269
2270 free(buffer);
2271 image_size += buf_cnt;
2272 }
2273 done:
2274
2275 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2276 {
2277 image_close(&image);
2278 return retvaltemp;
2279 }
2280
2281 if (retval==ERROR_OK)
2282 {
2283 command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
2284 }
2285 free(duration_text);
2286
2287 image_close(&image);
2288
2289 return retval;
2290 }
2291
2292 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2293 {
2294 int retval;
2295 target_t *target = get_current_target(cmd_ctx);
2296
2297 if (argc == 0)
2298 {
2299 breakpoint_t *breakpoint = target->breakpoints;
2300
2301 while (breakpoint)
2302 {
2303 if (breakpoint->type == BKPT_SOFT)
2304 {
2305 char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
2306 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
2307 free(buf);
2308 }
2309 else
2310 {
2311 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
2312 }
2313 breakpoint = breakpoint->next;
2314 }
2315 }
2316 else if (argc >= 2)
2317 {
2318 int hw = BKPT_SOFT;
2319 u32 length = 0;
2320
2321 length = strtoul(args[1], NULL, 0);
2322
2323 if (argc >= 3)
2324 if (strcmp(args[2], "hw") == 0)
2325 hw = BKPT_HARD;
2326
2327 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
2328 {
2329 LOG_ERROR("Failure setting breakpoints");
2330 }
2331 else
2332 {
2333 command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
2334 }
2335 }
2336 else
2337 {
2338 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2339 }
2340
2341 return ERROR_OK;
2342 }
2343
2344 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2345 {
2346 target_t *target = get_current_target(cmd_ctx);
2347
2348 if (argc > 0)
2349 breakpoint_remove(target, strtoul(args[0], NULL, 0));
2350
2351 return ERROR_OK;
2352 }
2353
2354 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2355 {
2356 target_t *target = get_current_target(cmd_ctx);
2357 int retval;
2358
2359 if (argc == 0)
2360 {
2361 watchpoint_t *watchpoint = target->watchpoints;
2362
2363 while (watchpoint)
2364 {
2365 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);
2366 watchpoint = watchpoint->next;
2367 }
2368 }
2369 else if (argc >= 2)
2370 {
2371 enum watchpoint_rw type = WPT_ACCESS;
2372 u32 data_value = 0x0;
2373 u32 data_mask = 0xffffffff;
2374
2375 if (argc >= 3)
2376 {
2377 switch(args[2][0])
2378 {
2379 case 'r':
2380 type = WPT_READ;
2381 break;
2382 case 'w':
2383 type = WPT_WRITE;
2384 break;
2385 case 'a':
2386 type = WPT_ACCESS;
2387 break;
2388 default:
2389 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2390 return ERROR_OK;
2391 }
2392 }
2393 if (argc >= 4)
2394 {
2395 data_value = strtoul(args[3], NULL, 0);
2396 }
2397 if (argc >= 5)
2398 {
2399 data_mask = strtoul(args[4], NULL, 0);
2400 }
2401
2402 if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
2403 strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
2404 {
2405 LOG_ERROR("Failure setting breakpoints");
2406 }
2407 }
2408 else
2409 {
2410 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2411 }
2412
2413 return ERROR_OK;
2414 }
2415
2416 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2417 {
2418 target_t *target = get_current_target(cmd_ctx);
2419
2420 if (argc > 0)
2421 watchpoint_remove(target, strtoul(args[0], NULL, 0));
2422
2423 return ERROR_OK;
2424 }
2425
2426 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
2427 {
2428 int retval;
2429 target_t *target = get_current_target(cmd_ctx);
2430 u32 va;
2431 u32 pa;
2432
2433 if (argc != 1)
2434 {
2435 return ERROR_COMMAND_SYNTAX_ERROR;
2436 }
2437 va = strtoul(args[0], NULL, 0);
2438
2439 retval = target->type->virt2phys(target, va, &pa);
2440 if (retval == ERROR_OK)
2441 {
2442 command_print(cmd_ctx, "Physical address 0x%08x", pa);
2443 }
2444 else
2445 {
2446 /* lower levels will have logged a detailed error which is
2447 * forwarded to telnet/GDB session.
2448 */
2449 }
2450 return retval;
2451 }
2452
2453 static void writeLong(FILE *f, int l)
2454 {
2455 int i;
2456 for (i=0; i<4; i++)
2457 {
2458 char c=(l>>(i*8))&0xff;
2459 fwrite(&c, 1, 1, f);
2460 }
2461
2462 }
2463
2464 static void writeString(FILE *f, char *s)
2465 {
2466 fwrite(s, 1, strlen(s), f);
2467 }
2468
2469 /* Dump a gmon.out histogram file. */
2470 static void writeGmon(u32 *samples, int sampleNum, char *filename)
2471 {
2472 int i;
2473 FILE *f=fopen(filename, "w");
2474 if (f==NULL)
2475 return;
2476 fwrite("gmon", 1, 4, f);
2477 writeLong(f, 0x00000001); /* Version */
2478 writeLong(f, 0); /* padding */
2479 writeLong(f, 0); /* padding */
2480 writeLong(f, 0); /* padding */
2481
2482 fwrite("", 1, 1, f); /* GMON_TAG_TIME_HIST */
2483
2484 /* figure out bucket size */
2485 u32 min=samples[0];
2486 u32 max=samples[0];
2487 for (i=0; i<sampleNum; i++)
2488 {
2489 if (min>samples[i])
2490 {
2491 min=samples[i];
2492 }
2493 if (max<samples[i])
2494 {
2495 max=samples[i];
2496 }
2497 }
2498
2499 int addressSpace=(max-min+1);
2500
2501 static int const maxBuckets=256*1024; /* maximum buckets. */
2502 int length=addressSpace;
2503 if (length > maxBuckets)
2504 {
2505 length=maxBuckets;
2506 }
2507 int *buckets=malloc(sizeof(int)*length);
2508 if (buckets==NULL)
2509 {
2510 fclose(f);
2511 return;
2512 }
2513 memset(buckets, 0, sizeof(int)*length);
2514 for (i=0; i<sampleNum;i++)
2515 {
2516 u32 address=samples[i];
2517 long long a=address-min;
2518 long long b=length-1;
2519 long long c=addressSpace-1;
2520 int index=(a*b)/c; /* danger!!!! int32 overflows */
2521 buckets[index]++;
2522 }
2523
2524 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2525 writeLong(f, min); /* low_pc */
2526 writeLong(f, max); /* high_pc */
2527 writeLong(f, length); /* # of samples */
2528 writeLong(f, 64000000); /* 64MHz */
2529 writeString(f, "seconds");
2530 for (i=0; i<(15-strlen("seconds")); i++)
2531 {
2532 fwrite("", 1, 1, f); /* padding */
2533 }
2534 writeString(f, "s");
2535
2536 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2537
2538 char *data=malloc(2*length);
2539 if (data!=NULL)
2540 {
2541 for (i=0; i<length;i++)
2542 {
2543 int val;
2544 val=buckets[i];
2545 if (val>65535)
2546 {
2547 val=65535;
2548 }
2549 data[i*2]=val&0xff;
2550 data[i*2+1]=(val>>8)&0xff;
2551 }
2552 free(buckets);
2553 fwrite(data, 1, length*2, f);
2554 free(data);
2555 } else
2556 {
2557 free(buckets);
2558 }
2559
2560 fclose(f);
2561 }
2562
2563 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2564 int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2565 {
2566 target_t *target = get_current_target(cmd_ctx);
2567 struct timeval timeout, now;
2568
2569 gettimeofday(&timeout, NULL);
2570 if (argc!=2)
2571 {
2572 return ERROR_COMMAND_SYNTAX_ERROR;
2573 }
2574 char *end;
2575 timeval_add_time(&timeout, strtoul(args[0], &end, 0), 0);
2576 if (*end)
2577 {
2578 return ERROR_OK;
2579 }
2580
2581 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
2582
2583 static const int maxSample=10000;
2584 u32 *samples=malloc(sizeof(u32)*maxSample);
2585 if (samples==NULL)
2586 return ERROR_OK;
2587
2588 int numSamples=0;
2589 int retval=ERROR_OK;
2590 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2591 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
2592
2593 for (;;)
2594 {
2595 target_poll(target);
2596 if (target->state == TARGET_HALTED)
2597 {
2598 u32 t=*((u32 *)reg->value);
2599 samples[numSamples++]=t;
2600 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2601 target_poll(target);
2602 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2603 } else if (target->state == TARGET_RUNNING)
2604 {
2605 /* We want to quickly sample the PC. */
2606 if((retval = target_halt(target)) != ERROR_OK)
2607 {
2608 free(samples);
2609 return retval;
2610 }
2611 } else
2612 {
2613 command_print(cmd_ctx, "Target not halted or running");
2614 retval=ERROR_OK;
2615 break;
2616 }
2617 if (retval!=ERROR_OK)
2618 {
2619 break;
2620 }
2621
2622 gettimeofday(&now, NULL);
2623 if ((numSamples>=maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
2624 {
2625 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2626 if((retval = target_poll(target)) != ERROR_OK)
2627 {
2628 free(samples);
2629 return retval;
2630 }
2631 if (target->state == TARGET_HALTED)
2632 {
2633 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2634 }
2635 if((retval = target_poll(target)) != ERROR_OK)
2636 {
2637 free(samples);
2638 return retval;
2639 }
2640 writeGmon(samples, numSamples, args[1]);
2641 command_print(cmd_ctx, "Wrote %s", args[1]);
2642 break;
2643 }
2644 }
2645 free(samples);
2646
2647 return ERROR_OK;
2648 }
2649
2650 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 val)
2651 {
2652 char *namebuf;
2653 Jim_Obj *nameObjPtr, *valObjPtr;
2654 int result;
2655
2656 namebuf = alloc_printf("%s(%d)", varname, idx);
2657 if (!namebuf)
2658 return JIM_ERR;
2659
2660 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2661 valObjPtr = Jim_NewIntObj(interp, val);
2662 if (!nameObjPtr || !valObjPtr)
2663 {
2664 free(namebuf);
2665 return JIM_ERR;
2666 }
2667
2668 Jim_IncrRefCount(nameObjPtr);
2669 Jim_IncrRefCount(valObjPtr);
2670 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
2671 Jim_DecrRefCount(interp, nameObjPtr);
2672 Jim_DecrRefCount(interp, valObjPtr);
2673 free(namebuf);
2674 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
2675 return result;
2676 }
2677
2678 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2679 {
2680 command_context_t *context;
2681 target_t *target;
2682
2683 context = Jim_GetAssocData(interp, "context");
2684 if (context == NULL)
2685 {
2686 LOG_ERROR("mem2array: no command context");
2687 return JIM_ERR;
2688 }
2689 target = get_current_target(context);
2690 if (target == NULL)
2691 {
2692 LOG_ERROR("mem2array: no current target");
2693 return JIM_ERR;
2694 }
2695
2696 return target_mem2array(interp, target, argc,argv);
2697 }
2698
2699 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
2700 {
2701 long l;
2702 u32 width;
2703 int len;
2704 u32 addr;
2705 u32 count;
2706 u32 v;
2707 const char *varname;
2708 u8 buffer[4096];
2709 int i, n, e, retval;
2710
2711 /* argv[1] = name of array to receive the data
2712 * argv[2] = desired width
2713 * argv[3] = memory address
2714 * argv[4] = count of times to read
2715 */
2716 if (argc != 5) {
2717 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2718 return JIM_ERR;
2719 }
2720 varname = Jim_GetString(argv[1], &len);
2721 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2722
2723 e = Jim_GetLong(interp, argv[2], &l);
2724 width = l;
2725 if (e != JIM_OK) {
2726 return e;
2727 }
2728
2729 e = Jim_GetLong(interp, argv[3], &l);
2730 addr = l;
2731 if (e != JIM_OK) {
2732 return e;
2733 }
2734 e = Jim_GetLong(interp, argv[4], &l);
2735 len = l;
2736 if (e != JIM_OK) {
2737 return e;
2738 }
2739 switch (width) {
2740 case 8:
2741 width = 1;
2742 break;
2743 case 16:
2744 width = 2;
2745 break;
2746 case 32:
2747 width = 4;
2748 break;
2749 default:
2750 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2751 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2752 return JIM_ERR;
2753 }
2754 if (len == 0) {
2755 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2756 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
2757 return JIM_ERR;
2758 }
2759 if ((addr + (len * width)) < addr) {
2760 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2761 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
2762 return JIM_ERR;
2763 }
2764 /* absurd transfer size? */
2765 if (len > 65536) {
2766 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2767 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
2768 return JIM_ERR;
2769 }
2770
2771 if ((width == 1) ||
2772 ((width == 2) && ((addr & 1) == 0)) ||
2773 ((width == 4) && ((addr & 3) == 0))) {
2774 /* all is well */
2775 } else {
2776 char buf[100];
2777 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2778 sprintf(buf, "mem2array address: 0x%08x is not aligned for %d byte reads", addr, width);
2779 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2780 return JIM_ERR;
2781 }
2782
2783 /* Transfer loop */
2784
2785 /* index counter */
2786 n = 0;
2787 /* assume ok */
2788 e = JIM_OK;
2789 while (len) {
2790 /* Slurp... in buffer size chunks */
2791
2792 count = len; /* in objects.. */
2793 if (count > (sizeof(buffer)/width)) {
2794 count = (sizeof(buffer)/width);
2795 }
2796
2797 retval = target->type->read_memory( target, addr, width, count, buffer );
2798 if (retval != ERROR_OK) {
2799 /* BOO !*/
2800 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
2801 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2802 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
2803 e = JIM_ERR;
2804 len = 0;
2805 } else {
2806 v = 0; /* shut up gcc */
2807 for (i = 0 ;i < count ;i++, n++) {
2808 switch (width) {
2809 case 4:
2810 v = target_buffer_get_u32(target, &buffer[i*width]);
2811 break;
2812 case 2:
2813 v = target_buffer_get_u16(target, &buffer[i*width]);
2814 break;
2815 case 1:
2816 v = buffer[i] & 0x0ff;
2817 break;
2818 }
2819 new_int_array_element(interp, varname, n, v);
2820 }
2821 len -= count;
2822 }
2823 }
2824
2825 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2826
2827 return JIM_OK;
2828 }
2829
2830 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 *val)
2831 {
2832 char *namebuf;
2833 Jim_Obj *nameObjPtr, *valObjPtr;
2834 int result;
2835 long l;
2836
2837 namebuf = alloc_printf("%s(%d)", varname, idx);
2838 if (!namebuf)
2839 return JIM_ERR;
2840
2841 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2842 if (!nameObjPtr)
2843 {
2844 free(namebuf);
2845 return JIM_ERR;
2846 }
2847
2848 Jim_IncrRefCount(nameObjPtr);
2849 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
2850 Jim_DecrRefCount(interp, nameObjPtr);
2851 free(namebuf);
2852 if (valObjPtr == NULL)
2853 return JIM_ERR;
2854
2855 result = Jim_GetLong(interp, valObjPtr, &l);
2856 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
2857 *val = l;
2858 return result;
2859 }
2860
2861 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2862 {
2863 command_context_t *context;
2864 target_t *target;
2865
2866 context = Jim_GetAssocData(interp, "context");
2867 if (context == NULL){
2868 LOG_ERROR("array2mem: no command context");
2869 return JIM_ERR;
2870 }
2871 target = get_current_target(context);
2872 if (target == NULL){
2873 LOG_ERROR("array2mem: no current target");
2874 return JIM_ERR;
2875 }
2876
2877 return target_array2mem( interp,target, argc, argv );
2878 }
2879
2880 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
2881 {
2882 long l;
2883 u32 width;
2884 int len;
2885 u32 addr;
2886 u32 count;
2887 u32 v;
2888 const char *varname;
2889 u8 buffer[4096];
2890 int i, n, e, retval;
2891
2892 /* argv[1] = name of array to get the data
2893 * argv[2] = desired width
2894 * argv[3] = memory address
2895 * argv[4] = count to write
2896 */
2897 if (argc != 5) {
2898 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2899 return JIM_ERR;
2900 }
2901 varname = Jim_GetString(argv[1], &len);
2902 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2903
2904 e = Jim_GetLong(interp, argv[2], &l);
2905 width = l;
2906 if (e != JIM_OK) {
2907 return e;
2908 }
2909
2910 e = Jim_GetLong(interp, argv[3], &l);
2911 addr = l;
2912 if (e != JIM_OK) {
2913 return e;
2914 }
2915 e = Jim_GetLong(interp, argv[4], &l);
2916 len = l;
2917 if (e != JIM_OK) {
2918 return e;
2919 }
2920 switch (width) {
2921 case 8:
2922 width = 1;
2923 break;
2924 case 16:
2925 width = 2;
2926 break;
2927 case 32:
2928 width = 4;
2929 break;
2930 default:
2931 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2932 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2933 return JIM_ERR;
2934 }
2935 if (len == 0) {
2936 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2937 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
2938 return JIM_ERR;
2939 }
2940 if ((addr + (len * width)) < addr) {
2941 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2942 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
2943 return JIM_ERR;
2944 }
2945 /* absurd transfer size? */
2946 if (len > 65536) {
2947 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2948 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
2949 return JIM_ERR;
2950 }
2951
2952 if ((width == 1) ||
2953 ((width == 2) && ((addr & 1) == 0)) ||
2954 ((width == 4) && ((addr & 3) == 0))) {
2955 /* all is well */
2956 } else {
2957 char buf[100];
2958 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2959 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads", addr, width);
2960 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2961 return JIM_ERR;
2962 }
2963
2964 /* Transfer loop */
2965
2966 /* index counter */
2967 n = 0;
2968 /* assume ok */
2969 e = JIM_OK;
2970 while (len) {
2971 /* Slurp... in buffer size chunks */
2972
2973 count = len; /* in objects.. */
2974 if (count > (sizeof(buffer)/width)) {
2975 count = (sizeof(buffer)/width);
2976 }
2977
2978 v = 0; /* shut up gcc */
2979 for (i = 0 ;i < count ;i++, n++) {
2980 get_int_array_element(interp, varname, n, &v);
2981 switch (width) {
2982 case 4:
2983 target_buffer_set_u32(target, &buffer[i*width], v);
2984 break;
2985 case 2:
2986 target_buffer_set_u16(target, &buffer[i*width], v);
2987 break;
2988 case 1:
2989 buffer[i] = v & 0x0ff;
2990 break;
2991 }
2992 }
2993 len -= count;
2994
2995 retval = target->type->write_memory(target, addr, width, count, buffer);
2996 if (retval != ERROR_OK) {
2997 /* BOO !*/
2998 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
2999 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3000 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3001 e = JIM_ERR;
3002 len = 0;
3003 }
3004 }
3005
3006 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3007
3008 return JIM_OK;
3009 }
3010
3011 void target_all_handle_event( enum target_event e )
3012 {
3013 target_t *target;
3014
3015 LOG_DEBUG( "**all*targets: event: %d, %s",
3016 e,
3017 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3018
3019 target = all_targets;
3020 while (target){
3021 target_handle_event( target, e );
3022 target = target->next;
3023 }
3024 }
3025
3026 void target_handle_event( target_t *target, enum target_event e )
3027 {
3028 target_event_action_t *teap;
3029 int done;
3030
3031 teap = target->event_action;
3032
3033 done = 0;
3034 while( teap ){
3035 if( teap->event == e ){
3036 done = 1;
3037 LOG_DEBUG( "target: (%d) %s (%s) event: %d (%s) action: %s\n",
3038 target->target_number,
3039 target->cmd_name,
3040 target->type->name,
3041 e,
3042 Jim_Nvp_value2name_simple( nvp_target_event, e )->name,
3043 Jim_GetString( teap->body, NULL ) );
3044 if (Jim_EvalObj( interp, teap->body )!=JIM_OK)
3045 {
3046 Jim_PrintErrorMessage(interp);
3047 }
3048 }
3049 teap = teap->next;
3050 }
3051 if( !done ){
3052 LOG_DEBUG( "event: %d %s - no action",
3053 e,
3054 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3055 }
3056 }
3057
3058 enum target_cfg_param {
3059 TCFG_TYPE,
3060 TCFG_EVENT,
3061 TCFG_WORK_AREA_VIRT,
3062 TCFG_WORK_AREA_PHYS,
3063 TCFG_WORK_AREA_SIZE,
3064 TCFG_WORK_AREA_BACKUP,
3065 TCFG_ENDIAN,
3066 TCFG_VARIANT,
3067 TCFG_CHAIN_POSITION,
3068 };
3069
3070 static Jim_Nvp nvp_config_opts[] = {
3071 { .name = "-type", .value = TCFG_TYPE },
3072 { .name = "-event", .value = TCFG_EVENT },
3073 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3074 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3075 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3076 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3077 { .name = "-endian" , .value = TCFG_ENDIAN },
3078 { .name = "-variant", .value = TCFG_VARIANT },
3079 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3080
3081 { .name = NULL, .value = -1 }
3082 };
3083
3084 static int target_configure( Jim_GetOptInfo *goi, target_t *target )
3085 {
3086 Jim_Nvp *n;
3087 Jim_Obj *o;
3088 jim_wide w;
3089 char *cp;
3090 int e;
3091
3092 /* parse config or cget options ... */
3093 while( goi->argc > 0 ){
3094 Jim_SetEmptyResult( goi->interp );
3095 /* Jim_GetOpt_Debug( goi ); */
3096
3097 if( target->type->target_jim_configure ){
3098 /* target defines a configure function */
3099 /* target gets first dibs on parameters */
3100 e = (*(target->type->target_jim_configure))( target, goi );
3101 if( e == JIM_OK ){
3102 /* more? */
3103 continue;
3104 }
3105 if( e == JIM_ERR ){
3106 /* An error */
3107 return e;
3108 }
3109 /* otherwise we 'continue' below */
3110 }
3111 e = Jim_GetOpt_Nvp( goi, nvp_config_opts, &n );
3112 if( e != JIM_OK ){
3113 Jim_GetOpt_NvpUnknown( goi, nvp_config_opts, 0 );
3114 return e;
3115 }
3116 switch( n->value ){
3117 case TCFG_TYPE:
3118 /* not setable */
3119 if( goi->isconfigure ){
3120 Jim_SetResult_sprintf( goi->interp, "not setable: %s", n->name );
3121 return JIM_ERR;
3122 } else {
3123 no_params:
3124 if( goi->argc != 0 ){
3125 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "NO PARAMS");
3126 return JIM_ERR;
3127 }
3128 }
3129 Jim_SetResultString( goi->interp, target->type->name, -1 );
3130 /* loop for more */
3131 break;
3132 case TCFG_EVENT:
3133 if( goi->argc == 0 ){
3134 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3135 return JIM_ERR;
3136 }
3137
3138 e = Jim_GetOpt_Nvp( goi, nvp_target_event, &n );
3139 if( e != JIM_OK ){
3140 Jim_GetOpt_NvpUnknown( goi, nvp_target_event, 1 );
3141 return e;
3142 }
3143
3144 if( goi->isconfigure ){
3145 if( goi->argc != 1 ){
3146 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3147 return JIM_ERR;
3148 }
3149 } else {
3150 if( goi->argc != 0 ){
3151 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3152 return JIM_ERR;
3153 }
3154 }
3155
3156 {
3157 target_event_action_t *teap;
3158
3159 teap = target->event_action;
3160 /* replace existing? */
3161 while( teap ){
3162 if( teap->event == n->value ){
3163 break;
3164 }
3165 teap = teap->next;
3166 }
3167
3168 if( goi->isconfigure ){
3169 if( teap == NULL ){
3170 /* create new */
3171 teap = calloc( 1, sizeof(*teap) );
3172 }
3173 teap->event = n->value;
3174 Jim_GetOpt_Obj( goi, &o );
3175 if( teap->body ){
3176 Jim_DecrRefCount( interp, teap->body );
3177 }
3178 teap->body = Jim_DuplicateObj( goi->interp, o );
3179 /*
3180 * FIXME:
3181 * Tcl/TK - "tk events" have a nice feature.
3182 * See the "BIND" command.
3183 * We should support that here.
3184 * You can specify %X and %Y in the event code.
3185 * The idea is: %T - target name.
3186 * The idea is: %N - target number
3187 * The idea is: %E - event name.
3188 */
3189 Jim_IncrRefCount( teap->body );
3190
3191 /* add to head of event list */
3192 teap->next = target->event_action;
3193 target->event_action = teap;
3194 Jim_SetEmptyResult(goi->interp);
3195 } else {
3196 /* get */
3197 if( teap == NULL ){
3198 Jim_SetEmptyResult( goi->interp );
3199 } else {
3200 Jim_SetResult( goi->interp, Jim_DuplicateObj( goi->interp, teap->body ) );
3201 }
3202 }
3203 }
3204 /* loop for more */
3205 break;
3206
3207 case TCFG_WORK_AREA_VIRT:
3208 if( goi->isconfigure ){
3209 target_free_all_working_areas(target);
3210 e = Jim_GetOpt_Wide( goi, &w );
3211 if( e != JIM_OK ){
3212 return e;
3213 }
3214 target->working_area_virt = w;
3215 } else {
3216 if( goi->argc != 0 ){
3217 goto no_params;
3218 }
3219 }
3220 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_virt ) );
3221 /* loop for more */
3222 break;
3223
3224 case TCFG_WORK_AREA_PHYS:
3225 if( goi->isconfigure ){
3226 target_free_all_working_areas(target);
3227 e = Jim_GetOpt_Wide( goi, &w );
3228 if( e != JIM_OK ){
3229 return e;
3230 }
3231 target->working_area_phys = w;
3232 } else {
3233 if( goi->argc != 0 ){
3234 goto no_params;
3235 }
3236 }
3237 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_phys ) );
3238 /* loop for more */
3239 break;
3240
3241 case TCFG_WORK_AREA_SIZE:
3242 if( goi->isconfigure ){
3243 target_free_all_working_areas(target);
3244 e = Jim_GetOpt_Wide( goi, &w );
3245 if( e != JIM_OK ){
3246 return e;
3247 }
3248 target->working_area_size = w;
3249 } else {
3250 if( goi->argc != 0 ){
3251 goto no_params;
3252 }
3253 }
3254 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
3255 /* loop for more */
3256 break;
3257
3258 case TCFG_WORK_AREA_BACKUP:
3259 if( goi->isconfigure ){
3260 target_free_all_working_areas(target);
3261 e = Jim_GetOpt_Wide( goi, &w );
3262 if( e != JIM_OK ){
3263 return e;
3264 }
3265 /* make this exactly 1 or 0 */
3266 target->backup_working_area = (!!w);
3267 } else {
3268 if( goi->argc != 0 ){
3269 goto no_params;
3270 }
3271 }
3272 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
3273 /* loop for more e*/
3274 break;
3275
3276 case TCFG_ENDIAN:
3277 if( goi->isconfigure ){
3278 e = Jim_GetOpt_Nvp( goi, nvp_target_endian, &n );
3279 if( e != JIM_OK ){
3280 Jim_GetOpt_NvpUnknown( goi, nvp_target_endian, 1 );
3281 return e;
3282 }
3283 target->endianness = n->value;
3284 } else {
3285 if( goi->argc != 0 ){
3286 goto no_params;
3287 }
3288 }
3289 n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3290 if( n->name == NULL ){
3291 target->endianness = TARGET_LITTLE_ENDIAN;
3292 n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3293 }
3294 Jim_SetResultString( goi->interp, n->name, -1 );
3295 /* loop for more */
3296 break;
3297
3298 case TCFG_VARIANT:
3299 if( goi->isconfigure ){
3300 if( goi->argc < 1 ){
3301 Jim_SetResult_sprintf( goi->interp,
3302 "%s ?STRING?",
3303 n->name );
3304 return JIM_ERR;
3305 }
3306 if( target->variant ){
3307 free((void *)(target->variant));
3308 }
3309 e = Jim_GetOpt_String( goi, &cp, NULL );
3310 target->variant = strdup(cp);
3311 } else {
3312 if( goi->argc != 0 ){
3313 goto no_params;
3314 }
3315 }
3316 Jim_SetResultString( goi->interp, target->variant,-1 );
3317 /* loop for more */
3318 break;
3319 case TCFG_CHAIN_POSITION:
3320 if( goi->isconfigure ){
3321 Jim_Obj *o;
3322 jtag_tap_t *tap;
3323 target_free_all_working_areas(target);
3324 e = Jim_GetOpt_Obj( goi, &o );
3325 if( e != JIM_OK ){
3326 return e;
3327 }
3328 tap = jtag_TapByJimObj( goi->interp, o );
3329 if( tap == NULL ){
3330 return JIM_ERR;
3331 }
3332 /* make this exactly 1 or 0 */
3333 target->tap = tap;
3334 } else {
3335 if( goi->argc != 0 ){
3336 goto no_params;
3337 }
3338 }
3339 Jim_SetResultString( interp, target->tap->dotted_name, -1 );
3340 /* loop for more e*/
3341 break;
3342 }
3343 } /* while( goi->argc ) */
3344 /* done - we return */
3345 return JIM_OK;
3346 }
3347
3348 /** this is the 'tcl' handler for the target specific command */
3349 static int tcl_target_func( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
3350 {
3351 Jim_GetOptInfo goi;
3352 jim_wide a,b,c;
3353 int x,y,z;
3354 u8 target_buf[32];
3355 Jim_Nvp *n;
3356 target_t *target;
3357 struct command_context_s *cmd_ctx;
3358 int e;
3359
3360 enum {
3361 TS_CMD_CONFIGURE,
3362 TS_CMD_CGET,
3363
3364 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3365 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3366 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3367 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3368 TS_CMD_EXAMINE,
3369 TS_CMD_POLL,
3370 TS_CMD_RESET,
3371 TS_CMD_HALT,
3372 TS_CMD_WAITSTATE,
3373 TS_CMD_EVENTLIST,
3374 TS_CMD_CURSTATE,
3375 TS_CMD_INVOKE_EVENT,
3376 };
3377
3378 static const Jim_Nvp target_options[] = {
3379 { .name = "configure", .value = TS_CMD_CONFIGURE },
3380 { .name = "cget", .value = TS_CMD_CGET },
3381 { .name = "mww", .value = TS_CMD_MWW },
3382 { .name = "mwh", .value = TS_CMD_MWH },
3383 { .name = "mwb", .value = TS_CMD_MWB },
3384 { .name = "mdw", .value = TS_CMD_MDW },
3385 { .name = "mdh", .value = TS_CMD_MDH },
3386 { .name = "mdb", .value = TS_CMD_MDB },
3387 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3388 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3389 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3390 { .name = "curstate", .value = TS_CMD_CURSTATE },
3391
3392 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3393 { .name = "arp_poll", .value = TS_CMD_POLL },
3394 { .name = "arp_reset", .value = TS_CMD_RESET },
3395 { .name = "arp_halt", .value = TS_CMD_HALT },
3396 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3397 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3398
3399 { .name = NULL, .value = -1 },
3400 };
3401
3402 /* go past the "command" */
3403 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
3404
3405 target = Jim_CmdPrivData( goi.interp );
3406 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3407
3408 /* commands here are in an NVP table */
3409 e = Jim_GetOpt_Nvp( &goi, target_options, &n );
3410 if( e != JIM_OK ){
3411 Jim_GetOpt_NvpUnknown( &goi, target_options, 0 );
3412 return e;
3413 }
3414 /* Assume blank result */
3415 Jim_SetEmptyResult( goi.interp );
3416
3417 switch( n->value ){
3418 case TS_CMD_CONFIGURE:
3419 if( goi.argc < 2 ){
3420 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3421 return JIM_ERR;
3422 }
3423 goi.isconfigure = 1;
3424 return target_configure( &goi, target );
3425 case TS_CMD_CGET:
3426 // some things take params
3427 if( goi.argc < 1 ){
3428 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "missing: ?-option?");
3429 return JIM_ERR;
3430 }
3431 goi.isconfigure = 0;
3432 return target_configure( &goi, target );
3433 break;
3434 case TS_CMD_MWW:
3435 case TS_CMD_MWH:
3436 case TS_CMD_MWB:
3437 /* argv[0] = cmd
3438 * argv[1] = address
3439 * argv[2] = data
3440 * argv[3] = optional count.
3441 */
3442
3443 if( (goi.argc == 3) || (goi.argc == 4) ){
3444 /* all is well */
3445 } else {
3446 mwx_error:
3447 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR DATA [COUNT]", n->name );
3448 return JIM_ERR;
3449 }
3450
3451 e = Jim_GetOpt_Wide( &goi, &a );
3452 if( e != JIM_OK ){
3453 goto mwx_error;
3454 }
3455
3456 e = Jim_GetOpt_Wide( &goi, &b );
3457 if( e != JIM_OK ){
3458 goto mwx_error;
3459 }
3460 if( goi.argc ){
3461 e = Jim_GetOpt_Wide( &goi, &c );
3462 if( e != JIM_OK ){
3463 goto mwx_error;
3464 }
3465 } else {
3466 c = 1;
3467 }
3468
3469 switch( n->value ){
3470 case TS_CMD_MWW:
3471 target_buffer_set_u32( target, target_buf, b );
3472 b = 4;
3473 break;
3474 case TS_CMD_MWH:
3475 target_buffer_set_u16( target, target_buf, b );
3476 b = 2;
3477 break;
3478 case TS_CMD_MWB:
3479 target_buffer_set_u8( target, target_buf, b );
3480 b = 1;
3481 break;
3482 }
3483 for( x = 0 ; x < c ; x++ ){
3484 e = target->type->write_memory( target, a, b, 1, target_buf );
3485 if( e != ERROR_OK ){
3486 Jim_SetResult_sprintf( interp, "Error writing @ 0x%08x: %d\n", (int)(a), e );
3487 return JIM_ERR;
3488 }
3489 /* b = width */
3490 a = a + b;
3491 }
3492 return JIM_OK;
3493 break;
3494
3495 /* display */
3496 case TS_CMD_MDW:
3497 case TS_CMD_MDH:
3498 case TS_CMD_MDB:
3499 /* argv[0] = command
3500 * argv[1] = address
3501 * argv[2] = optional count
3502 */
3503 if( (goi.argc == 2) || (goi.argc == 3) ){
3504 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR [COUNT]", n->name );
3505 return JIM_ERR;
3506 }
3507 e = Jim_GetOpt_Wide( &goi, &a );
3508 if( e != JIM_OK ){
3509 return JIM_ERR;
3510 }
3511 if( goi.argc ){
3512 e = Jim_GetOpt_Wide( &goi, &c );
3513 if( e != JIM_OK ){
3514 return JIM_ERR;
3515 }
3516 } else {
3517 c = 1;
3518 }
3519 b = 1; /* shut up gcc */
3520 switch( n->value ){
3521 case TS_CMD_MDW:
3522 b = 4;
3523 break;
3524 case TS_CMD_MDH:
3525 b = 2;
3526 break;
3527 case TS_CMD_MDB:
3528 b = 1;
3529 break;
3530 }
3531
3532 /* convert to "bytes" */
3533 c = c * b;
3534 /* count is now in 'BYTES' */
3535 while( c > 0 ){
3536 y = c;
3537 if( y > 16 ){
3538 y = 16;
3539 }
3540 e = target->type->read_memory( target, a, b, y / b, target_buf );
3541 if( e != ERROR_OK ){
3542 Jim_SetResult_sprintf( interp, "error reading target @ 0x%08lx", (int)(a) );
3543 return JIM_ERR;
3544 }
3545
3546 Jim_fprintf( interp, interp->cookie_stdout, "0x%08x ", (int)(a) );
3547 switch( b ){
3548 case 4:
3549 for( x = 0 ; (x < 16) && (x < y) ; x += 4 ){
3550 z = target_buffer_get_u32( target, &(target_buf[ x * 4 ]) );
3551 Jim_fprintf( interp, interp->cookie_stdout, "%08x ", (int)(z) );
3552 }
3553 for( ; (x < 16) ; x += 4 ){
3554 Jim_fprintf( interp, interp->cookie_stdout, " " );
3555 }
3556 break;
3557 case 2:
3558 for( x = 0 ; (x < 16) && (x < y) ; x += 2 ){
3559 z = target_buffer_get_u16( target, &(target_buf[ x * 2 ]) );
3560 Jim_fprintf( interp, interp->cookie_stdout, "%04x ", (int)(z) );
3561 }
3562 for( ; (x < 16) ; x += 2 ){
3563 Jim_fprintf( interp, interp->cookie_stdout, " " );
3564 }
3565 break;
3566 case 1:
3567 default:
3568 for( x = 0 ; (x < 16) && (x < y) ; x += 1 ){
3569 z = target_buffer_get_u8( target, &(target_buf[ x * 4 ]) );
3570 Jim_fprintf( interp, interp->cookie_stdout, "%02x ", (int)(z) );
3571 }
3572 for( ; (x < 16) ; x += 1 ){
3573 Jim_fprintf( interp, interp->cookie_stdout, " " );
3574 }
3575 break;
3576 }
3577 /* ascii-ify the bytes */
3578 for( x = 0 ; x < y ; x++ ){
3579 if( (target_buf[x] >= 0x20) &&
3580 (target_buf[x] <= 0x7e) ){
3581 /* good */
3582 } else {
3583 /* smack it */
3584 target_buf[x] = '.';
3585 }
3586 }
3587 /* space pad */
3588 while( x < 16 ){
3589 target_buf[x] = ' ';
3590 x++;
3591 }
3592 /* terminate */
3593 target_buf[16] = 0;
3594 /* print - with a newline */
3595 Jim_fprintf( interp, interp->cookie_stdout, "%s\n", target_buf );
3596 /* NEXT... */
3597 c -= 16;
3598 a += 16;
3599 }
3600 return JIM_OK;
3601 case TS_CMD_MEM2ARRAY:
3602 return target_mem2array( goi.interp, target, goi.argc, goi.argv );
3603 break;
3604 case TS_CMD_ARRAY2MEM:
3605 return target_array2mem( goi.interp, target, goi.argc, goi.argv );
3606 break;
3607 case TS_CMD_EXAMINE:
3608 if( goi.argc ){
3609 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3610 return JIM_ERR;
3611 }
3612 e = target->type->examine( target );
3613 if( e != ERROR_OK ){
3614 Jim_SetResult_sprintf( interp, "examine-fails: %d", e );
3615 return JIM_ERR;
3616 }
3617 return JIM_OK;
3618 case TS_CMD_POLL:
3619 if( goi.argc ){
3620 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3621 return JIM_ERR;
3622 }
3623 if( !(target->type->examined) ){
3624 e = ERROR_TARGET_NOT_EXAMINED;
3625 } else {
3626 e = target->type->poll( target );
3627 }
3628 if( e != ERROR_OK ){
3629 Jim_SetResult_sprintf( interp, "poll-fails: %d", e );
3630 return JIM_ERR;
3631 } else {
3632 return JIM_OK;
3633 }
3634 break;
3635 case TS_CMD_RESET:
3636 if( goi.argc != 2 ){
3637 Jim_WrongNumArgs( interp, 2, argv, "t|f|assert|deassert BOOL");
3638 return JIM_ERR;
3639 }
3640 e = Jim_GetOpt_Nvp( &goi, nvp_assert, &n );
3641 if( e != JIM_OK ){
3642 Jim_GetOpt_NvpUnknown( &goi, nvp_assert, 1 );
3643 return e;
3644 }
3645 /* the halt or not param */
3646 e = Jim_GetOpt_Wide( &goi, &a);
3647 if( e != JIM_OK ){
3648 return e;
3649 }
3650 /* determine if we should halt or not. */
3651 target->reset_halt = !!a;
3652 /* When this happens - all workareas are invalid. */
3653 target_free_all_working_areas_restore(target, 0);
3654
3655 /* do the assert */
3656 if( n->value == NVP_ASSERT ){
3657 target->type->assert_reset( target );
3658 } else {
3659 target->type->deassert_reset( target );
3660 }
3661 return JIM_OK;
3662 case TS_CMD_HALT:
3663 if( goi.argc ){
3664 Jim_WrongNumArgs( goi.interp, 0, argv, "halt [no parameters]");
3665 return JIM_ERR;
3666 }
3667 target->type->halt( target );
3668 return JIM_OK;
3669 case TS_CMD_WAITSTATE:
3670 /* params: <name> statename timeoutmsecs */
3671 if( goi.argc != 2 ){
3672 Jim_SetResult_sprintf( goi.interp, "%s STATENAME TIMEOUTMSECS", n->name );
3673 return JIM_ERR;
3674 }
3675 e = Jim_GetOpt_Nvp( &goi, nvp_target_state, &n );
3676 if( e != JIM_OK ){
3677 Jim_GetOpt_NvpUnknown( &goi, nvp_target_state,1 );
3678 return e;
3679 }
3680 e = Jim_GetOpt_Wide( &goi, &a );
3681 if( e != JIM_OK ){
3682 return e;
3683 }
3684 e = target_wait_state( target, n->value, a );
3685 if( e != ERROR_OK ){
3686 Jim_SetResult_sprintf( goi.interp,
3687 "target: %s wait %s fails (%d) %s",
3688 target->cmd_name,
3689 n->name,
3690 e, target_strerror_safe(e) );
3691 return JIM_ERR;
3692 } else {
3693 return JIM_OK;
3694 }
3695 case TS_CMD_EVENTLIST:
3696 /* List for human, Events defined for this target.
3697 * scripts/programs should use 'name cget -event NAME'
3698 */
3699 {
3700 target_event_action_t *teap;
3701 teap = target->event_action;
3702 command_print( cmd_ctx, "Event actions for target (%d) %s\n",
3703 target->target_number,
3704 target->cmd_name );
3705 command_print( cmd_ctx, "%-25s | Body", "Event");
3706 command_print( cmd_ctx, "------------------------- | ----------------------------------------");
3707 while( teap ){
3708 command_print( cmd_ctx,
3709 "%-25s | %s",
3710 Jim_Nvp_value2name_simple( nvp_target_event, teap->event )->name,
3711 Jim_GetString( teap->body, NULL ) );
3712 teap = teap->next;
3713 }
3714 command_print( cmd_ctx, "***END***");
3715 return JIM_OK;
3716 }
3717 case TS_CMD_CURSTATE:
3718 if( goi.argc != 0 ){
3719 Jim_WrongNumArgs( goi.interp, 0, argv, "[no parameters]");
3720 return JIM_ERR;
3721 }
3722 Jim_SetResultString( goi.interp,
3723 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name,-1);
3724 return JIM_OK;
3725 case TS_CMD_INVOKE_EVENT:
3726 if( goi.argc != 1 ){
3727 Jim_SetResult_sprintf( goi.interp, "%s ?EVENTNAME?",n->name);
3728 return JIM_ERR;
3729 }
3730 e = Jim_GetOpt_Nvp( &goi, nvp_target_event, &n );
3731 if( e != JIM_OK ){
3732 Jim_GetOpt_NvpUnknown( &goi, nvp_target_event, 1 );
3733 return e;
3734 }
3735 target_handle_event( target, n->value );
3736 return JIM_OK;
3737 }
3738 return JIM_ERR;
3739 }
3740
3741 static int target_create( Jim_GetOptInfo *goi )
3742 {
3743 Jim_Obj *new_cmd;
3744 Jim_Cmd *cmd;
3745 const char *cp;
3746 char *cp2;
3747 int e;
3748 int x;
3749 target_t *target;
3750 struct command_context_s *cmd_ctx;
3751
3752 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
3753 if( goi->argc < 3 ){
3754 Jim_WrongNumArgs( goi->interp, 1, goi->argv, "?name? ?type? ..options...");
3755 return JIM_ERR;
3756 }
3757
3758 /* COMMAND */
3759 Jim_GetOpt_Obj( goi, &new_cmd );
3760 /* does this command exist? */
3761 cmd = Jim_GetCommand( goi->interp, new_cmd, JIM_ERRMSG );
3762 if( cmd ){
3763 cp = Jim_GetString( new_cmd, NULL );
3764 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
3765 return JIM_ERR;
3766 }
3767
3768 /* TYPE */
3769 e = Jim_GetOpt_String( goi, &cp2, NULL );
3770 cp = cp2;
3771 /* now does target type exist */
3772 for( x = 0 ; target_types[x] ; x++ ){
3773 if( 0 == strcmp( cp, target_types[x]->name ) ){
3774 /* found */
3775 break;
3776 }
3777 }
3778 if( target_types[x] == NULL ){
3779 Jim_SetResult_sprintf( goi->interp, "Unknown target type %s, try one of ", cp );
3780 for( x = 0 ; target_types[x] ; x++ ){
3781 if( target_types[x+1] ){
3782 Jim_AppendStrings( goi->interp,
3783 Jim_GetResult(goi->interp),
3784 target_types[x]->name,
3785 ", ", NULL);
3786 } else {
3787 Jim_AppendStrings( goi->interp,
3788 Jim_GetResult(goi->interp),
3789 " or ",
3790 target_types[x]->name,NULL );
3791 }
3792 }
3793 return JIM_ERR;
3794 }
3795
3796 /* Create it */
3797 target = calloc(1,sizeof(target_t));
3798 /* set target number */
3799 target->target_number = new_target_number();
3800
3801 /* allocate memory for each unique target type */
3802 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
3803
3804 memcpy( target->type, target_types[x], sizeof(target_type_t));
3805
3806 /* will be set by "-endian" */
3807 target->endianness = TARGET_ENDIAN_UNKNOWN;
3808
3809 target->working_area = 0x0;
3810 target->working_area_size = 0x0;
3811 target->working_areas = NULL;
3812 target->backup_working_area = 0;
3813
3814 target->state = TARGET_UNKNOWN;
3815 target->debug_reason = DBG_REASON_UNDEFINED;
3816 target->reg_cache = NULL;
3817 target->breakpoints = NULL;
3818 target->watchpoints = NULL;
3819 target->next = NULL;
3820 target->arch_info = NULL;
3821
3822 target->display = 1;
3823
3824 /* initialize trace information */
3825 target->trace_info = malloc(sizeof(trace_t));
3826 target->trace_info->num_trace_points = 0;
3827 target->trace_info->trace_points_size = 0;
3828 target->trace_info->trace_points = NULL;
3829 target->trace_info->trace_history_size = 0;
3830 target->trace_info->trace_history = NULL;
3831 target->trace_info->trace_history_pos = 0;
3832 target->trace_info->trace_history_overflowed = 0;
3833
3834 target->dbgmsg = NULL;
3835 target->dbg_msg_enabled = 0;
3836
3837 target->endianness = TARGET_ENDIAN_UNKNOWN;
3838
3839 /* Do the rest as "configure" options */
3840 goi->isconfigure = 1;
3841 e = target_configure( goi, target);
3842 if( e != JIM_OK ){
3843 free( target->type );
3844 free( target );
3845 return e;
3846 }
3847
3848 if( target->endianness == TARGET_ENDIAN_UNKNOWN ){
3849 /* default endian to little if not specified */
3850 target->endianness = TARGET_LITTLE_ENDIAN;
3851 }
3852
3853 /* incase variant is not set */
3854 if (!target->variant)
3855 target->variant = strdup("");
3856
3857 /* create the target specific commands */
3858 if( target->type->register_commands ){
3859 (*(target->type->register_commands))( cmd_ctx );
3860 }
3861 if( target->type->target_create ){
3862 (*(target->type->target_create))( target, goi->interp );
3863 }
3864
3865 /* append to end of list */
3866 {
3867 target_t **tpp;
3868 tpp = &(all_targets);
3869 while( *tpp ){
3870 tpp = &( (*tpp)->next );
3871 }
3872 *tpp = target;
3873 }
3874
3875 cp = Jim_GetString( new_cmd, NULL );
3876 target->cmd_name = strdup(cp);
3877
3878 /* now - create the new target name command */
3879 e = Jim_CreateCommand( goi->interp,
3880 /* name */
3881 cp,
3882 tcl_target_func, /* C function */
3883 target, /* private data */
3884 NULL ); /* no del proc */
3885
3886 return e;
3887 }
3888
3889 static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
3890 {
3891 int x,r,e;
3892 jim_wide w;
3893 struct command_context_s *cmd_ctx;
3894 target_t *target;
3895 Jim_GetOptInfo goi;
3896 enum tcmd {
3897 /* TG = target generic */
3898 TG_CMD_CREATE,
3899 TG_CMD_TYPES,
3900 TG_CMD_NAMES,
3901 TG_CMD_CURRENT,
3902 TG_CMD_NUMBER,
3903 TG_CMD_COUNT,
3904 };
3905 const char *target_cmds[] = {
3906 "create", "types", "names", "current", "number",
3907 "count",
3908 NULL /* terminate */
3909 };
3910
3911 LOG_DEBUG("Target command params:");
3912 LOG_DEBUG(Jim_Debug_ArgvString( interp, argc, argv) );
3913
3914 cmd_ctx = Jim_GetAssocData( interp, "context" );
3915
3916 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
3917
3918 if( goi.argc == 0 ){
3919 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
3920 return JIM_ERR;
3921 }
3922
3923 /* Jim_GetOpt_Debug( &goi ); */
3924 r = Jim_GetOpt_Enum( &goi, target_cmds, &x );
3925 if( r != JIM_OK ){
3926 return r;
3927 }
3928
3929 switch(x){
3930 default:
3931 Jim_Panic(goi.interp,"Why am I here?");
3932 return JIM_ERR;
3933 case TG_CMD_CURRENT:
3934 if( goi.argc != 0 ){
3935 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters");
3936 return JIM_ERR;
3937 }
3938 Jim_SetResultString( goi.interp, get_current_target( cmd_ctx )->cmd_name, -1 );
3939 return JIM_OK;
3940 case TG_CMD_TYPES:
3941 if( goi.argc != 0 ){
3942 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
3943 return JIM_ERR;
3944 }
3945 Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
3946 for( x = 0 ; target_types[x] ; x++ ){
3947 Jim_ListAppendElement( goi.interp,
3948 Jim_GetResult(goi.interp),
3949 Jim_NewStringObj( goi.interp, target_types[x]->name, -1 ) );
3950 }
3951 return JIM_OK;
3952 case TG_CMD_NAMES:
3953 if( goi.argc != 0 ){
3954 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
3955 return JIM_ERR;
3956 }
3957 Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
3958 target = all_targets;
3959 while( target ){
3960 Jim_ListAppendElement( goi.interp,
3961 Jim_GetResult(goi.interp),
3962 Jim_NewStringObj( goi.interp, target->cmd_name, -1 ) );
3963 target = target->next;
3964 }
3965 return JIM_OK;
3966 case TG_CMD_CREATE:
3967 if( goi.argc < 3 ){
3968 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "?name ... config options ...");
3969 return JIM_ERR;
3970 }
3971 return target_create( &goi );
3972 break;
3973 case TG_CMD_NUMBER:
3974 if( goi.argc != 1 ){
3975 Jim_SetResult_sprintf( goi.interp, "expected: target number ?NUMBER?");
3976 return JIM_ERR;
3977 }
3978 e = Jim_GetOpt_Wide( &goi, &w );
3979 if( e != JIM_OK ){
3980 return JIM_ERR;
3981 }
3982 {
3983 target_t *t;
3984 t = get_target_by_num(w);
3985 if( t == NULL ){
3986 Jim_SetResult_sprintf( goi.interp,"Target: number %d does not exist", (int)(w));
3987 return JIM_ERR;
3988 }
3989 Jim_SetResultString( goi.interp, t->cmd_name, -1 );
3990 return JIM_OK;
3991 }
3992 case TG_CMD_COUNT:
3993 if( goi.argc != 0 ){
3994 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "<no parameters>");
3995 return JIM_ERR;
3996 }
3997 Jim_SetResult( goi.interp,
3998 Jim_NewIntObj( goi.interp, max_target_number()));
3999 return JIM_OK;
4000 }
4001
4002 return JIM_ERR;
4003 }
4004
4005
4006 struct FastLoad
4007 {
4008 u32 address;
4009 u8 *data;
4010 int length;
4011
4012 };
4013
4014 static int fastload_num;
4015 static struct FastLoad *fastload;
4016
4017 static void free_fastload()
4018 {
4019 if (fastload!=NULL)
4020 {
4021 int i;
4022 for (i=0; i<fastload_num; i++)
4023 {
4024 if (fastload[i].data)
4025 free(fastload[i].data);
4026 }
4027 free(fastload);
4028 fastload=NULL;
4029 }
4030 }
4031
4032
4033
4034
4035 int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4036 {
4037 u8 *buffer;
4038 u32 buf_cnt;
4039 u32 image_size;
4040 u32 min_address=0;
4041 u32 max_address=0xffffffff;
4042 int i;
4043 int retval;
4044
4045 image_t image;
4046
4047 duration_t duration;
4048 char *duration_text;
4049
4050 if ((argc < 1)||(argc > 5))
4051 {
4052 return ERROR_COMMAND_SYNTAX_ERROR;
4053 }
4054
4055 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
4056 if (argc >= 2)
4057 {
4058 image.base_address_set = 1;
4059 image.base_address = strtoul(args[1], NULL, 0);
4060 }
4061 else
4062 {
4063 image.base_address_set = 0;
4064 }
4065
4066
4067 image.start_address_set = 0;
4068
4069 if (argc>=4)
4070 {
4071 min_address=strtoul(args[3], NULL, 0);
4072 }
4073 if (argc>=5)
4074 {
4075 max_address=strtoul(args[4], NULL, 0)+min_address;
4076 }
4077
4078 if (min_address>max_address)
4079 {
4080 return ERROR_COMMAND_SYNTAX_ERROR;
4081 }
4082
4083 duration_start_measure(&duration);
4084
4085 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4086 {
4087 return ERROR_OK;
4088 }
4089
4090 image_size = 0x0;
4091 retval = ERROR_OK;
4092 fastload_num=image.num_sections;
4093 fastload=(struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4094 if (fastload==NULL)
4095 {
4096 image_close(&image);
4097 return ERROR_FAIL;
4098 }
4099 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4100 for (i = 0; i < image.num_sections; i++)
4101 {
4102 buffer = malloc(image.sections[i].size);
4103 if (buffer == NULL)
4104 {
4105 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
4106 break;
4107 }
4108
4109 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4110 {
4111 free(buffer);
4112 break;
4113 }
4114
4115 u32 offset=0;
4116 u32 length=buf_cnt;
4117
4118
4119 /* DANGER!!! beware of unsigned comparision here!!! */
4120
4121 if ((image.sections[i].base_address+buf_cnt>=min_address)&&
4122 (image.sections[i].base_address<max_address))
4123 {
4124 if (image.sections[i].base_address<min_address)
4125 {
4126 /* clip addresses below */
4127 offset+=min_address-image.sections[i].base_address;
4128 length-=offset;
4129 }
4130
4131 if (image.sections[i].base_address+buf_cnt>max_address)
4132 {
4133 length-=(image.sections[i].base_address+buf_cnt)-max_address;
4134 }
4135
4136 fastload[i].address=image.sections[i].base_address+offset;
4137 fastload[i].data=malloc(length);
4138 if (fastload[i].data==NULL)
4139 {
4140 free(buffer);
4141 break;
4142 }
4143 memcpy(fastload[i].data, buffer+offset, length);
4144 fastload[i].length=length;
4145
4146 image_size += length;
4147 command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
4148 }
4149
4150 free(buffer);
4151 }
4152
4153 duration_stop_measure(&duration, &duration_text);
4154 if (retval==ERROR_OK)
4155 {
4156 command_print(cmd_ctx, "Loaded %u bytes in %s", image_size, duration_text);
4157 command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4158 }
4159 free(duration_text);
4160
4161 image_close(&image);
4162
4163 if (retval!=ERROR_OK)
4164 {
4165 free_fastload();
4166 }
4167
4168 return retval;
4169 }
4170
4171 int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4172 {
4173 if (argc>0)
4174 return ERROR_COMMAND_SYNTAX_ERROR;
4175 if (fastload==NULL)
4176 {
4177 LOG_ERROR("No image in memory");
4178 return ERROR_FAIL;
4179 }
4180 int i;
4181 int ms=timeval_ms();
4182 int size=0;
4183 for (i=0; i<fastload_num;i++)
4184 {
4185 int retval;
4186 target_t *target = get_current_target(cmd_ctx);
4187 if ((retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data)) != ERROR_OK)
4188 {
4189 return retval;
4190 }
4191 size+=fastload[i].length;
4192 }
4193 int after=timeval_ms();
4194 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4195 return ERROR_OK;
4196 }

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)