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

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)