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

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)