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