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

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)