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

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)