command_registration: add empty usage field to chained commands
[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  *   Copyright (C) 2011 Andreas Fritiofson                                 *
24  *   andreas.fritiofson@gmail.com                                          *
25  *                                                                         *
26  *   This program is free software; you can redistribute it and/or modify  *
27  *   it under the terms of the GNU General Public License as published by  *
28  *   the Free Software Foundation; either version 2 of the License, or     *
29  *   (at your option) any later version.                                   *
30  *                                                                         *
31  *   This program is distributed in the hope that it will be useful,       *
32  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
33  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
34  *   GNU General Public License for more details.                          *
35  *                                                                         *
36  *   You should have received a copy of the GNU General Public License     *
37  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
38  ***************************************************************************/
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #include <helper/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
47
48 #include "target.h"
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
52 #include "register.h"
53 #include "trace.h"
54 #include "image.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
57 #include "arm_cti.h"
58
59 /* default halt wait timeout (ms) */
60 #define DEFAULT_HALT_TIMEOUT 5000
61
62 static int target_read_buffer_default(struct target *target, target_addr_t address,
63                 uint32_t count, uint8_t *buffer);
64 static int target_write_buffer_default(struct target *target, target_addr_t address,
65                 uint32_t count, const uint8_t *buffer);
66 static int target_array2mem(Jim_Interp *interp, struct target *target,
67                 int argc, Jim_Obj * const *argv);
68 static int target_mem2array(Jim_Interp *interp, struct target *target,
69                 int argc, Jim_Obj * const *argv);
70 static int target_register_user_commands(struct command_context *cmd_ctx);
71 static int target_get_gdb_fileio_info_default(struct target *target,
72                 struct gdb_fileio_info *fileio_info);
73 static int target_gdb_fileio_end_default(struct target *target, int retcode,
74                 int fileio_errno, bool ctrl_c);
75 static int target_profiling_default(struct target *target, uint32_t *samples,
76                 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds);
77
78 /* targets */
79 extern struct target_type arm7tdmi_target;
80 extern struct target_type arm720t_target;
81 extern struct target_type arm9tdmi_target;
82 extern struct target_type arm920t_target;
83 extern struct target_type arm966e_target;
84 extern struct target_type arm946e_target;
85 extern struct target_type arm926ejs_target;
86 extern struct target_type fa526_target;
87 extern struct target_type feroceon_target;
88 extern struct target_type dragonite_target;
89 extern struct target_type xscale_target;
90 extern struct target_type cortexm_target;
91 extern struct target_type cortexa_target;
92 extern struct target_type aarch64_target;
93 extern struct target_type cortexr4_target;
94 extern struct target_type arm11_target;
95 extern struct target_type ls1_sap_target;
96 extern struct target_type mips_m4k_target;
97 extern struct target_type avr_target;
98 extern struct target_type dsp563xx_target;
99 extern struct target_type dsp5680xx_target;
100 extern struct target_type testee_target;
101 extern struct target_type avr32_ap7k_target;
102 extern struct target_type hla_target;
103 extern struct target_type nds32_v2_target;
104 extern struct target_type nds32_v3_target;
105 extern struct target_type nds32_v3m_target;
106 extern struct target_type or1k_target;
107 extern struct target_type quark_x10xx_target;
108 extern struct target_type quark_d20xx_target;
109 extern struct target_type stm8_target;
110 extern struct target_type riscv_target;
111 extern struct target_type mem_ap_target;
112 extern struct target_type esirisc_target;
113
114 static struct target_type *target_types[] = {
115         &arm7tdmi_target,
116         &arm9tdmi_target,
117         &arm920t_target,
118         &arm720t_target,
119         &arm966e_target,
120         &arm946e_target,
121         &arm926ejs_target,
122         &fa526_target,
123         &feroceon_target,
124         &dragonite_target,
125         &xscale_target,
126         &cortexm_target,
127         &cortexa_target,
128         &cortexr4_target,
129         &arm11_target,
130         &ls1_sap_target,
131         &mips_m4k_target,
132         &avr_target,
133         &dsp563xx_target,
134         &dsp5680xx_target,
135         &testee_target,
136         &avr32_ap7k_target,
137         &hla_target,
138         &nds32_v2_target,
139         &nds32_v3_target,
140         &nds32_v3m_target,
141         &or1k_target,
142         &quark_x10xx_target,
143         &quark_d20xx_target,
144         &stm8_target,
145         &riscv_target,
146         &mem_ap_target,
147         &esirisc_target,
148 #if BUILD_TARGET64
149         &aarch64_target,
150 #endif
151         NULL,
152 };
153
154 struct target *all_targets;
155 static struct target_event_callback *target_event_callbacks;
156 static struct target_timer_callback *target_timer_callbacks;
157 LIST_HEAD(target_reset_callback_list);
158 LIST_HEAD(target_trace_callback_list);
159 static const int polling_interval = 100;
160
161 static const Jim_Nvp nvp_assert[] = {
162         { .name = "assert", NVP_ASSERT },
163         { .name = "deassert", NVP_DEASSERT },
164         { .name = "T", NVP_ASSERT },
165         { .name = "F", NVP_DEASSERT },
166         { .name = "t", NVP_ASSERT },
167         { .name = "f", NVP_DEASSERT },
168         { .name = NULL, .value = -1 }
169 };
170
171 static const Jim_Nvp nvp_error_target[] = {
172         { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
173         { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
174         { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
175         { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
176         { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
177         { .value = ERROR_TARGET_UNALIGNED_ACCESS   , .name = "err-unaligned-access" },
178         { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
179         { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
180         { .value = ERROR_TARGET_TRANSLATION_FAULT  , .name = "err-translation-fault" },
181         { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
182         { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
183         { .value = -1, .name = NULL }
184 };
185
186 static const char *target_strerror_safe(int err)
187 {
188         const Jim_Nvp *n;
189
190         n = Jim_Nvp_value2name_simple(nvp_error_target, err);
191         if (n->name == NULL)
192                 return "unknown";
193         else
194                 return n->name;
195 }
196
197 static const Jim_Nvp nvp_target_event[] = {
198
199         { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
200         { .value = TARGET_EVENT_HALTED, .name = "halted" },
201         { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
202         { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
203         { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
204
205         { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
206         { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
207
208         { .value = TARGET_EVENT_RESET_START,         .name = "reset-start" },
209         { .value = TARGET_EVENT_RESET_ASSERT_PRE,    .name = "reset-assert-pre" },
210         { .value = TARGET_EVENT_RESET_ASSERT,        .name = "reset-assert" },
211         { .value = TARGET_EVENT_RESET_ASSERT_POST,   .name = "reset-assert-post" },
212         { .value = TARGET_EVENT_RESET_DEASSERT_PRE,  .name = "reset-deassert-pre" },
213         { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
214         { .value = TARGET_EVENT_RESET_INIT,          .name = "reset-init" },
215         { .value = TARGET_EVENT_RESET_END,           .name = "reset-end" },
216
217         { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
218         { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
219
220         { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
221         { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
222
223         { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
224         { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
225
226         { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
227         { .value = TARGET_EVENT_GDB_FLASH_WRITE_END  , .name = "gdb-flash-write-end"   },
228
229         { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
230         { .value = TARGET_EVENT_GDB_FLASH_ERASE_END  , .name = "gdb-flash-erase-end" },
231
232         { .value = TARGET_EVENT_TRACE_CONFIG, .name = "trace-config" },
233
234         { .name = NULL, .value = -1 }
235 };
236
237 static const Jim_Nvp nvp_target_state[] = {
238         { .name = "unknown", .value = TARGET_UNKNOWN },
239         { .name = "running", .value = TARGET_RUNNING },
240         { .name = "halted",  .value = TARGET_HALTED },
241         { .name = "reset",   .value = TARGET_RESET },
242         { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
243         { .name = NULL, .value = -1 },
244 };
245
246 static const Jim_Nvp nvp_target_debug_reason[] = {
247         { .name = "debug-request"            , .value = DBG_REASON_DBGRQ },
248         { .name = "breakpoint"               , .value = DBG_REASON_BREAKPOINT },
249         { .name = "watchpoint"               , .value = DBG_REASON_WATCHPOINT },
250         { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
251         { .name = "single-step"              , .value = DBG_REASON_SINGLESTEP },
252         { .name = "target-not-halted"        , .value = DBG_REASON_NOTHALTED  },
253         { .name = "program-exit"             , .value = DBG_REASON_EXIT },
254         { .name = "exception-catch"          , .value = DBG_REASON_EXC_CATCH },
255         { .name = "undefined"                , .value = DBG_REASON_UNDEFINED },
256         { .name = NULL, .value = -1 },
257 };
258
259 static const Jim_Nvp nvp_target_endian[] = {
260         { .name = "big",    .value = TARGET_BIG_ENDIAN },
261         { .name = "little", .value = TARGET_LITTLE_ENDIAN },
262         { .name = "be",     .value = TARGET_BIG_ENDIAN },
263         { .name = "le",     .value = TARGET_LITTLE_ENDIAN },
264         { .name = NULL,     .value = -1 },
265 };
266
267 static const Jim_Nvp nvp_reset_modes[] = {
268         { .name = "unknown", .value = RESET_UNKNOWN },
269         { .name = "run"    , .value = RESET_RUN },
270         { .name = "halt"   , .value = RESET_HALT },
271         { .name = "init"   , .value = RESET_INIT },
272         { .name = NULL     , .value = -1 },
273 };
274
275 const char *debug_reason_name(struct target *t)
276 {
277         const char *cp;
278
279         cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
280                         t->debug_reason)->name;
281         if (!cp) {
282                 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
283                 cp = "(*BUG*unknown*BUG*)";
284         }
285         return cp;
286 }
287
288 const char *target_state_name(struct target *t)
289 {
290         const char *cp;
291         cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
292         if (!cp) {
293                 LOG_ERROR("Invalid target state: %d", (int)(t->state));
294                 cp = "(*BUG*unknown*BUG*)";
295         }
296
297         if (!target_was_examined(t) && t->defer_examine)
298                 cp = "examine deferred";
299
300         return cp;
301 }
302
303 const char *target_event_name(enum target_event event)
304 {
305         const char *cp;
306         cp = Jim_Nvp_value2name_simple(nvp_target_event, event)->name;
307         if (!cp) {
308                 LOG_ERROR("Invalid target event: %d", (int)(event));
309                 cp = "(*BUG*unknown*BUG*)";
310         }
311         return cp;
312 }
313
314 const char *target_reset_mode_name(enum target_reset_mode reset_mode)
315 {
316         const char *cp;
317         cp = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name;
318         if (!cp) {
319                 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode));
320                 cp = "(*BUG*unknown*BUG*)";
321         }
322         return cp;
323 }
324
325 /* determine the number of the new target */
326 static int new_target_number(void)
327 {
328         struct target *t;
329         int x;
330
331         /* number is 0 based */
332         x = -1;
333         t = all_targets;
334         while (t) {
335                 if (x < t->target_number)
336                         x = t->target_number;
337                 t = t->next;
338         }
339         return x + 1;
340 }
341
342 /* read a uint64_t from a buffer in target memory endianness */
343 uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
344 {
345         if (target->endianness == TARGET_LITTLE_ENDIAN)
346                 return le_to_h_u64(buffer);
347         else
348                 return be_to_h_u64(buffer);
349 }
350
351 /* read a uint32_t from a buffer in target memory endianness */
352 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
353 {
354         if (target->endianness == TARGET_LITTLE_ENDIAN)
355                 return le_to_h_u32(buffer);
356         else
357                 return be_to_h_u32(buffer);
358 }
359
360 /* read a uint24_t from a buffer in target memory endianness */
361 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
362 {
363         if (target->endianness == TARGET_LITTLE_ENDIAN)
364                 return le_to_h_u24(buffer);
365         else
366                 return be_to_h_u24(buffer);
367 }
368
369 /* read a uint16_t from a buffer in target memory endianness */
370 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
371 {
372         if (target->endianness == TARGET_LITTLE_ENDIAN)
373                 return le_to_h_u16(buffer);
374         else
375                 return be_to_h_u16(buffer);
376 }
377
378 /* read a uint8_t from a buffer in target memory endianness */
379 static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
380 {
381         return *buffer & 0x0ff;
382 }
383
384 /* write a uint64_t to a buffer in target memory endianness */
385 void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
386 {
387         if (target->endianness == TARGET_LITTLE_ENDIAN)
388                 h_u64_to_le(buffer, value);
389         else
390                 h_u64_to_be(buffer, value);
391 }
392
393 /* write a uint32_t to a buffer in target memory endianness */
394 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
395 {
396         if (target->endianness == TARGET_LITTLE_ENDIAN)
397                 h_u32_to_le(buffer, value);
398         else
399                 h_u32_to_be(buffer, value);
400 }
401
402 /* write a uint24_t to a buffer in target memory endianness */
403 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
404 {
405         if (target->endianness == TARGET_LITTLE_ENDIAN)
406                 h_u24_to_le(buffer, value);
407         else
408                 h_u24_to_be(buffer, value);
409 }
410
411 /* write a uint16_t to a buffer in target memory endianness */
412 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
413 {
414         if (target->endianness == TARGET_LITTLE_ENDIAN)
415                 h_u16_to_le(buffer, value);
416         else
417                 h_u16_to_be(buffer, value);
418 }
419
420 /* write a uint8_t to a buffer in target memory endianness */
421 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
422 {
423         *buffer = value;
424 }
425
426 /* write a uint64_t array to a buffer in target memory endianness */
427 void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
428 {
429         uint32_t i;
430         for (i = 0; i < count; i++)
431                 dstbuf[i] = target_buffer_get_u64(target, &buffer[i * 8]);
432 }
433
434 /* write a uint32_t array to a buffer in target memory endianness */
435 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
436 {
437         uint32_t i;
438         for (i = 0; i < count; i++)
439                 dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
440 }
441
442 /* write a uint16_t array to a buffer in target memory endianness */
443 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
444 {
445         uint32_t i;
446         for (i = 0; i < count; i++)
447                 dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
448 }
449
450 /* write a uint64_t array to a buffer in target memory endianness */
451 void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
452 {
453         uint32_t i;
454         for (i = 0; i < count; i++)
455                 target_buffer_set_u64(target, &buffer[i * 8], srcbuf[i]);
456 }
457
458 /* write a uint32_t array to a buffer in target memory endianness */
459 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
460 {
461         uint32_t i;
462         for (i = 0; i < count; i++)
463                 target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
464 }
465
466 /* write a uint16_t array to a buffer in target memory endianness */
467 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
468 {
469         uint32_t i;
470         for (i = 0; i < count; i++)
471                 target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
472 }
473
474 /* return a pointer to a configured target; id is name or number */
475 struct target *get_target(const char *id)
476 {
477         struct target *target;
478
479         /* try as tcltarget name */
480         for (target = all_targets; target; target = target->next) {
481                 if (target_name(target) == NULL)
482                         continue;
483                 if (strcmp(id, target_name(target)) == 0)
484                         return target;
485         }
486
487         /* It's OK to remove this fallback sometime after August 2010 or so */
488
489         /* no match, try as number */
490         unsigned num;
491         if (parse_uint(id, &num) != ERROR_OK)
492                 return NULL;
493
494         for (target = all_targets; target; target = target->next) {
495                 if (target->target_number == (int)num) {
496                         LOG_WARNING("use '%s' as target identifier, not '%u'",
497                                         target_name(target), num);
498                         return target;
499                 }
500         }
501
502         return NULL;
503 }
504
505 /* returns a pointer to the n-th configured target */
506 struct target *get_target_by_num(int num)
507 {
508         struct target *target = all_targets;
509
510         while (target) {
511                 if (target->target_number == num)
512                         return target;
513                 target = target->next;
514         }
515
516         return NULL;
517 }
518
519 struct target *get_current_target(struct command_context *cmd_ctx)
520 {
521         struct target *target = get_current_target_or_null(cmd_ctx);
522
523         if (target == NULL) {
524                 LOG_ERROR("BUG: current_target out of bounds");
525                 exit(-1);
526         }
527
528         return target;
529 }
530
531 struct target *get_current_target_or_null(struct command_context *cmd_ctx)
532 {
533         return cmd_ctx->current_target_override
534                 ? cmd_ctx->current_target_override
535                 : cmd_ctx->current_target;
536 }
537
538 int target_poll(struct target *target)
539 {
540         int retval;
541
542         /* We can't poll until after examine */
543         if (!target_was_examined(target)) {
544                 /* Fail silently lest we pollute the log */
545                 return ERROR_FAIL;
546         }
547
548         retval = target->type->poll(target);
549         if (retval != ERROR_OK)
550                 return retval;
551
552         if (target->halt_issued) {
553                 if (target->state == TARGET_HALTED)
554                         target->halt_issued = false;
555                 else {
556                         int64_t t = timeval_ms() - target->halt_issued_time;
557                         if (t > DEFAULT_HALT_TIMEOUT) {
558                                 target->halt_issued = false;
559                                 LOG_INFO("Halt timed out, wake up GDB.");
560                                 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
561                         }
562                 }
563         }
564
565         return ERROR_OK;
566 }
567
568 int target_halt(struct target *target)
569 {
570         int retval;
571         /* We can't poll until after examine */
572         if (!target_was_examined(target)) {
573                 LOG_ERROR("Target not examined yet");
574                 return ERROR_FAIL;
575         }
576
577         retval = target->type->halt(target);
578         if (retval != ERROR_OK)
579                 return retval;
580
581         target->halt_issued = true;
582         target->halt_issued_time = timeval_ms();
583
584         return ERROR_OK;
585 }
586
587 /**
588  * Make the target (re)start executing using its saved execution
589  * context (possibly with some modifications).
590  *
591  * @param target Which target should start executing.
592  * @param current True to use the target's saved program counter instead
593  *      of the address parameter
594  * @param address Optionally used as the program counter.
595  * @param handle_breakpoints True iff breakpoints at the resumption PC
596  *      should be skipped.  (For example, maybe execution was stopped by
597  *      such a breakpoint, in which case it would be counterprodutive to
598  *      let it re-trigger.
599  * @param debug_execution False if all working areas allocated by OpenOCD
600  *      should be released and/or restored to their original contents.
601  *      (This would for example be true to run some downloaded "helper"
602  *      algorithm code, which resides in one such working buffer and uses
603  *      another for data storage.)
604  *
605  * @todo Resolve the ambiguity about what the "debug_execution" flag
606  * signifies.  For example, Target implementations don't agree on how
607  * it relates to invalidation of the register cache, or to whether
608  * breakpoints and watchpoints should be enabled.  (It would seem wrong
609  * to enable breakpoints when running downloaded "helper" algorithms
610  * (debug_execution true), since the breakpoints would be set to match
611  * target firmware being debugged, not the helper algorithm.... and
612  * enabling them could cause such helpers to malfunction (for example,
613  * by overwriting data with a breakpoint instruction.  On the other
614  * hand the infrastructure for running such helpers might use this
615  * procedure but rely on hardware breakpoint to detect termination.)
616  */
617 int target_resume(struct target *target, int current, target_addr_t address,
618                 int handle_breakpoints, int debug_execution)
619 {
620         int retval;
621
622         /* We can't poll until after examine */
623         if (!target_was_examined(target)) {
624                 LOG_ERROR("Target not examined yet");
625                 return ERROR_FAIL;
626         }
627
628         target_call_event_callbacks(target, TARGET_EVENT_RESUME_START);
629
630         /* note that resume *must* be asynchronous. The CPU can halt before
631          * we poll. The CPU can even halt at the current PC as a result of
632          * a software breakpoint being inserted by (a bug?) the application.
633          */
634         retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
635         if (retval != ERROR_OK)
636                 return retval;
637
638         target_call_event_callbacks(target, TARGET_EVENT_RESUME_END);
639
640         return retval;
641 }
642
643 static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
644 {
645         char buf[100];
646         int retval;
647         Jim_Nvp *n;
648         n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
649         if (n->name == NULL) {
650                 LOG_ERROR("invalid reset mode");
651                 return ERROR_FAIL;
652         }
653
654         struct target *target;
655         for (target = all_targets; target; target = target->next)
656                 target_call_reset_callbacks(target, reset_mode);
657
658         /* disable polling during reset to make reset event scripts
659          * more predictable, i.e. dr/irscan & pathmove in events will
660          * not have JTAG operations injected into the middle of a sequence.
661          */
662         bool save_poll = jtag_poll_get_enabled();
663
664         jtag_poll_set_enabled(false);
665
666         sprintf(buf, "ocd_process_reset %s", n->name);
667         retval = Jim_Eval(cmd_ctx->interp, buf);
668
669         jtag_poll_set_enabled(save_poll);
670
671         if (retval != JIM_OK) {
672                 Jim_MakeErrorMessage(cmd_ctx->interp);
673                 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
674                 return ERROR_FAIL;
675         }
676
677         /* We want any events to be processed before the prompt */
678         retval = target_call_timer_callbacks_now();
679
680         for (target = all_targets; target; target = target->next) {
681                 target->type->check_reset(target);
682                 target->running_alg = false;
683         }
684
685         return retval;
686 }
687
688 static int identity_virt2phys(struct target *target,
689                 target_addr_t virtual, target_addr_t *physical)
690 {
691         *physical = virtual;
692         return ERROR_OK;
693 }
694
695 static int no_mmu(struct target *target, int *enabled)
696 {
697         *enabled = 0;
698         return ERROR_OK;
699 }
700
701 static int default_examine(struct target *target)
702 {
703         target_set_examined(target);
704         return ERROR_OK;
705 }
706
707 /* no check by default */
708 static int default_check_reset(struct target *target)
709 {
710         return ERROR_OK;
711 }
712
713 int target_examine_one(struct target *target)
714 {
715         target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_START);
716
717         int retval = target->type->examine(target);
718         if (retval != ERROR_OK)
719                 return retval;
720
721         target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_END);
722
723         return ERROR_OK;
724 }
725
726 static int jtag_enable_callback(enum jtag_event event, void *priv)
727 {
728         struct target *target = priv;
729
730         if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
731                 return ERROR_OK;
732
733         jtag_unregister_event_callback(jtag_enable_callback, target);
734
735         return target_examine_one(target);
736 }
737
738 /* Targets that correctly implement init + examine, i.e.
739  * no communication with target during init:
740  *
741  * XScale
742  */
743 int target_examine(void)
744 {
745         int retval = ERROR_OK;
746         struct target *target;
747
748         for (target = all_targets; target; target = target->next) {
749                 /* defer examination, but don't skip it */
750                 if (!target->tap->enabled) {
751                         jtag_register_event_callback(jtag_enable_callback,
752                                         target);
753                         continue;
754                 }
755
756                 if (target->defer_examine)
757                         continue;
758
759                 retval = target_examine_one(target);
760                 if (retval != ERROR_OK)
761                         return retval;
762         }
763         return retval;
764 }
765
766 const char *target_type_name(struct target *target)
767 {
768         return target->type->name;
769 }
770
771 static int target_soft_reset_halt(struct target *target)
772 {
773         if (!target_was_examined(target)) {
774                 LOG_ERROR("Target not examined yet");
775                 return ERROR_FAIL;
776         }
777         if (!target->type->soft_reset_halt) {
778                 LOG_ERROR("Target %s does not support soft_reset_halt",
779                                 target_name(target));
780                 return ERROR_FAIL;
781         }
782         return target->type->soft_reset_halt(target);
783 }
784
785 /**
786  * Downloads a target-specific native code algorithm to the target,
787  * and executes it.  * Note that some targets may need to set up, enable,
788  * and tear down a breakpoint (hard or * soft) to detect algorithm
789  * termination, while others may support  lower overhead schemes where
790  * soft breakpoints embedded in the algorithm automatically terminate the
791  * algorithm.
792  *
793  * @param target used to run the algorithm
794  * @param arch_info target-specific description of the algorithm.
795  */
796 int target_run_algorithm(struct target *target,
797                 int num_mem_params, struct mem_param *mem_params,
798                 int num_reg_params, struct reg_param *reg_param,
799                 uint32_t entry_point, uint32_t exit_point,
800                 int timeout_ms, void *arch_info)
801 {
802         int retval = ERROR_FAIL;
803
804         if (!target_was_examined(target)) {
805                 LOG_ERROR("Target not examined yet");
806                 goto done;
807         }
808         if (!target->type->run_algorithm) {
809                 LOG_ERROR("Target type '%s' does not support %s",
810                                 target_type_name(target), __func__);
811                 goto done;
812         }
813
814         target->running_alg = true;
815         retval = target->type->run_algorithm(target,
816                         num_mem_params, mem_params,
817                         num_reg_params, reg_param,
818                         entry_point, exit_point, timeout_ms, arch_info);
819         target->running_alg = false;
820
821 done:
822         return retval;
823 }
824
825 /**
826  * Executes a target-specific native code algorithm and leaves it running.
827  *
828  * @param target used to run the algorithm
829  * @param arch_info target-specific description of the algorithm.
830  */
831 int target_start_algorithm(struct target *target,
832                 int num_mem_params, struct mem_param *mem_params,
833                 int num_reg_params, struct reg_param *reg_params,
834                 uint32_t entry_point, uint32_t exit_point,
835                 void *arch_info)
836 {
837         int retval = ERROR_FAIL;
838
839         if (!target_was_examined(target)) {
840                 LOG_ERROR("Target not examined yet");
841                 goto done;
842         }
843         if (!target->type->start_algorithm) {
844                 LOG_ERROR("Target type '%s' does not support %s",
845                                 target_type_name(target), __func__);
846                 goto done;
847         }
848         if (target->running_alg) {
849                 LOG_ERROR("Target is already running an algorithm");
850                 goto done;
851         }
852
853         target->running_alg = true;
854         retval = target->type->start_algorithm(target,
855                         num_mem_params, mem_params,
856                         num_reg_params, reg_params,
857                         entry_point, exit_point, arch_info);
858
859 done:
860         return retval;
861 }
862
863 /**
864  * Waits for an algorithm started with target_start_algorithm() to complete.
865  *
866  * @param target used to run the algorithm
867  * @param arch_info target-specific description of the algorithm.
868  */
869 int target_wait_algorithm(struct target *target,
870                 int num_mem_params, struct mem_param *mem_params,
871                 int num_reg_params, struct reg_param *reg_params,
872                 uint32_t exit_point, int timeout_ms,
873                 void *arch_info)
874 {
875         int retval = ERROR_FAIL;
876
877         if (!target->type->wait_algorithm) {
878                 LOG_ERROR("Target type '%s' does not support %s",
879                                 target_type_name(target), __func__);
880                 goto done;
881         }
882         if (!target->running_alg) {
883                 LOG_ERROR("Target is not running an algorithm");
884                 goto done;
885         }
886
887         retval = target->type->wait_algorithm(target,
888                         num_mem_params, mem_params,
889                         num_reg_params, reg_params,
890                         exit_point, timeout_ms, arch_info);
891         if (retval != ERROR_TARGET_TIMEOUT)
892                 target->running_alg = false;
893
894 done:
895         return retval;
896 }
897
898 /**
899  * Streams data to a circular buffer on target intended for consumption by code
900  * running asynchronously on target.
901  *
902  * This is intended for applications where target-specific native code runs
903  * on the target, receives data from the circular buffer, does something with
904  * it (most likely writing it to a flash memory), and advances the circular
905  * buffer pointer.
906  *
907  * This assumes that the helper algorithm has already been loaded to the target,
908  * but has not been started yet. Given memory and register parameters are passed
909  * to the algorithm.
910  *
911  * The buffer is defined by (buffer_start, buffer_size) arguments and has the
912  * following format:
913  *
914  *     [buffer_start + 0, buffer_start + 4):
915  *         Write Pointer address (aka head). Written and updated by this
916  *         routine when new data is written to the circular buffer.
917  *     [buffer_start + 4, buffer_start + 8):
918  *         Read Pointer address (aka tail). Updated by code running on the
919  *         target after it consumes data.
920  *     [buffer_start + 8, buffer_start + buffer_size):
921  *         Circular buffer contents.
922  *
923  * See contrib/loaders/flash/stm32f1x.S for an example.
924  *
925  * @param target used to run the algorithm
926  * @param buffer address on the host where data to be sent is located
927  * @param count number of blocks to send
928  * @param block_size size in bytes of each block
929  * @param num_mem_params count of memory-based params to pass to algorithm
930  * @param mem_params memory-based params to pass to algorithm
931  * @param num_reg_params count of register-based params to pass to algorithm
932  * @param reg_params memory-based params to pass to algorithm
933  * @param buffer_start address on the target of the circular buffer structure
934  * @param buffer_size size of the circular buffer structure
935  * @param entry_point address on the target to execute to start the algorithm
936  * @param exit_point address at which to set a breakpoint to catch the
937  *     end of the algorithm; can be 0 if target triggers a breakpoint itself
938  */
939
940 int target_run_flash_async_algorithm(struct target *target,
941                 const uint8_t *buffer, uint32_t count, int block_size,
942                 int num_mem_params, struct mem_param *mem_params,
943                 int num_reg_params, struct reg_param *reg_params,
944                 uint32_t buffer_start, uint32_t buffer_size,
945                 uint32_t entry_point, uint32_t exit_point, void *arch_info)
946 {
947         int retval;
948         int timeout = 0;
949
950         const uint8_t *buffer_orig = buffer;
951
952         /* Set up working area. First word is write pointer, second word is read pointer,
953          * rest is fifo data area. */
954         uint32_t wp_addr = buffer_start;
955         uint32_t rp_addr = buffer_start + 4;
956         uint32_t fifo_start_addr = buffer_start + 8;
957         uint32_t fifo_end_addr = buffer_start + buffer_size;
958
959         uint32_t wp = fifo_start_addr;
960         uint32_t rp = fifo_start_addr;
961
962         /* validate block_size is 2^n */
963         assert(!block_size || !(block_size & (block_size - 1)));
964
965         retval = target_write_u32(target, wp_addr, wp);
966         if (retval != ERROR_OK)
967                 return retval;
968         retval = target_write_u32(target, rp_addr, rp);
969         if (retval != ERROR_OK)
970                 return retval;
971
972         /* Start up algorithm on target and let it idle while writing the first chunk */
973         retval = target_start_algorithm(target, num_mem_params, mem_params,
974                         num_reg_params, reg_params,
975                         entry_point,
976                         exit_point,
977                         arch_info);
978
979         if (retval != ERROR_OK) {
980                 LOG_ERROR("error starting target flash write algorithm");
981                 return retval;
982         }
983
984         while (count > 0) {
985
986                 retval = target_read_u32(target, rp_addr, &rp);
987                 if (retval != ERROR_OK) {
988                         LOG_ERROR("failed to get read pointer");
989                         break;
990                 }
991
992                 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32,
993                         (size_t) (buffer - buffer_orig), count, wp, rp);
994
995                 if (rp == 0) {
996                         LOG_ERROR("flash write algorithm aborted by target");
997                         retval = ERROR_FLASH_OPERATION_FAILED;
998                         break;
999                 }
1000
1001                 if (((rp - fifo_start_addr) & (block_size - 1)) || rp < fifo_start_addr || rp >= fifo_end_addr) {
1002                         LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
1003                         break;
1004                 }
1005
1006                 /* Count the number of bytes available in the fifo without
1007                  * crossing the wrap around. Make sure to not fill it completely,
1008                  * because that would make wp == rp and that's the empty condition. */
1009                 uint32_t thisrun_bytes;
1010                 if (rp > wp)
1011                         thisrun_bytes = rp - wp - block_size;
1012                 else if (rp > fifo_start_addr)
1013                         thisrun_bytes = fifo_end_addr - wp;
1014                 else
1015                         thisrun_bytes = fifo_end_addr - wp - block_size;
1016
1017                 if (thisrun_bytes == 0) {
1018                         /* Throttle polling a bit if transfer is (much) faster than flash
1019                          * programming. The exact delay shouldn't matter as long as it's
1020                          * less than buffer size / flash speed. This is very unlikely to
1021                          * run when using high latency connections such as USB. */
1022                         alive_sleep(10);
1023
1024                         /* to stop an infinite loop on some targets check and increment a timeout
1025                          * this issue was observed on a stellaris using the new ICDI interface */
1026                         if (timeout++ >= 500) {
1027                                 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1028                                 return ERROR_FLASH_OPERATION_FAILED;
1029                         }
1030                         continue;
1031                 }
1032
1033                 /* reset our timeout */
1034                 timeout = 0;
1035
1036                 /* Limit to the amount of data we actually want to write */
1037                 if (thisrun_bytes > count * block_size)
1038                         thisrun_bytes = count * block_size;
1039
1040                 /* Write data to fifo */
1041                 retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
1042                 if (retval != ERROR_OK)
1043                         break;
1044
1045                 /* Update counters and wrap write pointer */
1046                 buffer += thisrun_bytes;
1047                 count -= thisrun_bytes / block_size;
1048                 wp += thisrun_bytes;
1049                 if (wp >= fifo_end_addr)
1050                         wp = fifo_start_addr;
1051
1052                 /* Store updated write pointer to target */
1053                 retval = target_write_u32(target, wp_addr, wp);
1054                 if (retval != ERROR_OK)
1055                         break;
1056
1057                 /* Avoid GDB timeouts */
1058                 keep_alive();
1059         }
1060
1061         if (retval != ERROR_OK) {
1062                 /* abort flash write algorithm on target */
1063                 target_write_u32(target, wp_addr, 0);
1064         }
1065
1066         int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
1067                         num_reg_params, reg_params,
1068                         exit_point,
1069                         10000,
1070                         arch_info);
1071
1072         if (retval2 != ERROR_OK) {
1073                 LOG_ERROR("error waiting for target flash write algorithm");
1074                 retval = retval2;
1075         }
1076
1077         if (retval == ERROR_OK) {
1078                 /* check if algorithm set rp = 0 after fifo writer loop finished */
1079                 retval = target_read_u32(target, rp_addr, &rp);
1080                 if (retval == ERROR_OK && rp == 0) {
1081                         LOG_ERROR("flash write algorithm aborted by target");
1082                         retval = ERROR_FLASH_OPERATION_FAILED;
1083                 }
1084         }
1085
1086         return retval;
1087 }
1088
1089 int target_read_memory(struct target *target,
1090                 target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1091 {
1092         if (!target_was_examined(target)) {
1093                 LOG_ERROR("Target not examined yet");
1094                 return ERROR_FAIL;
1095         }
1096         if (!target->type->read_memory) {
1097                 LOG_ERROR("Target %s doesn't support read_memory", target_name(target));
1098                 return ERROR_FAIL;
1099         }
1100         return target->type->read_memory(target, address, size, count, buffer);
1101 }
1102
1103 int target_read_phys_memory(struct target *target,
1104                 target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1105 {
1106         if (!target_was_examined(target)) {
1107                 LOG_ERROR("Target not examined yet");
1108                 return ERROR_FAIL;
1109         }
1110         if (!target->type->read_phys_memory) {
1111                 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target));
1112                 return ERROR_FAIL;
1113         }
1114         return target->type->read_phys_memory(target, address, size, count, buffer);
1115 }
1116
1117 int target_write_memory(struct target *target,
1118                 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1119 {
1120         if (!target_was_examined(target)) {
1121                 LOG_ERROR("Target not examined yet");
1122                 return ERROR_FAIL;
1123         }
1124         if (!target->type->write_memory) {
1125                 LOG_ERROR("Target %s doesn't support write_memory", target_name(target));
1126                 return ERROR_FAIL;
1127         }
1128         return target->type->write_memory(target, address, size, count, buffer);
1129 }
1130
1131 int target_write_phys_memory(struct target *target,
1132                 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1133 {
1134         if (!target_was_examined(target)) {
1135                 LOG_ERROR("Target not examined yet");
1136                 return ERROR_FAIL;
1137         }
1138         if (!target->type->write_phys_memory) {
1139                 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target));
1140                 return ERROR_FAIL;
1141         }
1142         return target->type->write_phys_memory(target, address, size, count, buffer);
1143 }
1144
1145 int target_add_breakpoint(struct target *target,
1146                 struct breakpoint *breakpoint)
1147 {
1148         if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
1149                 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target));
1150                 return ERROR_TARGET_NOT_HALTED;
1151         }
1152         return target->type->add_breakpoint(target, breakpoint);
1153 }
1154
1155 int target_add_context_breakpoint(struct target *target,
1156                 struct breakpoint *breakpoint)
1157 {
1158         if (target->state != TARGET_HALTED) {
1159                 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target));
1160                 return ERROR_TARGET_NOT_HALTED;
1161         }
1162         return target->type->add_context_breakpoint(target, breakpoint);
1163 }
1164
1165 int target_add_hybrid_breakpoint(struct target *target,
1166                 struct breakpoint *breakpoint)
1167 {
1168         if (target->state != TARGET_HALTED) {
1169                 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target));
1170                 return ERROR_TARGET_NOT_HALTED;
1171         }
1172         return target->type->add_hybrid_breakpoint(target, breakpoint);
1173 }
1174
1175 int target_remove_breakpoint(struct target *target,
1176                 struct breakpoint *breakpoint)
1177 {
1178         return target->type->remove_breakpoint(target, breakpoint);
1179 }
1180
1181 int target_add_watchpoint(struct target *target,
1182                 struct watchpoint *watchpoint)
1183 {
1184         if (target->state != TARGET_HALTED) {
1185                 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target));
1186                 return ERROR_TARGET_NOT_HALTED;
1187         }
1188         return target->type->add_watchpoint(target, watchpoint);
1189 }
1190 int target_remove_watchpoint(struct target *target,
1191                 struct watchpoint *watchpoint)
1192 {
1193         return target->type->remove_watchpoint(target, watchpoint);
1194 }
1195 int target_hit_watchpoint(struct target *target,
1196                 struct watchpoint **hit_watchpoint)
1197 {
1198         if (target->state != TARGET_HALTED) {
1199                 LOG_WARNING("target %s is not halted (hit watchpoint)", target->cmd_name);
1200                 return ERROR_TARGET_NOT_HALTED;
1201         }
1202
1203         if (target->type->hit_watchpoint == NULL) {
1204                 /* For backward compatible, if hit_watchpoint is not implemented,
1205                  * return ERROR_FAIL such that gdb_server will not take the nonsense
1206                  * information. */
1207                 return ERROR_FAIL;
1208         }
1209
1210         return target->type->hit_watchpoint(target, hit_watchpoint);
1211 }
1212
1213 const char *target_get_gdb_arch(struct target *target)
1214 {
1215         if (target->type->get_gdb_arch == NULL)
1216                 return NULL;
1217         return target->type->get_gdb_arch(target);
1218 }
1219
1220 int target_get_gdb_reg_list(struct target *target,
1221                 struct reg **reg_list[], int *reg_list_size,
1222                 enum target_register_class reg_class)
1223 {
1224         return target->type->get_gdb_reg_list(target, reg_list, reg_list_size, reg_class);
1225 }
1226
1227 bool target_supports_gdb_connection(struct target *target)
1228 {
1229         /*
1230          * based on current code, we can simply exclude all the targets that
1231          * don't provide get_gdb_reg_list; this could change with new targets.
1232          */
1233         return !!target->type->get_gdb_reg_list;
1234 }
1235
1236 int target_step(struct target *target,
1237                 int current, target_addr_t address, int handle_breakpoints)
1238 {
1239         return target->type->step(target, current, address, handle_breakpoints);
1240 }
1241
1242 int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
1243 {
1244         if (target->state != TARGET_HALTED) {
1245                 LOG_WARNING("target %s is not halted (gdb fileio)", target->cmd_name);
1246                 return ERROR_TARGET_NOT_HALTED;
1247         }
1248         return target->type->get_gdb_fileio_info(target, fileio_info);
1249 }
1250
1251 int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
1252 {
1253         if (target->state != TARGET_HALTED) {
1254                 LOG_WARNING("target %s is not halted (gdb fileio end)", target->cmd_name);
1255                 return ERROR_TARGET_NOT_HALTED;
1256         }
1257         return target->type->gdb_fileio_end(target, retcode, fileio_errno, ctrl_c);
1258 }
1259
1260 target_addr_t target_address_max(struct target *target)
1261 {
1262         unsigned bits = target_address_bits(target);
1263         if (sizeof(target_addr_t) * 8 == bits)
1264                 return (target_addr_t) -1;
1265         else
1266                 return (((target_addr_t) 1) << bits) - 1;
1267 }
1268
1269 unsigned target_address_bits(struct target *target)
1270 {
1271         if (target->type->address_bits)
1272                 return target->type->address_bits(target);
1273         return 32;
1274 }
1275
1276 int target_profiling(struct target *target, uint32_t *samples,
1277                         uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1278 {
1279         if (target->state != TARGET_HALTED) {
1280                 LOG_WARNING("target %s is not halted (profiling)", target->cmd_name);
1281                 return ERROR_TARGET_NOT_HALTED;
1282         }
1283         return target->type->profiling(target, samples, max_num_samples,
1284                         num_samples, seconds);
1285 }
1286
1287 /**
1288  * Reset the @c examined flag for the given target.
1289  * Pure paranoia -- targets are zeroed on allocation.
1290  */
1291 static void target_reset_examined(struct target *target)
1292 {
1293         target->examined = false;
1294 }
1295
1296 static int handle_target(void *priv);
1297
1298 static int target_init_one(struct command_context *cmd_ctx,
1299                 struct target *target)
1300 {
1301         target_reset_examined(target);
1302
1303         struct target_type *type = target->type;
1304         if (type->examine == NULL)
1305                 type->examine = default_examine;
1306
1307         if (type->check_reset == NULL)
1308                 type->check_reset = default_check_reset;
1309
1310         assert(type->init_target != NULL);
1311
1312         int retval = type->init_target(cmd_ctx, target);
1313         if (ERROR_OK != retval) {
1314                 LOG_ERROR("target '%s' init failed", target_name(target));
1315                 return retval;
1316         }
1317
1318         /* Sanity-check MMU support ... stub in what we must, to help
1319          * implement it in stages, but warn if we need to do so.
1320          */
1321         if (type->mmu) {
1322                 if (type->virt2phys == NULL) {
1323                         LOG_ERROR("type '%s' is missing virt2phys", type->name);
1324                         type->virt2phys = identity_virt2phys;
1325                 }
1326         } else {
1327                 /* Make sure no-MMU targets all behave the same:  make no
1328                  * distinction between physical and virtual addresses, and
1329                  * ensure that virt2phys() is always an identity mapping.
1330                  */
1331                 if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1332                         LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1333
1334                 type->mmu = no_mmu;
1335                 type->write_phys_memory = type->write_memory;
1336                 type->read_phys_memory = type->read_memory;
1337                 type->virt2phys = identity_virt2phys;
1338         }
1339
1340         if (target->type->read_buffer == NULL)
1341                 target->type->read_buffer = target_read_buffer_default;
1342
1343         if (target->type->write_buffer == NULL)
1344                 target->type->write_buffer = target_write_buffer_default;
1345
1346         if (target->type->get_gdb_fileio_info == NULL)
1347                 target->type->get_gdb_fileio_info = target_get_gdb_fileio_info_default;
1348
1349         if (target->type->gdb_fileio_end == NULL)
1350                 target->type->gdb_fileio_end = target_gdb_fileio_end_default;
1351
1352         if (target->type->profiling == NULL)
1353                 target->type->profiling = target_profiling_default;
1354
1355         return ERROR_OK;
1356 }
1357
1358 static int target_init(struct command_context *cmd_ctx)
1359 {
1360         struct target *target;
1361         int retval;
1362
1363         for (target = all_targets; target; target = target->next) {
1364                 retval = target_init_one(cmd_ctx, target);
1365                 if (ERROR_OK != retval)
1366                         return retval;
1367         }
1368
1369         if (!all_targets)
1370                 return ERROR_OK;
1371
1372         retval = target_register_user_commands(cmd_ctx);
1373         if (ERROR_OK != retval)
1374                 return retval;
1375
1376         retval = target_register_timer_callback(&handle_target,
1377                         polling_interval, TARGET_TIMER_TYPE_PERIODIC, cmd_ctx->interp);
1378         if (ERROR_OK != retval)
1379                 return retval;
1380
1381         return ERROR_OK;
1382 }
1383
1384 COMMAND_HANDLER(handle_target_init_command)
1385 {
1386         int retval;
1387
1388         if (CMD_ARGC != 0)
1389                 return ERROR_COMMAND_SYNTAX_ERROR;
1390
1391         static bool target_initialized;
1392         if (target_initialized) {
1393                 LOG_INFO("'target init' has already been called");
1394                 return ERROR_OK;
1395         }
1396         target_initialized = true;
1397
1398         retval = command_run_line(CMD_CTX, "init_targets");
1399         if (ERROR_OK != retval)
1400                 return retval;
1401
1402         retval = command_run_line(CMD_CTX, "init_target_events");
1403         if (ERROR_OK != retval)
1404                 return retval;
1405
1406         retval = command_run_line(CMD_CTX, "init_board");
1407         if (ERROR_OK != retval)
1408                 return retval;
1409
1410         LOG_DEBUG("Initializing targets...");
1411         return target_init(CMD_CTX);
1412 }
1413
1414 int target_register_event_callback(int (*callback)(struct target *target,
1415                 enum target_event event, void *priv), void *priv)
1416 {
1417         struct target_event_callback **callbacks_p = &target_event_callbacks;
1418
1419         if (callback == NULL)
1420                 return ERROR_COMMAND_SYNTAX_ERROR;
1421
1422         if (*callbacks_p) {
1423                 while ((*callbacks_p)->next)
1424                         callbacks_p = &((*callbacks_p)->next);
1425                 callbacks_p = &((*callbacks_p)->next);
1426         }
1427
1428         (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1429         (*callbacks_p)->callback = callback;
1430         (*callbacks_p)->priv = priv;
1431         (*callbacks_p)->next = NULL;
1432
1433         return ERROR_OK;
1434 }
1435
1436 int target_register_reset_callback(int (*callback)(struct target *target,
1437                 enum target_reset_mode reset_mode, void *priv), void *priv)
1438 {
1439         struct target_reset_callback *entry;
1440
1441         if (callback == NULL)
1442                 return ERROR_COMMAND_SYNTAX_ERROR;
1443
1444         entry = malloc(sizeof(struct target_reset_callback));
1445         if (entry == NULL) {
1446                 LOG_ERROR("error allocating buffer for reset callback entry");
1447                 return ERROR_COMMAND_SYNTAX_ERROR;
1448         }
1449
1450         entry->callback = callback;
1451         entry->priv = priv;
1452         list_add(&entry->list, &target_reset_callback_list);
1453
1454
1455         return ERROR_OK;
1456 }
1457
1458 int target_register_trace_callback(int (*callback)(struct target *target,
1459                 size_t len, uint8_t *data, void *priv), void *priv)
1460 {
1461         struct target_trace_callback *entry;
1462
1463         if (callback == NULL)
1464                 return ERROR_COMMAND_SYNTAX_ERROR;
1465
1466         entry = malloc(sizeof(struct target_trace_callback));
1467         if (entry == NULL) {
1468                 LOG_ERROR("error allocating buffer for trace callback entry");
1469                 return ERROR_COMMAND_SYNTAX_ERROR;
1470         }
1471
1472         entry->callback = callback;
1473         entry->priv = priv;
1474         list_add(&entry->list, &target_trace_callback_list);
1475
1476
1477         return ERROR_OK;
1478 }
1479
1480 int target_register_timer_callback(int (*callback)(void *priv),
1481                 unsigned int time_ms, enum target_timer_type type, void *priv)
1482 {
1483         struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1484
1485         if (callback == NULL)
1486                 return ERROR_COMMAND_SYNTAX_ERROR;
1487
1488         if (*callbacks_p) {
1489                 while ((*callbacks_p)->next)
1490                         callbacks_p = &((*callbacks_p)->next);
1491                 callbacks_p = &((*callbacks_p)->next);
1492         }
1493
1494         (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1495         (*callbacks_p)->callback = callback;
1496         (*callbacks_p)->type = type;
1497         (*callbacks_p)->time_ms = time_ms;
1498         (*callbacks_p)->removed = false;
1499
1500         gettimeofday(&(*callbacks_p)->when, NULL);
1501         timeval_add_time(&(*callbacks_p)->when, 0, time_ms * 1000);
1502
1503         (*callbacks_p)->priv = priv;
1504         (*callbacks_p)->next = NULL;
1505
1506         return ERROR_OK;
1507 }
1508
1509 int target_unregister_event_callback(int (*callback)(struct target *target,
1510                 enum target_event event, void *priv), void *priv)
1511 {
1512         struct target_event_callback **p = &target_event_callbacks;
1513         struct target_event_callback *c = target_event_callbacks;
1514
1515         if (callback == NULL)
1516                 return ERROR_COMMAND_SYNTAX_ERROR;
1517
1518         while (c) {
1519                 struct target_event_callback *next = c->next;
1520                 if ((c->callback == callback) && (c->priv == priv)) {
1521                         *p = next;
1522                         free(c);
1523                         return ERROR_OK;
1524                 } else
1525                         p = &(c->next);
1526                 c = next;
1527         }
1528
1529         return ERROR_OK;
1530 }
1531
1532 int target_unregister_reset_callback(int (*callback)(struct target *target,
1533                 enum target_reset_mode reset_mode, void *priv), void *priv)
1534 {
1535         struct target_reset_callback *entry;
1536
1537         if (callback == NULL)
1538                 return ERROR_COMMAND_SYNTAX_ERROR;
1539
1540         list_for_each_entry(entry, &target_reset_callback_list, list) {
1541                 if (entry->callback == callback && entry->priv == priv) {
1542                         list_del(&entry->list);
1543                         free(entry);
1544                         break;
1545                 }
1546         }
1547
1548         return ERROR_OK;
1549 }
1550
1551 int target_unregister_trace_callback(int (*callback)(struct target *target,
1552                 size_t len, uint8_t *data, void *priv), void *priv)
1553 {
1554         struct target_trace_callback *entry;
1555
1556         if (callback == NULL)
1557                 return ERROR_COMMAND_SYNTAX_ERROR;
1558
1559         list_for_each_entry(entry, &target_trace_callback_list, list) {
1560                 if (entry->callback == callback && entry->priv == priv) {
1561                         list_del(&entry->list);
1562                         free(entry);
1563                         break;
1564                 }
1565         }
1566
1567         return ERROR_OK;
1568 }
1569
1570 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1571 {
1572         if (callback == NULL)
1573                 return ERROR_COMMAND_SYNTAX_ERROR;
1574
1575         for (struct target_timer_callback *c = target_timer_callbacks;
1576              c; c = c->next) {
1577                 if ((c->callback == callback) && (c->priv == priv)) {
1578                         c->removed = true;
1579                         return ERROR_OK;
1580                 }
1581         }
1582
1583         return ERROR_FAIL;
1584 }
1585
1586 int target_call_event_callbacks(struct target *target, enum target_event event)
1587 {
1588         struct target_event_callback *callback = target_event_callbacks;
1589         struct target_event_callback *next_callback;
1590
1591         if (event == TARGET_EVENT_HALTED) {
1592                 /* execute early halted first */
1593                 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1594         }
1595
1596         LOG_DEBUG("target event %i (%s)", event,
1597                         Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1598
1599         target_handle_event(target, event);
1600
1601         while (callback) {
1602                 next_callback = callback->next;
1603                 callback->callback(target, event, callback->priv);
1604                 callback = next_callback;
1605         }
1606
1607         return ERROR_OK;
1608 }
1609
1610 int target_call_reset_callbacks(struct target *target, enum target_reset_mode reset_mode)
1611 {
1612         struct target_reset_callback *callback;
1613
1614         LOG_DEBUG("target reset %i (%s)", reset_mode,
1615                         Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode)->name);
1616
1617         list_for_each_entry(callback, &target_reset_callback_list, list)
1618                 callback->callback(target, reset_mode, callback->priv);
1619
1620         return ERROR_OK;
1621 }
1622
1623 int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data)
1624 {
1625         struct target_trace_callback *callback;
1626
1627         list_for_each_entry(callback, &target_trace_callback_list, list)
1628                 callback->callback(target, len, data, callback->priv);
1629
1630         return ERROR_OK;
1631 }
1632
1633 static int target_timer_callback_periodic_restart(
1634                 struct target_timer_callback *cb, struct timeval *now)
1635 {
1636         cb->when = *now;
1637         timeval_add_time(&cb->when, 0, cb->time_ms * 1000L);
1638         return ERROR_OK;
1639 }
1640
1641 static int target_call_timer_callback(struct target_timer_callback *cb,
1642                 struct timeval *now)
1643 {
1644         cb->callback(cb->priv);
1645
1646         if (cb->type == TARGET_TIMER_TYPE_PERIODIC)
1647                 return target_timer_callback_periodic_restart(cb, now);
1648
1649         return target_unregister_timer_callback(cb->callback, cb->priv);
1650 }
1651
1652 static int target_call_timer_callbacks_check_time(int checktime)
1653 {
1654         static bool callback_processing;
1655
1656         /* Do not allow nesting */
1657         if (callback_processing)
1658                 return ERROR_OK;
1659
1660         callback_processing = true;
1661
1662         keep_alive();
1663
1664         struct timeval now;
1665         gettimeofday(&now, NULL);
1666
1667         /* Store an address of the place containing a pointer to the
1668          * next item; initially, that's a standalone "root of the
1669          * list" variable. */
1670         struct target_timer_callback **callback = &target_timer_callbacks;
1671         while (*callback) {
1672                 if ((*callback)->removed) {
1673                         struct target_timer_callback *p = *callback;
1674                         *callback = (*callback)->next;
1675                         free(p);
1676                         continue;
1677                 }
1678
1679                 bool call_it = (*callback)->callback &&
1680                         ((!checktime && (*callback)->type == TARGET_TIMER_TYPE_PERIODIC) ||
1681                          timeval_compare(&now, &(*callback)->when) >= 0);
1682
1683                 if (call_it)
1684                         target_call_timer_callback(*callback, &now);
1685
1686                 callback = &(*callback)->next;
1687         }
1688
1689         callback_processing = false;
1690         return ERROR_OK;
1691 }
1692
1693 int target_call_timer_callbacks(void)
1694 {
1695         return target_call_timer_callbacks_check_time(1);
1696 }
1697
1698 /* invoke periodic callbacks immediately */
1699 int target_call_timer_callbacks_now(void)
1700 {
1701         return target_call_timer_callbacks_check_time(0);
1702 }
1703
1704 /* Prints the working area layout for debug purposes */
1705 static void print_wa_layout(struct target *target)
1706 {
1707         struct working_area *c = target->working_areas;
1708
1709         while (c) {
1710                 LOG_DEBUG("%c%c " TARGET_ADDR_FMT "-" TARGET_ADDR_FMT " (%" PRIu32 " bytes)",
1711                         c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1712                         c->address, c->address + c->size - 1, c->size);
1713                 c = c->next;
1714         }
1715 }
1716
1717 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1718 static void target_split_working_area(struct working_area *area, uint32_t size)
1719 {
1720         assert(area->free); /* Shouldn't split an allocated area */
1721         assert(size <= area->size); /* Caller should guarantee this */
1722
1723         /* Split only if not already the right size */
1724         if (size < area->size) {
1725                 struct working_area *new_wa = malloc(sizeof(*new_wa));
1726
1727                 if (new_wa == NULL)
1728                         return;
1729
1730                 new_wa->next = area->next;
1731                 new_wa->size = area->size - size;
1732                 new_wa->address = area->address + size;
1733                 new_wa->backup = NULL;
1734                 new_wa->user = NULL;
1735                 new_wa->free = true;
1736
1737                 area->next = new_wa;
1738                 area->size = size;
1739
1740                 /* If backup memory was allocated to this area, it has the wrong size
1741                  * now so free it and it will be reallocated if/when needed */
1742                 if (area->backup) {
1743                         free(area->backup);
1744                         area->backup = NULL;
1745                 }
1746         }
1747 }
1748
1749 /* Merge all adjacent free areas into one */
1750 static void target_merge_working_areas(struct target *target)
1751 {
1752         struct working_area *c = target->working_areas;
1753
1754         while (c && c->next) {
1755                 assert(c->next->address == c->address + c->size); /* This is an invariant */
1756
1757                 /* Find two adjacent free areas */
1758                 if (c->free && c->next->free) {
1759                         /* Merge the last into the first */
1760                         c->size += c->next->size;
1761
1762                         /* Remove the last */
1763                         struct working_area *to_be_freed = c->next;
1764                         c->next = c->next->next;
1765                         if (to_be_freed->backup)
1766                                 free(to_be_freed->backup);
1767                         free(to_be_freed);
1768
1769                         /* If backup memory was allocated to the remaining area, it's has
1770                          * the wrong size now */
1771                         if (c->backup) {
1772                                 free(c->backup);
1773                                 c->backup = NULL;
1774                         }
1775                 } else {
1776                         c = c->next;
1777                 }
1778         }
1779 }
1780
1781 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1782 {
1783         /* Reevaluate working area address based on MMU state*/
1784         if (target->working_areas == NULL) {
1785                 int retval;
1786                 int enabled;
1787
1788                 retval = target->type->mmu(target, &enabled);
1789                 if (retval != ERROR_OK)
1790                         return retval;
1791
1792                 if (!enabled) {
1793                         if (target->working_area_phys_spec) {
1794                                 LOG_DEBUG("MMU disabled, using physical "
1795                                         "address for working memory " TARGET_ADDR_FMT,
1796                                         target->working_area_phys);
1797                                 target->working_area = target->working_area_phys;
1798                         } else {
1799                                 LOG_ERROR("No working memory available. "
1800                                         "Specify -work-area-phys to target.");
1801                                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1802                         }
1803                 } else {
1804                         if (target->working_area_virt_spec) {
1805                                 LOG_DEBUG("MMU enabled, using virtual "
1806                                         "address for working memory " TARGET_ADDR_FMT,
1807                                         target->working_area_virt);
1808                                 target->working_area = target->working_area_virt;
1809                         } else {
1810                                 LOG_ERROR("No working memory available. "
1811                                         "Specify -work-area-virt to target.");
1812                                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1813                         }
1814                 }
1815
1816                 /* Set up initial working area on first call */
1817                 struct working_area *new_wa = malloc(sizeof(*new_wa));
1818                 if (new_wa) {
1819                         new_wa->next = NULL;
1820                         new_wa->size = target->working_area_size & ~3UL; /* 4-byte align */
1821                         new_wa->address = target->working_area;
1822                         new_wa->backup = NULL;
1823                         new_wa->user = NULL;
1824                         new_wa->free = true;
1825                 }
1826
1827                 target->working_areas = new_wa;
1828         }
1829
1830         /* only allocate multiples of 4 byte */
1831         if (size % 4)
1832                 size = (size + 3) & (~3UL);
1833
1834         struct working_area *c = target->working_areas;
1835
1836         /* Find the first large enough working area */
1837         while (c) {
1838                 if (c->free && c->size >= size)
1839                         break;
1840                 c = c->next;
1841         }
1842
1843         if (c == NULL)
1844                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1845
1846         /* Split the working area into the requested size */
1847         target_split_working_area(c, size);
1848
1849         LOG_DEBUG("allocated new working area of %" PRIu32 " bytes at address " TARGET_ADDR_FMT,
1850                           size, c->address);
1851
1852         if (target->backup_working_area) {
1853                 if (c->backup == NULL) {
1854                         c->backup = malloc(c->size);
1855                         if (c->backup == NULL)
1856                                 return ERROR_FAIL;
1857                 }
1858
1859                 int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
1860                 if (retval != ERROR_OK)
1861                         return retval;
1862         }
1863
1864         /* mark as used, and return the new (reused) area */
1865         c->free = false;
1866         *area = c;
1867
1868         /* user pointer */
1869         c->user = area;
1870
1871         print_wa_layout(target);
1872
1873         return ERROR_OK;
1874 }
1875
1876 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1877 {
1878         int retval;
1879
1880         retval = target_alloc_working_area_try(target, size, area);
1881         if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1882                 LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
1883         return retval;
1884
1885 }
1886
1887 static int target_restore_working_area(struct target *target, struct working_area *area)
1888 {
1889         int retval = ERROR_OK;
1890
1891         if (target->backup_working_area && area->backup != NULL) {
1892                 retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
1893                 if (retval != ERROR_OK)
1894                         LOG_ERROR("failed to restore %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
1895                                         area->size, area->address);
1896         }
1897
1898         return retval;
1899 }
1900
1901 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1902 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1903 {
1904         int retval = ERROR_OK;
1905
1906         if (area->free)
1907                 return retval;
1908
1909         if (restore) {
1910                 retval = target_restore_working_area(target, area);
1911                 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1912                 if (retval != ERROR_OK)
1913                         return retval;
1914         }
1915
1916         area->free = true;
1917
1918         LOG_DEBUG("freed %" PRIu32 " bytes of working area at address " TARGET_ADDR_FMT,
1919                         area->size, area->address);
1920
1921         /* mark user pointer invalid */
1922         /* TODO: Is this really safe? It points to some previous caller's memory.
1923          * How could we know that the area pointer is still in that place and not
1924          * some other vital data? What's the purpose of this, anyway? */
1925         *area->user = NULL;
1926         area->user = NULL;
1927
1928         target_merge_working_areas(target);
1929
1930         print_wa_layout(target);
1931
1932         return retval;
1933 }
1934
1935 int target_free_working_area(struct target *target, struct working_area *area)
1936 {
1937         return target_free_working_area_restore(target, area, 1);
1938 }
1939
1940 /* free resources and restore memory, if restoring memory fails,
1941  * free up resources anyway
1942  */
1943 static void target_free_all_working_areas_restore(struct target *target, int restore)
1944 {
1945         struct working_area *c = target->working_areas;
1946
1947         LOG_DEBUG("freeing all working areas");
1948
1949         /* Loop through all areas, restoring the allocated ones and marking them as free */
1950         while (c) {
1951                 if (!c->free) {
1952                         if (restore)
1953                                 target_restore_working_area(target, c);
1954                         c->free = true;
1955                         *c->user = NULL; /* Same as above */
1956                         c->user = NULL;
1957                 }
1958                 c = c->next;
1959         }
1960
1961         /* Run a merge pass to combine all areas into one */
1962         target_merge_working_areas(target);
1963
1964         print_wa_layout(target);
1965 }
1966
1967 void target_free_all_working_areas(struct target *target)
1968 {
1969         target_free_all_working_areas_restore(target, 1);
1970
1971         /* Now we have none or only one working area marked as free */
1972         if (target->working_areas) {
1973                 /* Free the last one to allow on-the-fly moving and resizing */
1974                 free(target->working_areas->backup);
1975                 free(target->working_areas);
1976                 target->working_areas = NULL;
1977         }
1978 }
1979
1980 /* Find the largest number of bytes that can be allocated */
1981 uint32_t target_get_working_area_avail(struct target *target)
1982 {
1983         struct working_area *c = target->working_areas;
1984         uint32_t max_size = 0;
1985
1986         if (c == NULL)
1987                 return target->working_area_size;
1988
1989         while (c) {
1990                 if (c->free && max_size < c->size)
1991                         max_size = c->size;
1992
1993                 c = c->next;
1994         }
1995
1996         return max_size;
1997 }
1998
1999 static void target_destroy(struct target *target)
2000 {
2001         if (target->type->deinit_target)
2002                 target->type->deinit_target(target);
2003
2004         if (target->semihosting)
2005                 free(target->semihosting);
2006
2007         jtag_unregister_event_callback(jtag_enable_callback, target);
2008
2009         struct target_event_action *teap = target->event_action;
2010         while (teap) {
2011                 struct target_event_action *next = teap->next;
2012                 Jim_DecrRefCount(teap->interp, teap->body);
2013                 free(teap);
2014                 teap = next;
2015         }
2016
2017         target_free_all_working_areas(target);
2018
2019         /* release the targets SMP list */
2020         if (target->smp) {
2021                 struct target_list *head = target->head;
2022                 while (head != NULL) {
2023                         struct target_list *pos = head->next;
2024                         head->target->smp = 0;
2025                         free(head);
2026                         head = pos;
2027                 }
2028                 target->smp = 0;
2029         }
2030
2031         free(target->gdb_port_override);
2032         free(target->type);
2033         free(target->trace_info);
2034         free(target->fileio_info);
2035         free(target->cmd_name);
2036         free(target);
2037 }
2038
2039 void target_quit(void)
2040 {
2041         struct target_event_callback *pe = target_event_callbacks;
2042         while (pe) {
2043                 struct target_event_callback *t = pe->next;
2044                 free(pe);
2045                 pe = t;
2046         }
2047         target_event_callbacks = NULL;
2048
2049         struct target_timer_callback *pt = target_timer_callbacks;
2050         while (pt) {
2051                 struct target_timer_callback *t = pt->next;
2052                 free(pt);
2053                 pt = t;
2054         }
2055         target_timer_callbacks = NULL;
2056
2057         for (struct target *target = all_targets; target;) {
2058                 struct target *tmp;
2059
2060                 tmp = target->next;
2061                 target_destroy(target);
2062                 target = tmp;
2063         }
2064
2065         all_targets = NULL;
2066 }
2067
2068 int target_arch_state(struct target *target)
2069 {
2070         int retval;
2071         if (target == NULL) {
2072                 LOG_WARNING("No target has been configured");
2073                 return ERROR_OK;
2074         }
2075
2076         if (target->state != TARGET_HALTED)
2077                 return ERROR_OK;
2078
2079         retval = target->type->arch_state(target);
2080         return retval;
2081 }
2082
2083 static int target_get_gdb_fileio_info_default(struct target *target,
2084                 struct gdb_fileio_info *fileio_info)
2085 {
2086         /* If target does not support semi-hosting function, target
2087            has no need to provide .get_gdb_fileio_info callback.
2088            It just return ERROR_FAIL and gdb_server will return "Txx"
2089            as target halted every time.  */
2090         return ERROR_FAIL;
2091 }
2092
2093 static int target_gdb_fileio_end_default(struct target *target,
2094                 int retcode, int fileio_errno, bool ctrl_c)
2095 {
2096         return ERROR_OK;
2097 }
2098
2099 static int target_profiling_default(struct target *target, uint32_t *samples,
2100                 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
2101 {
2102         struct timeval timeout, now;
2103
2104         gettimeofday(&timeout, NULL);
2105         timeval_add_time(&timeout, seconds, 0);
2106
2107         LOG_INFO("Starting profiling. Halting and resuming the"
2108                         " target as often as we can...");
2109
2110         uint32_t sample_count = 0;
2111         /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2112         struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
2113
2114         int retval = ERROR_OK;
2115         for (;;) {
2116                 target_poll(target);
2117                 if (target->state == TARGET_HALTED) {
2118                         uint32_t t = buf_get_u32(reg->value, 0, 32);
2119                         samples[sample_count++] = t;
2120                         /* current pc, addr = 0, do not handle breakpoints, not debugging */
2121                         retval = target_resume(target, 1, 0, 0, 0);
2122                         target_poll(target);
2123                         alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2124                 } else if (target->state == TARGET_RUNNING) {
2125                         /* We want to quickly sample the PC. */
2126                         retval = target_halt(target);
2127                 } else {
2128                         LOG_INFO("Target not halted or running");
2129                         retval = ERROR_OK;
2130                         break;
2131                 }
2132
2133                 if (retval != ERROR_OK)
2134                         break;
2135
2136                 gettimeofday(&now, NULL);
2137                 if ((sample_count >= max_num_samples) || timeval_compare(&now, &timeout) >= 0) {
2138                         LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
2139                         break;
2140                 }
2141         }
2142
2143         *num_samples = sample_count;
2144         return retval;
2145 }
2146
2147 /* Single aligned words are guaranteed to use 16 or 32 bit access
2148  * mode respectively, otherwise data is handled as quickly as
2149  * possible
2150  */
2151 int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
2152 {
2153         LOG_DEBUG("writing buffer of %" PRIi32 " byte at " TARGET_ADDR_FMT,
2154                           size, address);
2155
2156         if (!target_was_examined(target)) {
2157                 LOG_ERROR("Target not examined yet");
2158                 return ERROR_FAIL;
2159         }
2160
2161         if (size == 0)
2162                 return ERROR_OK;
2163
2164         if ((address + size - 1) < address) {
2165                 /* GDB can request this when e.g. PC is 0xfffffffc */
2166                 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2167                                   address,
2168                                   size);
2169                 return ERROR_FAIL;
2170         }
2171
2172         return target->type->write_buffer(target, address, size, buffer);
2173 }
2174
2175 static int target_write_buffer_default(struct target *target,
2176         target_addr_t address, uint32_t count, const uint8_t *buffer)
2177 {
2178         uint32_t size;
2179
2180         /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2181          * will have something to do with the size we leave to it. */
2182         for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2183                 if (address & size) {
2184                         int retval = target_write_memory(target, address, size, 1, buffer);
2185                         if (retval != ERROR_OK)
2186                                 return retval;
2187                         address += size;
2188                         count -= size;
2189                         buffer += size;
2190                 }
2191         }
2192
2193         /* Write the data with as large access size as possible. */
2194         for (; size > 0; size /= 2) {
2195                 uint32_t aligned = count - count % size;
2196                 if (aligned > 0) {
2197                         int retval = target_write_memory(target, address, size, aligned / size, buffer);
2198                         if (retval != ERROR_OK)
2199                                 return retval;
2200                         address += aligned;
2201                         count -= aligned;
2202                         buffer += aligned;
2203                 }
2204         }
2205
2206         return ERROR_OK;
2207 }
2208
2209 /* Single aligned words are guaranteed to use 16 or 32 bit access
2210  * mode respectively, otherwise data is handled as quickly as
2211  * possible
2212  */
2213 int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
2214 {
2215         LOG_DEBUG("reading buffer of %" PRIi32 " byte at " TARGET_ADDR_FMT,
2216                           size, address);
2217
2218         if (!target_was_examined(target)) {
2219                 LOG_ERROR("Target not examined yet");
2220                 return ERROR_FAIL;
2221         }
2222
2223         if (size == 0)
2224                 return ERROR_OK;
2225
2226         if ((address + size - 1) < address) {
2227                 /* GDB can request this when e.g. PC is 0xfffffffc */
2228                 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT ", 0x%08" PRIx32 ")",
2229                                   address,
2230                                   size);
2231                 return ERROR_FAIL;
2232         }
2233
2234         return target->type->read_buffer(target, address, size, buffer);
2235 }
2236
2237 static int target_read_buffer_default(struct target *target, target_addr_t address, uint32_t count, uint8_t *buffer)
2238 {
2239         uint32_t size;
2240
2241         /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2242          * will have something to do with the size we leave to it. */
2243         for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
2244                 if (address & size) {
2245                         int retval = target_read_memory(target, address, size, 1, buffer);
2246                         if (retval != ERROR_OK)
2247                                 return retval;
2248                         address += size;
2249                         count -= size;
2250                         buffer += size;
2251                 }
2252         }
2253
2254         /* Read the data with as large access size as possible. */
2255         for (; size > 0; size /= 2) {
2256                 uint32_t aligned = count - count % size;
2257                 if (aligned > 0) {
2258                         int retval = target_read_memory(target, address, size, aligned / size, buffer);
2259                         if (retval != ERROR_OK)
2260                                 return retval;
2261                         address += aligned;
2262                         count -= aligned;
2263                         buffer += aligned;
2264                 }
2265         }
2266
2267         return ERROR_OK;
2268 }
2269
2270 int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t* crc)
2271 {
2272         uint8_t *buffer;
2273         int retval;
2274         uint32_t i;
2275         uint32_t checksum = 0;
2276         if (!target_was_examined(target)) {
2277                 LOG_ERROR("Target not examined yet");
2278                 return ERROR_FAIL;
2279         }
2280
2281         retval = target->type->checksum_memory(target, address, size, &checksum);
2282         if (retval != ERROR_OK) {
2283                 buffer = malloc(size);
2284                 if (buffer == NULL) {
2285                         LOG_ERROR("error allocating buffer for section (%" PRId32 " bytes)", size);
2286                         return ERROR_COMMAND_SYNTAX_ERROR;
2287                 }
2288                 retval = target_read_buffer(target, address, size, buffer);
2289                 if (retval != ERROR_OK) {
2290                         free(buffer);
2291                         return retval;
2292                 }
2293
2294                 /* convert to target endianness */
2295                 for (i = 0; i < (size/sizeof(uint32_t)); i++) {
2296                         uint32_t target_data;
2297                         target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
2298                         target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
2299                 }
2300
2301                 retval = image_calculate_checksum(buffer, size, &checksum);
2302                 free(buffer);
2303         }
2304
2305         *crc = checksum;
2306
2307         return retval;
2308 }
2309
2310 int target_blank_check_memory(struct target *target,
2311         struct target_memory_check_block *blocks, int num_blocks,
2312         uint8_t erased_value)
2313 {
2314         if (!target_was_examined(target)) {
2315                 LOG_ERROR("Target not examined yet");
2316                 return ERROR_FAIL;
2317         }
2318
2319         if (target->type->blank_check_memory == NULL)
2320                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2321
2322         return target->type->blank_check_memory(target, blocks, num_blocks, erased_value);
2323 }
2324
2325 int target_read_u64(struct target *target, target_addr_t address, uint64_t *value)
2326 {
2327         uint8_t value_buf[8];
2328         if (!target_was_examined(target)) {
2329                 LOG_ERROR("Target not examined yet");
2330                 return ERROR_FAIL;
2331         }
2332
2333         int retval = target_read_memory(target, address, 8, 1, value_buf);
2334
2335         if (retval == ERROR_OK) {
2336                 *value = target_buffer_get_u64(target, value_buf);
2337                 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2338                                   address,
2339                                   *value);
2340         } else {
2341                 *value = 0x0;
2342                 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2343                                   address);
2344         }
2345
2346         return retval;
2347 }
2348
2349 int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
2350 {
2351         uint8_t value_buf[4];
2352         if (!target_was_examined(target)) {
2353                 LOG_ERROR("Target not examined yet");
2354                 return ERROR_FAIL;
2355         }
2356
2357         int retval = target_read_memory(target, address, 4, 1, value_buf);
2358
2359         if (retval == ERROR_OK) {
2360                 *value = target_buffer_get_u32(target, value_buf);
2361                 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2362                                   address,
2363                                   *value);
2364         } else {
2365                 *value = 0x0;
2366                 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2367                                   address);
2368         }
2369
2370         return retval;
2371 }
2372
2373 int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
2374 {
2375         uint8_t value_buf[2];
2376         if (!target_was_examined(target)) {
2377                 LOG_ERROR("Target not examined yet");
2378                 return ERROR_FAIL;
2379         }
2380
2381         int retval = target_read_memory(target, address, 2, 1, value_buf);
2382
2383         if (retval == ERROR_OK) {
2384                 *value = target_buffer_get_u16(target, value_buf);
2385                 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%4.4" PRIx16,
2386                                   address,
2387                                   *value);
2388         } else {
2389                 *value = 0x0;
2390                 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2391                                   address);
2392         }
2393
2394         return retval;
2395 }
2396
2397 int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
2398 {
2399         if (!target_was_examined(target)) {
2400                 LOG_ERROR("Target not examined yet");
2401                 return ERROR_FAIL;
2402         }
2403
2404         int retval = target_read_memory(target, address, 1, 1, value);
2405
2406         if (retval == ERROR_OK) {
2407                 LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2408                                   address,
2409                                   *value);
2410         } else {
2411                 *value = 0x0;
2412                 LOG_DEBUG("address: " TARGET_ADDR_FMT " failed",
2413                                   address);
2414         }
2415
2416         return retval;
2417 }
2418
2419 int target_write_u64(struct target *target, target_addr_t address, uint64_t value)
2420 {
2421         int retval;
2422         uint8_t value_buf[8];
2423         if (!target_was_examined(target)) {
2424                 LOG_ERROR("Target not examined yet");
2425                 return ERROR_FAIL;
2426         }
2427
2428         LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2429                           address,
2430                           value);
2431
2432         target_buffer_set_u64(target, value_buf, value);
2433         retval = target_write_memory(target, address, 8, 1, value_buf);
2434         if (retval != ERROR_OK)
2435                 LOG_DEBUG("failed: %i", retval);
2436
2437         return retval;
2438 }
2439
2440 int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
2441 {
2442         int retval;
2443         uint8_t value_buf[4];
2444         if (!target_was_examined(target)) {
2445                 LOG_ERROR("Target not examined yet");
2446                 return ERROR_FAIL;
2447         }
2448
2449         LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2450                           address,
2451                           value);
2452
2453         target_buffer_set_u32(target, value_buf, value);
2454         retval = target_write_memory(target, address, 4, 1, value_buf);
2455         if (retval != ERROR_OK)
2456                 LOG_DEBUG("failed: %i", retval);
2457
2458         return retval;
2459 }
2460
2461 int target_write_u16(struct target *target, target_addr_t address, uint16_t value)
2462 {
2463         int retval;
2464         uint8_t value_buf[2];
2465         if (!target_was_examined(target)) {
2466                 LOG_ERROR("Target not examined yet");
2467                 return ERROR_FAIL;
2468         }
2469
2470         LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2471                           address,
2472                           value);
2473
2474         target_buffer_set_u16(target, value_buf, value);
2475         retval = target_write_memory(target, address, 2, 1, value_buf);
2476         if (retval != ERROR_OK)
2477                 LOG_DEBUG("failed: %i", retval);
2478
2479         return retval;
2480 }
2481
2482 int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
2483 {
2484         int retval;
2485         if (!target_was_examined(target)) {
2486                 LOG_ERROR("Target not examined yet");
2487                 return ERROR_FAIL;
2488         }
2489
2490         LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2491                           address, value);
2492
2493         retval = target_write_memory(target, address, 1, 1, &value);
2494         if (retval != ERROR_OK)
2495                 LOG_DEBUG("failed: %i", retval);
2496
2497         return retval;
2498 }
2499
2500 int target_write_phys_u64(struct target *target, target_addr_t address, uint64_t value)
2501 {
2502         int retval;
2503         uint8_t value_buf[8];
2504         if (!target_was_examined(target)) {
2505                 LOG_ERROR("Target not examined yet");
2506                 return ERROR_FAIL;
2507         }
2508
2509         LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%16.16" PRIx64 "",
2510                           address,
2511                           value);
2512
2513         target_buffer_set_u64(target, value_buf, value);
2514         retval = target_write_phys_memory(target, address, 8, 1, value_buf);
2515         if (retval != ERROR_OK)
2516                 LOG_DEBUG("failed: %i", retval);
2517
2518         return retval;
2519 }
2520
2521 int target_write_phys_u32(struct target *target, target_addr_t address, uint32_t value)
2522 {
2523         int retval;
2524         uint8_t value_buf[4];
2525         if (!target_was_examined(target)) {
2526                 LOG_ERROR("Target not examined yet");
2527                 return ERROR_FAIL;
2528         }
2529
2530         LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx32 "",
2531                           address,
2532                           value);
2533
2534         target_buffer_set_u32(target, value_buf, value);
2535         retval = target_write_phys_memory(target, address, 4, 1, value_buf);
2536         if (retval != ERROR_OK)
2537                 LOG_DEBUG("failed: %i", retval);
2538
2539         return retval;
2540 }
2541
2542 int target_write_phys_u16(struct target *target, target_addr_t address, uint16_t value)
2543 {
2544         int retval;
2545         uint8_t value_buf[2];
2546         if (!target_was_examined(target)) {
2547                 LOG_ERROR("Target not examined yet");
2548                 return ERROR_FAIL;
2549         }
2550
2551         LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%8.8" PRIx16,
2552                           address,
2553                           value);
2554
2555         target_buffer_set_u16(target, value_buf, value);
2556         retval = target_write_phys_memory(target, address, 2, 1, value_buf);
2557         if (retval != ERROR_OK)
2558                 LOG_DEBUG("failed: %i", retval);
2559
2560         return retval;
2561 }
2562
2563 int target_write_phys_u8(struct target *target, target_addr_t address, uint8_t value)
2564 {
2565         int retval;
2566         if (!target_was_examined(target)) {
2567                 LOG_ERROR("Target not examined yet");
2568                 return ERROR_FAIL;
2569         }
2570
2571         LOG_DEBUG("address: " TARGET_ADDR_FMT ", value: 0x%2.2" PRIx8,
2572                           address, value);
2573
2574         retval = target_write_phys_memory(target, address, 1, 1, &value);
2575         if (retval != ERROR_OK)
2576                 LOG_DEBUG("failed: %i", retval);
2577
2578         return retval;
2579 }
2580
2581 static int find_target(struct command_context *cmd_ctx, const char *name)
2582 {
2583         struct target *target = get_target(name);
2584         if (target == NULL) {
2585                 LOG_ERROR("Target: %s is unknown, try one of:\n", name);
2586                 return ERROR_FAIL;
2587         }
2588         if (!target->tap->enabled) {
2589                 LOG_USER("Target: TAP %s is disabled, "
2590                          "can't be the current target\n",
2591                          target->tap->dotted_name);
2592                 return ERROR_FAIL;
2593         }
2594
2595         cmd_ctx->current_target = target;
2596         if (cmd_ctx->current_target_override)
2597                 cmd_ctx->current_target_override = target;
2598
2599         return ERROR_OK;
2600 }
2601
2602
2603 COMMAND_HANDLER(handle_targets_command)
2604 {
2605         int retval = ERROR_OK;
2606         if (CMD_ARGC == 1) {
2607                 retval = find_target(CMD_CTX, CMD_ARGV[0]);
2608                 if (retval == ERROR_OK) {
2609                         /* we're done! */
2610                         return retval;
2611                 }
2612         }
2613
2614         struct target *target = all_targets;
2615         command_print(CMD_CTX, "    TargetName         Type       Endian TapName            State       ");
2616         command_print(CMD_CTX, "--  ------------------ ---------- ------ ------------------ ------------");
2617         while (target) {
2618                 const char *state;
2619                 char marker = ' ';
2620
2621                 if (target->tap->enabled)
2622                         state = target_state_name(target);
2623                 else
2624                         state = "tap-disabled";
2625
2626                 if (CMD_CTX->current_target == target)
2627                         marker = '*';
2628
2629                 /* keep columns lined up to match the headers above */
2630                 command_print(CMD_CTX,
2631                                 "%2d%c %-18s %-10s %-6s %-18s %s",
2632                                 target->target_number,
2633                                 marker,
2634                                 target_name(target),
2635                                 target_type_name(target),
2636                                 Jim_Nvp_value2name_simple(nvp_target_endian,
2637                                         target->endianness)->name,
2638                                 target->tap->dotted_name,
2639                                 state);
2640                 target = target->next;
2641         }
2642
2643         return retval;
2644 }
2645
2646 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2647
2648 static int powerDropout;
2649 static int srstAsserted;
2650
2651 static int runPowerRestore;
2652 static int runPowerDropout;
2653 static int runSrstAsserted;
2654 static int runSrstDeasserted;
2655
2656 static int sense_handler(void)
2657 {
2658         static int prevSrstAsserted;
2659         static int prevPowerdropout;
2660
2661         int retval = jtag_power_dropout(&powerDropout);
2662         if (retval != ERROR_OK)
2663                 return retval;
2664
2665         int powerRestored;
2666         powerRestored = prevPowerdropout && !powerDropout;
2667         if (powerRestored)
2668                 runPowerRestore = 1;
2669
2670         int64_t current = timeval_ms();
2671         static int64_t lastPower;
2672         bool waitMore = lastPower + 2000 > current;
2673         if (powerDropout && !waitMore) {
2674                 runPowerDropout = 1;
2675                 lastPower = current;
2676         }
2677
2678         retval = jtag_srst_asserted(&srstAsserted);
2679         if (retval != ERROR_OK)
2680                 return retval;
2681
2682         int srstDeasserted;
2683         srstDeasserted = prevSrstAsserted && !srstAsserted;
2684
2685         static int64_t lastSrst;
2686         waitMore = lastSrst + 2000 > current;
2687         if (srstDeasserted && !waitMore) {
2688                 runSrstDeasserted = 1;
2689                 lastSrst = current;
2690         }
2691
2692         if (!prevSrstAsserted && srstAsserted)
2693                 runSrstAsserted = 1;
2694
2695         prevSrstAsserted = srstAsserted;
2696         prevPowerdropout = powerDropout;
2697
2698         if (srstDeasserted || powerRestored) {
2699                 /* Other than logging the event we can't do anything here.
2700                  * Issuing a reset is a particularly bad idea as we might
2701                  * be inside a reset already.
2702                  */
2703         }
2704
2705         return ERROR_OK;
2706 }
2707
2708 /* process target state changes */
2709 static int handle_target(void *priv)
2710 {
2711         Jim_Interp *interp = (Jim_Interp *)priv;
2712         int retval = ERROR_OK;
2713
2714         if (!is_jtag_poll_safe()) {
2715                 /* polling is disabled currently */
2716                 return ERROR_OK;
2717         }
2718
2719         /* we do not want to recurse here... */
2720         static int recursive;
2721         if (!recursive) {
2722                 recursive = 1;
2723                 sense_handler();
2724                 /* danger! running these procedures can trigger srst assertions and power dropouts.
2725                  * We need to avoid an infinite loop/recursion here and we do that by
2726                  * clearing the flags after running these events.
2727                  */
2728                 int did_something = 0;
2729                 if (runSrstAsserted) {
2730                         LOG_INFO("srst asserted detected, running srst_asserted proc.");
2731                         Jim_Eval(interp, "srst_asserted");
2732                         did_something = 1;
2733                 }
2734                 if (runSrstDeasserted) {
2735                         Jim_Eval(interp, "srst_deasserted");
2736                         did_something = 1;
2737                 }
2738                 if (runPowerDropout) {
2739                         LOG_INFO("Power dropout detected, running power_dropout proc.");
2740                         Jim_Eval(interp, "power_dropout");
2741                         did_something = 1;
2742                 }
2743                 if (runPowerRestore) {
2744                         Jim_Eval(interp, "power_restore");
2745                         did_something = 1;
2746                 }
2747
2748                 if (did_something) {
2749                         /* clear detect flags */
2750                         sense_handler();
2751                 }
2752
2753                 /* clear action flags */
2754
2755                 runSrstAsserted = 0;
2756                 runSrstDeasserted = 0;
2757                 runPowerRestore = 0;
2758                 runPowerDropout = 0;
2759
2760                 recursive = 0;
2761         }
2762
2763         /* Poll targets for state changes unless that's globally disabled.
2764          * Skip targets that are currently disabled.
2765          */
2766         for (struct target *target = all_targets;
2767                         is_jtag_poll_safe() && target;
2768                         target = target->next) {
2769
2770                 if (!target_was_examined(target))
2771                         continue;
2772
2773                 if (!target->tap->enabled)
2774                         continue;
2775
2776                 if (target->backoff.times > target->backoff.count) {
2777                         /* do not poll this time as we failed previously */
2778                         target->backoff.count++;
2779                         continue;
2780                 }
2781                 target->backoff.count = 0;
2782
2783                 /* only poll target if we've got power and srst isn't asserted */
2784                 if (!powerDropout && !srstAsserted) {
2785                         /* polling may fail silently until the target has been examined */
2786                         retval = target_poll(target);
2787                         if (retval != ERROR_OK) {
2788                                 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2789                                 if (target->backoff.times * polling_interval < 5000) {
2790                                         target->backoff.times *= 2;
2791                                         target->backoff.times++;
2792                                 }
2793
2794                                 /* Tell GDB to halt the debugger. This allows the user to
2795                                  * run monitor commands to handle the situation.
2796                                  */
2797                                 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2798                         }
2799                         if (target->backoff.times > 0) {
2800                                 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target));
2801                                 target_reset_examined(target);
2802                                 retval = target_examine_one(target);
2803                                 /* Target examination could have failed due to unstable connection,
2804                                  * but we set the examined flag anyway to repoll it later */
2805                                 if (retval != ERROR_OK) {
2806                                         target->examined = true;
2807                                         LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2808                                                  target->backoff.times * polling_interval);
2809                                         return retval;
2810                                 }
2811                         }
2812
2813                         /* Since we succeeded, we reset backoff count */
2814                         target->backoff.times = 0;
2815                 }
2816         }
2817
2818         return retval;
2819 }
2820
2821 COMMAND_HANDLER(handle_reg_command)
2822 {
2823         struct target *target;
2824         struct reg *reg = NULL;
2825         unsigned count = 0;
2826         char *value;
2827
2828         LOG_DEBUG("-");
2829
2830         target = get_current_target(CMD_CTX);
2831
2832         /* list all available registers for the current target */
2833         if (CMD_ARGC == 0) {
2834                 struct reg_cache *cache = target->reg_cache;
2835
2836                 count = 0;
2837                 while (cache) {
2838                         unsigned i;
2839
2840                         command_print(CMD_CTX, "===== %s", cache->name);
2841
2842                         for (i = 0, reg = cache->reg_list;
2843                                         i < cache->num_regs;
2844                                         i++, reg++, count++) {
2845                                 if (reg->exist == false)
2846                                         continue;
2847                                 /* only print cached values if they are valid */
2848                                 if (reg->valid) {
2849                                         value = buf_to_str(reg->value,
2850                                                         reg->size, 16);
2851                                         command_print(CMD_CTX,
2852                                                         "(%i) %s (/%" PRIu32 "): 0x%s%s",
2853                                                         count, reg->name,
2854                                                         reg->size, value,
2855                                                         reg->dirty
2856                                                                 ? " (dirty)"
2857                                                                 : "");
2858                                         free(value);
2859                                 } else {
2860                                         command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
2861                                                           count, reg->name,
2862                                                           reg->size) ;
2863                                 }
2864                         }
2865                         cache = cache->next;
2866                 }
2867
2868                 return ERROR_OK;
2869         }
2870
2871         /* access a single register by its ordinal number */
2872         if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
2873                 unsigned num;
2874                 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2875
2876                 struct reg_cache *cache = target->reg_cache;
2877                 count = 0;
2878                 while (cache) {
2879                         unsigned i;
2880                         for (i = 0; i < cache->num_regs; i++) {
2881                                 if (count++ == num) {
2882                                         reg = &cache->reg_list[i];
2883                                         break;
2884                                 }
2885                         }
2886                         if (reg)
2887                                 break;
2888                         cache = cache->next;
2889                 }
2890
2891                 if (!reg) {
2892                         command_print(CMD_CTX, "%i is out of bounds, the current target "
2893                                         "has only %i registers (0 - %i)", num, count, count - 1);
2894                         return ERROR_OK;
2895                 }
2896         } else {
2897                 /* access a single register by its name */
2898                 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2899
2900                 if (!reg)
2901                         goto not_found;
2902         }
2903
2904         assert(reg != NULL); /* give clang a hint that we *know* reg is != NULL here */
2905
2906         if (!reg->exist)
2907                 goto not_found;
2908
2909         /* display a register */
2910         if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
2911                         && (CMD_ARGV[1][0] <= '9')))) {
2912                 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2913                         reg->valid = 0;
2914
2915                 if (reg->valid == 0)
2916                         reg->type->get(reg);
2917                 value = buf_to_str(reg->value, reg->size, 16);
2918                 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2919                 free(value);
2920                 return ERROR_OK;
2921         }
2922
2923         /* set register value */
2924         if (CMD_ARGC == 2) {
2925                 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2926                 if (buf == NULL)
2927                         return ERROR_FAIL;
2928                 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2929
2930                 reg->type->set(reg, buf);
2931
2932                 value = buf_to_str(reg->value, reg->size, 16);
2933                 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2934                 free(value);
2935
2936                 free(buf);
2937
2938                 return ERROR_OK;
2939         }
2940
2941         return ERROR_COMMAND_SYNTAX_ERROR;
2942
2943 not_found:
2944         command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2945         return ERROR_OK;
2946 }
2947
2948 COMMAND_HANDLER(handle_poll_command)
2949 {
2950         int retval = ERROR_OK;
2951         struct target *target = get_current_target(CMD_CTX);
2952
2953         if (CMD_ARGC == 0) {
2954                 command_print(CMD_CTX, "background polling: %s",
2955                                 jtag_poll_get_enabled() ? "on" : "off");
2956                 command_print(CMD_CTX, "TAP: %s (%s)",
2957                                 target->tap->dotted_name,
2958                                 target->tap->enabled ? "enabled" : "disabled");
2959                 if (!target->tap->enabled)
2960                         return ERROR_OK;
2961                 retval = target_poll(target);
2962                 if (retval != ERROR_OK)
2963                         return retval;
2964                 retval = target_arch_state(target);
2965                 if (retval != ERROR_OK)
2966                         return retval;
2967         } else if (CMD_ARGC == 1) {
2968                 bool enable;
2969                 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2970                 jtag_poll_set_enabled(enable);
2971         } else
2972                 return ERROR_COMMAND_SYNTAX_ERROR;
2973
2974         return retval;
2975 }
2976
2977 COMMAND_HANDLER(handle_wait_halt_command)
2978 {
2979         if (CMD_ARGC > 1)
2980                 return ERROR_COMMAND_SYNTAX_ERROR;
2981
2982         unsigned ms = DEFAULT_HALT_TIMEOUT;
2983         if (1 == CMD_ARGC) {
2984                 int retval = parse_uint(CMD_ARGV[0], &ms);
2985                 if (ERROR_OK != retval)
2986                         return ERROR_COMMAND_SYNTAX_ERROR;
2987         }
2988
2989         struct target *target = get_current_target(CMD_CTX);
2990         return target_wait_state(target, TARGET_HALTED, ms);
2991 }
2992
2993 /* wait for target state to change. The trick here is to have a low
2994  * latency for short waits and not to suck up all the CPU time
2995  * on longer waits.
2996  *
2997  * After 500ms, keep_alive() is invoked
2998  */
2999 int target_wait_state(struct target *target, enum target_state state, int ms)
3000 {
3001         int retval;
3002         int64_t then = 0, cur;
3003         bool once = true;
3004
3005         for (;;) {
3006                 retval = target_poll(target);
3007                 if (retval != ERROR_OK)
3008                         return retval;
3009                 if (target->state == state)
3010                         break;
3011                 cur = timeval_ms();
3012                 if (once) {
3013                         once = false;
3014                         then = timeval_ms();
3015                         LOG_DEBUG("waiting for target %s...",
3016                                 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
3017                 }
3018
3019                 if (cur-then > 500)
3020                         keep_alive();
3021
3022                 if ((cur-then) > ms) {
3023                         LOG_ERROR("timed out while waiting for target %s",
3024                                 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
3025                         return ERROR_FAIL;
3026                 }
3027         }
3028
3029         return ERROR_OK;
3030 }
3031
3032 COMMAND_HANDLER(handle_halt_command)
3033 {
3034         LOG_DEBUG("-");
3035
3036         struct target *target = get_current_target(CMD_CTX);
3037
3038         target->verbose_halt_msg = true;
3039
3040         int retval = target_halt(target);
3041         if (ERROR_OK != retval)
3042                 return retval;
3043
3044         if (CMD_ARGC == 1) {
3045                 unsigned wait_local;
3046                 retval = parse_uint(CMD_ARGV[0], &wait_local);
3047                 if (ERROR_OK != retval)
3048                         return ERROR_COMMAND_SYNTAX_ERROR;
3049                 if (!wait_local)
3050                         return ERROR_OK;
3051         }
3052
3053         return CALL_COMMAND_HANDLER(handle_wait_halt_command);
3054 }
3055
3056 COMMAND_HANDLER(handle_soft_reset_halt_command)
3057 {
3058         struct target *target = get_current_target(CMD_CTX);
3059
3060         LOG_USER("requesting target halt and executing a soft reset");
3061
3062         target_soft_reset_halt(target);
3063
3064         return ERROR_OK;
3065 }
3066
3067 COMMAND_HANDLER(handle_reset_command)
3068 {
3069         if (CMD_ARGC > 1)
3070                 return ERROR_COMMAND_SYNTAX_ERROR;
3071
3072         enum target_reset_mode reset_mode = RESET_RUN;
3073         if (CMD_ARGC == 1) {
3074                 const Jim_Nvp *n;
3075                 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
3076                 if ((n->name == NULL) || (n->value == RESET_UNKNOWN))
3077                         return ERROR_COMMAND_SYNTAX_ERROR;
3078                 reset_mode = n->value;
3079         }
3080
3081         /* reset *all* targets */
3082         return target_process_reset(CMD_CTX, reset_mode);
3083 }
3084
3085
3086 COMMAND_HANDLER(handle_resume_command)
3087 {
3088         int current = 1;
3089         if (CMD_ARGC > 1)
3090                 return ERROR_COMMAND_SYNTAX_ERROR;
3091
3092         struct target *target = get_current_target(CMD_CTX);
3093
3094         /* with no CMD_ARGV, resume from current pc, addr = 0,
3095          * with one arguments, addr = CMD_ARGV[0],
3096          * handle breakpoints, not debugging */
3097         target_addr_t addr = 0;
3098         if (CMD_ARGC == 1) {
3099                 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3100                 current = 0;
3101         }
3102
3103         return target_resume(target, current, addr, 1, 0);
3104 }
3105
3106 COMMAND_HANDLER(handle_step_command)
3107 {
3108         if (CMD_ARGC > 1)
3109                 return ERROR_COMMAND_SYNTAX_ERROR;
3110
3111         LOG_DEBUG("-");
3112
3113         /* with no CMD_ARGV, step from current pc, addr = 0,
3114          * with one argument addr = CMD_ARGV[0],
3115          * handle breakpoints, debugging */
3116         target_addr_t addr = 0;
3117         int current_pc = 1;
3118         if (CMD_ARGC == 1) {
3119                 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3120                 current_pc = 0;
3121         }
3122
3123         struct target *target = get_current_target(CMD_CTX);
3124
3125         return target->type->step(target, current_pc, addr, 1);
3126 }
3127
3128 static void handle_md_output(struct command_context *cmd_ctx,
3129                 struct target *target, target_addr_t address, unsigned size,
3130                 unsigned count, const uint8_t *buffer)
3131 {
3132         const unsigned line_bytecnt = 32;
3133         unsigned line_modulo = line_bytecnt / size;
3134
3135         char output[line_bytecnt * 4 + 1];
3136         unsigned output_len = 0;
3137
3138         const char *value_fmt;
3139         switch (size) {
3140         case 8:
3141                 value_fmt = "%16.16"PRIx64" ";
3142                 break;
3143         case 4:
3144                 value_fmt = "%8.8"PRIx64" ";
3145                 break;
3146         case 2:
3147                 value_fmt = "%4.4"PRIx64" ";
3148                 break;
3149         case 1:
3150                 value_fmt = "%2.2"PRIx64" ";
3151                 break;
3152         default:
3153                 /* "can't happen", caller checked */
3154                 LOG_ERROR("invalid memory read size: %u", size);
3155                 return;
3156         }
3157
3158         for (unsigned i = 0; i < count; i++) {
3159                 if (i % line_modulo == 0) {
3160                         output_len += snprintf(output + output_len,
3161                                         sizeof(output) - output_len,
3162                                         TARGET_ADDR_FMT ": ",
3163                                         (address + (i * size)));
3164                 }
3165
3166                 uint64_t value = 0;
3167                 const uint8_t *value_ptr = buffer + i * size;
3168                 switch (size) {
3169                 case 8:
3170                         value = target_buffer_get_u64(target, value_ptr);
3171                         break;
3172                 case 4:
3173                         value = target_buffer_get_u32(target, value_ptr);
3174                         break;
3175                 case 2:
3176                         value = target_buffer_get_u16(target, value_ptr);
3177                         break;
3178                 case 1:
3179                         value = *value_ptr;
3180                 }
3181                 output_len += snprintf(output + output_len,
3182                                 sizeof(output) - output_len,
3183                                 value_fmt, value);
3184
3185                 if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
3186                         command_print(cmd_ctx, "%s", output);
3187                         output_len = 0;
3188                 }
3189         }
3190 }
3191
3192 COMMAND_HANDLER(handle_md_command)
3193 {
3194         if (CMD_ARGC < 1)
3195                 return ERROR_COMMAND_SYNTAX_ERROR;
3196
3197         unsigned size = 0;
3198         switch (CMD_NAME[2]) {
3199         case 'd':
3200                 size = 8;
3201                 break;
3202         case 'w':
3203                 size = 4;
3204                 break;
3205         case 'h':
3206                 size = 2;
3207                 break;
3208         case 'b':
3209                 size = 1;
3210                 break;
3211         default:
3212                 return ERROR_COMMAND_SYNTAX_ERROR;
3213         }
3214
3215         bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3216         int (*fn)(struct target *target,
3217                         target_addr_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
3218         if (physical) {
3219                 CMD_ARGC--;
3220                 CMD_ARGV++;
3221                 fn = target_read_phys_memory;
3222         } else
3223                 fn = target_read_memory;
3224         if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
3225                 return ERROR_COMMAND_SYNTAX_ERROR;
3226
3227         target_addr_t address;
3228         COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
3229
3230         unsigned count = 1;
3231         if (CMD_ARGC == 2)
3232                 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
3233
3234         uint8_t *buffer = calloc(count, size);
3235         if (buffer == NULL) {
3236                 LOG_ERROR("Failed to allocate md read buffer");
3237                 return ERROR_FAIL;
3238         }
3239
3240         struct target *target = get_current_target(CMD_CTX);
3241         int retval = fn(target, address, size, count, buffer);
3242         if (ERROR_OK == retval)
3243                 handle_md_output(CMD_CTX, target, address, size, count, buffer);
3244
3245         free(buffer);
3246
3247         return retval;
3248 }
3249
3250 typedef int (*target_write_fn)(struct target *target,
3251                 target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
3252
3253 static int target_fill_mem(struct target *target,
3254                 target_addr_t address,
3255                 target_write_fn fn,
3256                 unsigned data_size,
3257                 /* value */
3258                 uint64_t b,
3259                 /* count */
3260                 unsigned c)
3261 {
3262         /* We have to write in reasonably large chunks to be able
3263          * to fill large memory areas with any sane speed */
3264         const unsigned chunk_size = 16384;
3265         uint8_t *target_buf = malloc(chunk_size * data_size);
3266         if (target_buf == NULL) {
3267                 LOG_ERROR("Out of memory");
3268                 return ERROR_FAIL;
3269         }
3270
3271         for (unsigned i = 0; i < chunk_size; i++) {
3272                 switch (data_size) {
3273                 case 8:
3274                         target_buffer_set_u64(target, target_buf + i * data_size, b);
3275                         break;
3276                 case 4:
3277                         target_buffer_set_u32(target, target_buf + i * data_size, b);
3278                         break;
3279                 case 2:
3280                         target_buffer_set_u16(target, target_buf + i * data_size, b);
3281                         break;
3282                 case 1:
3283                         target_buffer_set_u8(target, target_buf + i * data_size, b);
3284                         break;
3285                 default:
3286                         exit(-1);
3287                 }
3288         }
3289
3290         int retval = ERROR_OK;
3291
3292         for (unsigned x = 0; x < c; x += chunk_size) {
3293                 unsigned current;
3294                 current = c - x;
3295                 if (current > chunk_size)
3296                         current = chunk_size;
3297                 retval = fn(target, address + x * data_size, data_size, current, target_buf);
3298                 if (retval != ERROR_OK)
3299                         break;
3300                 /* avoid GDB timeouts */
3301                 keep_alive();
3302         }
3303         free(target_buf);
3304
3305         return retval;
3306 }
3307
3308
3309 COMMAND_HANDLER(handle_mw_command)
3310 {
3311         if (CMD_ARGC < 2)
3312                 return ERROR_COMMAND_SYNTAX_ERROR;
3313         bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
3314         target_write_fn fn;
3315         if (physical) {
3316                 CMD_ARGC--;
3317                 CMD_ARGV++;
3318                 fn = target_write_phys_memory;
3319         } else
3320                 fn = target_write_memory;
3321         if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
3322                 return ERROR_COMMAND_SYNTAX_ERROR;
3323
3324         target_addr_t address;
3325         COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
3326
3327         target_addr_t value;
3328         COMMAND_PARSE_ADDRESS(CMD_ARGV[1], value);
3329
3330         unsigned count = 1;
3331         if (CMD_ARGC == 3)
3332                 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
3333
3334         struct target *target = get_current_target(CMD_CTX);
3335         unsigned wordsize;
3336         switch (CMD_NAME[2]) {
3337                 case 'd':
3338                         wordsize = 8;
3339                         break;
3340                 case 'w':
3341                         wordsize = 4;
3342                         break;
3343                 case 'h':
3344                         wordsize = 2;
3345                         break;
3346                 case 'b':
3347                         wordsize = 1;
3348                         break;
3349                 default:
3350                         return ERROR_COMMAND_SYNTAX_ERROR;
3351         }
3352
3353         return target_fill_mem(target, address, fn, wordsize, value, count);
3354 }
3355
3356 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
3357                 target_addr_t *min_address, target_addr_t *max_address)
3358 {
3359         if (CMD_ARGC < 1 || CMD_ARGC > 5)
3360                 return ERROR_COMMAND_SYNTAX_ERROR;
3361
3362         /* a base address isn't always necessary,
3363          * default to 0x0 (i.e. don't relocate) */
3364         if (CMD_ARGC >= 2) {
3365                 target_addr_t addr;
3366                 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], addr);
3367                 image->base_address = addr;
3368                 image->base_address_set = 1;
3369         } else
3370                 image->base_address_set = 0;
3371
3372         image->start_address_set = 0;
3373
3374         if (CMD_ARGC >= 4)
3375                 COMMAND_PARSE_ADDRESS(CMD_ARGV[3], *min_address);
3376         if (CMD_ARGC == 5) {
3377                 COMMAND_PARSE_ADDRESS(CMD_ARGV[4], *max_address);
3378                 /* use size (given) to find max (required) */
3379                 *max_address += *min_address;
3380         }
3381
3382         if (*min_address > *max_address)
3383                 return ERROR_COMMAND_SYNTAX_ERROR;
3384
3385         return ERROR_OK;
3386 }
3387
3388 COMMAND_HANDLER(handle_load_image_command)
3389 {
3390         uint8_t *buffer;
3391         size_t buf_cnt;
3392         uint32_t image_size;
3393         target_addr_t min_address = 0;
3394         target_addr_t max_address = -1;
3395         int i;
3396         struct image image;
3397
3398         int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
3399                         &image, &min_address, &max_address);
3400         if (ERROR_OK != retval)
3401                 return retval;
3402
3403         struct target *target = get_current_target(CMD_CTX);
3404
3405         struct duration bench;
3406         duration_start(&bench);
3407
3408         if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
3409                 return ERROR_FAIL;
3410
3411         image_size = 0x0;
3412         retval = ERROR_OK;
3413         for (i = 0; i < image.num_sections; i++) {
3414                 buffer = malloc(image.sections[i].size);
3415                 if (buffer == NULL) {
3416                         command_print(CMD_CTX,
3417                                                   "error allocating buffer for section (%d bytes)",
3418                                                   (int)(image.sections[i].size));
3419                         retval = ERROR_FAIL;
3420                         break;
3421                 }
3422
3423                 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3424                 if (retval != ERROR_OK) {
3425                         free(buffer);
3426                         break;
3427                 }
3428
3429                 uint32_t offset = 0;
3430                 uint32_t length = buf_cnt;
3431
3432                 /* DANGER!!! beware of unsigned comparision here!!! */
3433
3434                 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
3435                                 (image.sections[i].base_address < max_address)) {
3436
3437                         if (image.sections[i].base_address < min_address) {
3438                                 /* clip addresses below */
3439                                 offset += min_address-image.sections[i].base_address;
3440                                 length -= offset;
3441                         }
3442
3443                         if (image.sections[i].base_address + buf_cnt > max_address)
3444                                 length -= (image.sections[i].base_address + buf_cnt)-max_address;
3445
3446                         retval = target_write_buffer(target,
3447                                         image.sections[i].base_address + offset, length, buffer + offset);
3448                         if (retval != ERROR_OK) {
3449                                 free(buffer);
3450                                 break;
3451                         }
3452                         image_size += length;
3453                         command_print(CMD_CTX, "%u bytes written at address " TARGET_ADDR_FMT "",
3454                                         (unsigned int)length,
3455                                         image.sections[i].base_address + offset);
3456                 }
3457
3458                 free(buffer);
3459         }
3460
3461         if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3462                 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
3463                                 "in %fs (%0.3f KiB/s)", image_size,
3464                                 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3465         }
3466
3467         image_close(&image);
3468
3469         return retval;
3470
3471 }
3472
3473 COMMAND_HANDLER(handle_dump_image_command)
3474 {
3475         struct fileio *fileio;
3476         uint8_t *buffer;
3477         int retval, retvaltemp;
3478         target_addr_t address, size;
3479         struct duration bench;
3480         struct target *target = get_current_target(CMD_CTX);
3481
3482         if (CMD_ARGC != 3)
3483                 return ERROR_COMMAND_SYNTAX_ERROR;
3484
3485         COMMAND_PARSE_ADDRESS(CMD_ARGV[1], address);
3486         COMMAND_PARSE_ADDRESS(CMD_ARGV[2], size);
3487
3488         uint32_t buf_size = (size > 4096) ? 4096 : size;
3489         buffer = malloc(buf_size);
3490         if (!buffer)
3491                 return ERROR_FAIL;
3492
3493         retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
3494         if (retval != ERROR_OK) {
3495                 free(buffer);
3496                 return retval;
3497         }
3498
3499         duration_start(&bench);
3500
3501         while (size > 0) {
3502                 size_t size_written;
3503                 uint32_t this_run_size = (size > buf_size) ? buf_size : size;
3504                 retval = target_read_buffer(target, address, this_run_size, buffer);
3505                 if (retval != ERROR_OK)
3506                         break;
3507
3508                 retval = fileio_write(fileio, this_run_size, buffer, &size_written);
3509                 if (retval != ERROR_OK)
3510                         break;
3511
3512                 size -= this_run_size;
3513                 address += this_run_size;
3514         }
3515
3516         free(buffer);
3517
3518         if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3519                 size_t filesize;
3520                 retval = fileio_size(fileio, &filesize);
3521                 if (retval != ERROR_OK)
3522                         return retval;
3523                 command_print(CMD_CTX,
3524                                 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize,
3525                                 duration_elapsed(&bench), duration_kbps(&bench, filesize));
3526         }
3527
3528         retvaltemp = fileio_close(fileio);
3529         if (retvaltemp != ERROR_OK)
3530                 return retvaltemp;
3531
3532         return retval;
3533 }
3534
3535 enum verify_mode {
3536         IMAGE_TEST = 0,
3537         IMAGE_VERIFY = 1,
3538         IMAGE_CHECKSUM_ONLY = 2
3539 };
3540
3541 static COMMAND_HELPER(handle_verify_image_command_internal, enum verify_mode verify)
3542 {
3543         uint8_t *buffer;
3544         size_t buf_cnt;
3545         uint32_t image_size;
3546         int i;
3547         int retval;
3548         uint32_t checksum = 0;
3549         uint32_t mem_checksum = 0;
3550
3551         struct image image;
3552
3553         struct target *target = get_current_target(CMD_CTX);
3554
3555         if (CMD_ARGC < 1)
3556                 return ERROR_COMMAND_SYNTAX_ERROR;
3557
3558         if (!target) {
3559                 LOG_ERROR("no target selected");
3560                 return ERROR_FAIL;
3561         }
3562
3563         struct duration bench;
3564         duration_start(&bench);
3565
3566         if (CMD_ARGC >= 2) {
3567                 target_addr_t addr;
3568                 COMMAND_PARSE_ADDRESS(CMD_ARGV[1], addr);
3569                 image.base_address = addr;
3570                 image.base_address_set = 1;
3571         } else {
3572                 image.base_address_set = 0;
3573                 image.base_address = 0x0;
3574         }
3575
3576         image.start_address_set = 0;
3577
3578         retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
3579         if (retval != ERROR_OK)
3580                 return retval;
3581
3582         image_size = 0x0;
3583         int diffs = 0;
3584         retval = ERROR_OK;
3585         for (i = 0; i < image.num_sections; i++) {
3586                 buffer = malloc(image.sections[i].size);
3587                 if (buffer == NULL) {
3588                         command_print(CMD_CTX,
3589                                         "error allocating buffer for section (%d bytes)",
3590                                         (int)(image.sections[i].size));
3591                         break;
3592                 }
3593                 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3594                 if (retval != ERROR_OK) {
3595                         free(buffer);
3596                         break;
3597                 }
3598
3599                 if (verify >= IMAGE_VERIFY) {
3600                         /* calculate checksum of image */
3601                         retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
3602                         if (retval != ERROR_OK) {
3603                                 free(buffer);
3604                                 break;
3605                         }
3606
3607                         retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
3608                         if (retval != ERROR_OK) {
3609                                 free(buffer);
3610                                 break;
3611                         }
3612                         if ((checksum != mem_checksum) && (verify == IMAGE_CHECKSUM_ONLY)) {
3613                                 LOG_ERROR("checksum mismatch");
3614                                 free(buffer);
3615                                 retval = ERROR_FAIL;
3616                                 goto done;
3617                         }
3618                         if (checksum != mem_checksum) {
3619                                 /* failed crc checksum, fall back to a binary compare */
3620                                 uint8_t *data;
3621
3622                                 if (diffs == 0)
3623                                         LOG_ERROR("checksum mismatch - attempting binary compare");
3624
3625                                 data = malloc(buf_cnt);
3626
3627                                 /* Can we use 32bit word accesses? */
3628                                 int size = 1;
3629                                 int count = buf_cnt;
3630                                 if ((count % 4) == 0) {
3631                                         size *= 4;
3632                                         count /= 4;
3633                                 }
3634                                 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
3635                                 if (retval == ERROR_OK) {
3636                                         uint32_t t;
3637                                         for (t = 0; t < buf_cnt; t++) {
3638                                                 if (data[t] != buffer[t]) {
3639                                                         command_print(CMD_CTX,
3640                                                                                   "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3641                                                                                   diffs,
3642                                                                                   (unsigned)(t + image.sections[i].base_address),
3643                                                                                   data[t],
3644                                                                                   buffer[t]);
3645                                                         if (diffs++ >= 127) {
3646                                                                 command_print(CMD_CTX, "More than 128 errors, the rest are not printed.");
3647                                                                 free(data);
3648                                                                 free(buffer);
3649                                                                 goto done;
3650                                                         }
3651                                                 }
3652                                                 keep_alive();
3653                                         }
3654                                 }
3655                                 free(data);
3656                         }
3657                 } else {
3658                         command_print(CMD_CTX, "address " TARGET_ADDR_FMT " length 0x%08zx",
3659                                                   image.sections[i].base_address,
3660                                                   buf_cnt);
3661                 }
3662
3663                 free(buffer);
3664                 image_size += buf_cnt;
3665         }
3666         if (diffs > 0)
3667                 command_print(CMD_CTX, "No more differences found.");
3668 done:
3669         if (diffs > 0)
3670                 retval = ERROR_FAIL;
3671         if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3672                 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
3673                                 "in %fs (%0.3f KiB/s)", image_size,
3674                                 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3675         }
3676
3677         image_close(&image);
3678
3679         return retval;
3680 }
3681
3682 COMMAND_HANDLER(handle_verify_image_checksum_command)
3683 {
3684         return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_CHECKSUM_ONLY);
3685 }
3686
3687 COMMAND_HANDLER(handle_verify_image_command)
3688 {
3689         return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_VERIFY);
3690 }
3691
3692 COMMAND_HANDLER(handle_test_image_command)
3693 {
3694         return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, IMAGE_TEST);
3695 }
3696
3697 static int handle_bp_command_list(struct command_context *cmd_ctx)
3698 {
3699         struct target *target = get_current_target(cmd_ctx);
3700         struct breakpoint *breakpoint = target->breakpoints;
3701         while (breakpoint) {
3702                 if (breakpoint->type == BKPT_SOFT) {
3703                         char *buf = buf_to_str(breakpoint->orig_instr,
3704                                         breakpoint->length, 16);
3705                         command_print(cmd_ctx, "IVA breakpoint: " TARGET_ADDR_FMT ", 0x%x, %i, 0x%s",
3706                                         breakpoint->address,
3707                                         breakpoint->length,
3708                                         breakpoint->set, buf);
3709                         free(buf);
3710                 } else {
3711                         if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3712                                 command_print(cmd_ctx, "Context breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i",
3713                                                         breakpoint->asid,
3714                                                         breakpoint->length, breakpoint->set);
3715                         else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
3716                                 command_print(cmd_ctx, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT ", 0x%x, %i",
3717                                                         breakpoint->address,
3718                                                         breakpoint->length, breakpoint->set);
3719                                 command_print(cmd_ctx, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3720                                                         breakpoint->asid);
3721                         } else
3722                                 command_print(cmd_ctx, "Breakpoint(IVA): " TARGET_ADDR_FMT ", 0x%x, %i",
3723                                                         breakpoint->address,
3724                                                         breakpoint->length, breakpoint->set);
3725                 }
3726
3727                 breakpoint = breakpoint->next;
3728         }
3729         return ERROR_OK;
3730 }
3731
3732 static int handle_bp_command_set(struct command_context *cmd_ctx,
3733                 target_addr_t addr, uint32_t asid, uint32_t length, int hw)
3734 {
3735         struct target *target = get_current_target(cmd_ctx);
3736         int retval;
3737
3738         if (asid == 0) {
3739                 retval = breakpoint_add(target, addr, length, hw);
3740                 /* error is always logged in breakpoint_add(), do not print it again */
3741                 if (ERROR_OK == retval)
3742                         command_print(cmd_ctx, "breakpoint set at " TARGET_ADDR_FMT "", addr);
3743
3744         } else if (addr == 0) {
3745                 if (target->type->add_context_breakpoint == NULL) {
3746                         LOG_ERROR("Context breakpoint not available");
3747                         return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
3748                 }
3749                 retval = context_breakpoint_add(target, asid, length, hw);
3750                 /* error is always logged in context_breakpoint_add(), do not print it again */
3751                 if (ERROR_OK == retval)
3752                         command_print(cmd_ctx, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
3753
3754         } else {
3755                 if (target->type->add_hybrid_breakpoint == NULL) {
3756                         LOG_ERROR("Hybrid breakpoint not available");
3757                         return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
3758                 }
3759                 retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3760                 /* error is always logged in hybrid_breakpoint_add(), do not print it again */
3761                 if (ERROR_OK == retval)
3762                         command_print(cmd_ctx, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
3763         }
3764         return retval;
3765 }
3766
3767 COMMAND_HANDLER(handle_bp_command)
3768 {
3769         target_addr_t addr;
3770         uint32_t asid;
3771         uint32_t length;
3772         int hw = BKPT_SOFT;
3773
3774         switch (CMD_ARGC) {
3775                 case 0:
3776                         return handle_bp_command_list(CMD_CTX);
3777
3778                 case 2:
3779                         asid = 0;
3780                         COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3781                         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3782                         return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3783
3784                 case 3:
3785                         if (strcmp(CMD_ARGV[2], "hw") == 0) {
3786                                 hw = BKPT_HARD;
3787                                 COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3788                                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3789                                 asid = 0;
3790                                 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3791                         } else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
3792                                 hw = BKPT_HARD;
3793                                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
3794                                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3795                                 addr = 0;
3796                                 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3797                         }
3798                         /* fallthrough */
3799                 case 4:
3800                         hw = BKPT_HARD;
3801                         COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3802                         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
3803                         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], length);
3804                         return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3805
3806                 default:
3807                         return ERROR_COMMAND_SYNTAX_ERROR;
3808         }
3809 }
3810
3811 COMMAND_HANDLER(handle_rbp_command)
3812 {
3813         if (CMD_ARGC != 1)
3814                 return ERROR_COMMAND_SYNTAX_ERROR;
3815
3816         target_addr_t addr;
3817         COMMAND_PARSE_ADDRESS(CMD_ARGV[0], addr);
3818
3819         struct target *target = get_current_target(CMD_CTX);
3820         breakpoint_remove(target, addr);
3821
3822         return ERROR_OK;
3823 }
3824
3825 COMMAND_HANDLER(handle_wp_command)
3826 {
3827         struct target *target = get_current_target(CMD_CTX);
3828
3829         if (CMD_ARGC == 0) {
3830                 struct watchpoint *watchpoint = target->watchpoints;
3831
3832                 while (watchpoint) {
3833                         command_print(CMD_CTX, "address: " TARGET_ADDR_FMT
3834                                         ", len: 0x%8.8" PRIx32
3835                                         ", r/w/a: %i, value: 0x%8.8" PRIx32
3836                                         ", mask: 0x%8.8" PRIx32,
3837                                         watchpoint->address,
3838                                         watchpoint->length,
3839                                         (int)watchpoint->rw,
3840                                         watchpoint->value,
3841                                         watchpoint->mask);
3842                         watchpoint = watchpoint->next;
3843                 }
3844                 return ERROR_OK;
3845         }
3846
3847         enum watchpoint_rw type = WPT_ACCESS;
3848         uint32_t addr = 0;
3849         uint32_t length = 0;
3850         uint32_t data_value = 0x0;
3851         uint32_t data_mask = 0xffffffff;
3852
3853         switch (CMD_ARGC) {
3854         case 5:
3855                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
3856                 /* fall through */
3857         case 4:
3858                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
3859                 /* fall through */
3860         case 3:
3861                 switch (CMD_ARGV[2][0]) {
3862                 case 'r':
3863                         type = WPT_READ;
3864                         break;
3865                 case 'w':
3866                         type = WPT_WRITE;
3867                         break;
3868                 case 'a':
3869                         type = WPT_ACCESS;
3870                         break;
3871                 default:
3872                         LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
3873                         return ERROR_COMMAND_SYNTAX_ERROR;
3874                 }
3875                 /* fall through */
3876         case 2:
3877                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3878                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3879                 break;
3880
3881         default:
3882                 return ERROR_COMMAND_SYNTAX_ERROR;
3883         }
3884
3885         int retval = watchpoint_add(target, addr, length, type,
3886                         data_value, data_mask);
3887         if (ERROR_OK != retval)
3888                 LOG_ERROR("Failure setting watchpoints");
3889
3890         return retval;
3891 }
3892
3893 COMMAND_HANDLER(handle_rwp_command)
3894 {
3895         if (CMD_ARGC != 1)
3896                 return ERROR_COMMAND_SYNTAX_ERROR;
3897
3898         uint32_t addr;
3899         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3900
3901         struct target *target = get_current_target(CMD_CTX);
3902         watchpoint_remove(target, addr);
3903
3904         return ERROR_OK;
3905 }
3906
3907 /**
3908  * Translate a virtual address to a physical address.
3909  *
3910  * The low-level target implementation must have logged a detailed error
3911  * which is forwarded to telnet/GDB session.
3912  */
3913 COMMAND_HANDLER(handle_virt2phys_command)
3914 {
3915         if (CMD_ARGC != 1)
3916                 return ERROR_COMMAND_SYNTAX_ERROR;
3917
3918         target_addr_t va;
3919         COMMAND_PARSE_ADDRESS(CMD_ARGV[0], va);
3920         target_addr_t pa;
3921
3922         struct target *target = get_current_target(CMD_CTX);
3923         int retval = target->type->virt2phys(target, va, &pa);
3924         if (retval == ERROR_OK)
3925                 command_print(CMD_CTX, "Physical address " TARGET_ADDR_FMT "", pa);
3926
3927         return retval;
3928 }
3929
3930 static void writeData(FILE *f, const void *data, size_t len)
3931 {
3932         size_t written = fwrite(data, 1, len, f);
3933         if (written != len)
3934                 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3935 }
3936
3937 static void writeLong(FILE *f, int l, struct target *target)
3938 {
3939         uint8_t val[4];
3940
3941         target_buffer_set_u32(target, val, l);
3942         writeData(f, val, 4);
3943 }
3944
3945 static void writeString(FILE *f, char *s)
3946 {
3947         writeData(f, s, strlen(s));
3948 }
3949
3950 typedef unsigned char UNIT[2];  /* unit of profiling */
3951
3952 /* Dump a gmon.out histogram file. */
3953 static void write_gmon(uint32_t *samples, uint32_t sampleNum, const char *filename, bool with_range,
3954                         uint32_t start_address, uint32_t end_address, struct target *target, uint32_t duration_ms)
3955 {
3956         uint32_t i;
3957         FILE *f = fopen(filename, "w");
3958         if (f == NULL)
3959                 return;
3960         writeString(f, "gmon");
3961         writeLong(f, 0x00000001, target); /* Version */
3962         writeLong(f, 0, target); /* padding */
3963         writeLong(f, 0, target); /* padding */
3964         writeLong(f, 0, target); /* padding */
3965
3966         uint8_t zero = 0;  /* GMON_TAG_TIME_HIST */
3967         writeData(f, &zero, 1);
3968
3969         /* figure out bucket size */
3970         uint32_t min;
3971         uint32_t max;
3972         if (with_range) {
3973                 min = start_address;
3974                 max = end_address;
3975         } else {
3976                 min = samples[0];
3977                 max = samples[0];
3978                 for (i = 0; i < sampleNum; i++) {
3979                         if (min > samples[i])
3980                                 min = samples[i];
3981                         if (max < samples[i])
3982                                 max = samples[i];
3983                 }
3984
3985                 /* max should be (largest sample + 1)
3986                  * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3987                 max++;
3988         }
3989
3990         int addressSpace = max - min;
3991         assert(addressSpace >= 2);
3992
3993         /* FIXME: What is the reasonable number of buckets?
3994          * The profiling result will be more accurate if there are enough buckets. */
3995         static const uint32_t maxBuckets = 128 * 1024; /* maximum buckets. */
3996         uint32_t numBuckets = addressSpace / sizeof(UNIT);
3997         if (numBuckets > maxBuckets)
3998                 numBuckets = maxBuckets;
3999         int *buckets = malloc(sizeof(int) * numBuckets);
4000         if (buckets == NULL) {
4001                 fclose(f);
4002                 return;
4003         }
4004         memset(buckets, 0, sizeof(int) * numBuckets);
4005         for (i = 0; i < sampleNum; i++) {
4006                 uint32_t address = samples[i];
4007
4008                 if ((address < min) || (max <= address))
4009                         continue;
4010
4011                 long long a = address - min;
4012                 long long b = numBuckets;
4013                 long long c = addressSpace;
4014                 int index_t = (a * b) / c; /* danger!!!! int32 overflows */
4015                 buckets[index_t]++;
4016         }
4017
4018         /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
4019         writeLong(f, min, target);                      /* low_pc */
4020         writeLong(f, max, target);                      /* high_pc */
4021         writeLong(f, numBuckets, target);       /* # of buckets */
4022         float sample_rate = sampleNum / (duration_ms / 1000.0);
4023         writeLong(f, sample_rate, target);
4024         writeString(f, "seconds");
4025         for (i = 0; i < (15-strlen("seconds")); i++)
4026                 writeData(f, &zero, 1);
4027         writeString(f, "s");
4028
4029         /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
4030
4031         char *data = malloc(2 * numBuckets);
4032         if (data != NULL) {
4033                 for (i = 0; i < numBuckets; i++) {
4034                         int val;
4035                         val = buckets[i];
4036                         if (val > 65535)
4037                                 val = 65535;
4038                         data[i * 2] = val&0xff;
4039                         data[i * 2 + 1] = (val >> 8) & 0xff;
4040                 }
4041                 free(buckets);
4042                 writeData(f, data, numBuckets * 2);
4043                 free(data);
4044         } else
4045                 free(buckets);
4046
4047         fclose(f);
4048 }
4049
4050 /* profiling samples the CPU PC as quickly as OpenOCD is able,
4051  * which will be used as a random sampling of PC */
4052 COMMAND_HANDLER(handle_profile_command)
4053 {
4054         struct target *target = get_current_target(CMD_CTX);
4055
4056         if ((CMD_ARGC != 2) && (CMD_ARGC != 4))
4057                 return ERROR_COMMAND_SYNTAX_ERROR;
4058
4059         const uint32_t MAX_PROFILE_SAMPLE_NUM = 10000;
4060         uint32_t offset;
4061         uint32_t num_of_samples;
4062         int retval = ERROR_OK;
4063
4064         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], offset);
4065
4066         uint32_t *samples = malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM);
4067         if (samples == NULL) {
4068                 LOG_ERROR("No memory to store samples.");
4069                 return ERROR_FAIL;
4070         }
4071
4072         uint64_t timestart_ms = timeval_ms();
4073         /**
4074          * Some cores let us sample the PC without the
4075          * annoying halt/resume step; for example, ARMv7 PCSR.
4076          * Provide a way to use that more efficient mechanism.
4077          */
4078         retval = target_profiling(target, samples, MAX_PROFILE_SAMPLE_NUM,
4079                                 &num_of_samples, offset);
4080         if (retval != ERROR_OK) {
4081                 free(samples);
4082                 return retval;
4083         }
4084         uint32_t duration_ms = timeval_ms() - timestart_ms;
4085
4086         assert(num_of_samples <= MAX_PROFILE_SAMPLE_NUM);
4087
4088         retval = target_poll(target);
4089         if (retval != ERROR_OK) {
4090                 free(samples);
4091                 return retval;
4092         }
4093         if (target->state == TARGET_RUNNING) {
4094                 retval = target_halt(target);
4095                 if (retval != ERROR_OK) {
4096                         free(samples);
4097                         return retval;
4098                 }
4099         }
4100
4101         retval = target_poll(target);
4102         if (retval != ERROR_OK) {
4103                 free(samples);
4104                 return retval;
4105         }
4106
4107         uint32_t start_address = 0;
4108         uint32_t end_address = 0;
4109         bool with_range = false;
4110         if (CMD_ARGC == 4) {
4111                 with_range = true;
4112                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], start_address);
4113                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], end_address);
4114         }
4115
4116         write_gmon(samples, num_of_samples, CMD_ARGV[1],
4117                    with_range, start_address, end_address, target, duration_ms);
4118         command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
4119
4120         free(samples);
4121         return retval;
4122 }
4123
4124 static int new_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t val)
4125 {
4126         char *namebuf;
4127         Jim_Obj *nameObjPtr, *valObjPtr;
4128         int result;
4129
4130         namebuf = alloc_printf("%s(%d)", varname, idx);
4131         if (!namebuf)
4132                 return JIM_ERR;
4133
4134         nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
4135         valObjPtr = Jim_NewIntObj(interp, val);
4136         if (!nameObjPtr || !valObjPtr) {
4137                 free(namebuf);
4138                 return JIM_ERR;
4139         }
4140
4141         Jim_IncrRefCount(nameObjPtr);
4142         Jim_IncrRefCount(valObjPtr);
4143         result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
4144         Jim_DecrRefCount(interp, nameObjPtr);
4145         Jim_DecrRefCount(interp, valObjPtr);
4146         free(namebuf);
4147         /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4148         return result;
4149 }
4150
4151 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4152 {
4153         struct command_context *context;
4154         struct target *target;
4155
4156         context = current_command_context(interp);
4157         assert(context != NULL);
4158
4159         target = get_current_target(context);
4160         if (target == NULL) {
4161                 LOG_ERROR("mem2array: no current target");
4162                 return JIM_ERR;
4163         }
4164
4165         return target_mem2array(interp, target, argc - 1, argv + 1);
4166 }
4167
4168 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
4169 {
4170         long l;
4171         uint32_t width;
4172         int len;
4173         uint32_t addr;
4174         uint32_t count;
4175         uint32_t v;
4176         const char *varname;
4177         const char *phys;
4178         bool is_phys;
4179         int  n, e, retval;
4180         uint32_t i;
4181
4182         /* argv[1] = name of array to receive the data
4183          * argv[2] = desired width
4184          * argv[3] = memory address
4185          * argv[4] = count of times to read
4186          */
4187
4188         if (argc < 4 || argc > 5) {
4189                 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems [phys]");
4190                 return JIM_ERR;
4191         }
4192         varname = Jim_GetString(argv[0], &len);
4193         /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4194
4195         e = Jim_GetLong(interp, argv[1], &l);
4196         width = l;
4197         if (e != JIM_OK)
4198                 return e;
4199
4200         e = Jim_GetLong(interp, argv[2], &l);
4201         addr = l;
4202         if (e != JIM_OK)
4203                 return e;
4204         e = Jim_GetLong(interp, argv[3], &l);
4205         len = l;
4206         if (e != JIM_OK)
4207                 return e;
4208         is_phys = false;
4209         if (argc > 4) {
4210                 phys = Jim_GetString(argv[4], &n);
4211                 if (!strncmp(phys, "phys", n))
4212                         is_phys = true;
4213                 else
4214                         return JIM_ERR;
4215         }
4216         switch (width) {
4217                 case 8:
4218                         width = 1;
4219                         break;
4220                 case 16:
4221                         width = 2;
4222                         break;
4223                 case 32:
4224                         width = 4;
4225                         break;
4226                 default:
4227                         Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4228                         Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
4229                         return JIM_ERR;
4230         }
4231         if (len == 0) {
4232                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4233                 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
4234                 return JIM_ERR;
4235         }
4236         if ((addr + (len * width)) < addr) {
4237                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4238                 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
4239                 return JIM_ERR;
4240         }
4241         /* absurd transfer size? */
4242         if (len > 65536) {
4243                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4244                 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
4245                 return JIM_ERR;
4246         }
4247
4248         if ((width == 1) ||
4249                 ((width == 2) && ((addr & 1) == 0)) ||
4250                 ((width == 4) && ((addr & 3) == 0))) {
4251                 /* all is well */
4252         } else {
4253                 char buf[100];
4254                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4255                 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
4256                                 addr,
4257                                 width);
4258                 Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
4259                 return JIM_ERR;
4260         }
4261
4262         /* Transfer loop */
4263
4264         /* index counter */
4265         n = 0;
4266
4267         size_t buffersize = 4096;
4268         uint8_t *buffer = malloc(buffersize);
4269         if (buffer == NULL)
4270                 return JIM_ERR;
4271
4272         /* assume ok */
4273         e = JIM_OK;
4274         while (len) {
4275                 /* Slurp... in buffer size chunks */
4276
4277                 count = len; /* in objects.. */
4278                 if (count > (buffersize / width))
4279                         count = (buffersize / width);
4280
4281                 if (is_phys)
4282                         retval = target_read_phys_memory(target, addr, width, count, buffer);
4283                 else
4284                         retval = target_read_memory(target, addr, width, count, buffer);
4285                 if (retval != ERROR_OK) {
4286                         /* BOO !*/
4287                         LOG_ERROR("mem2array: Read @ 0x%08" PRIx32 ", w=%" PRId32 ", cnt=%" PRId32 ", failed",
4288                                           addr,
4289                                           width,
4290                                           count);
4291                         Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4292                         Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
4293                         e = JIM_ERR;
4294                         break;
4295                 } else {
4296                         v = 0; /* shut up gcc */
4297                         for (i = 0; i < count ; i++, n++) {
4298                                 switch (width) {
4299                                         case 4:
4300                                                 v = target_buffer_get_u32(target, &buffer[i*width]);
4301                                                 break;
4302                                         case 2:
4303                                                 v = target_buffer_get_u16(target, &buffer[i*width]);
4304                                                 break;
4305                                         case 1:
4306                                                 v = buffer[i] & 0x0ff;
4307                                                 break;
4308                                 }
4309                                 new_int_array_element(interp, varname, n, v);
4310                         }
4311                         len -= count;
4312                         addr += count * width;
4313                 }
4314         }
4315
4316         free(buffer);
4317
4318         Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4319
4320         return e;
4321 }
4322
4323 static int get_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t *val)
4324 {
4325         char *namebuf;
4326         Jim_Obj *nameObjPtr, *valObjPtr;
4327         int result;
4328         long l;
4329
4330         namebuf = alloc_printf("%s(%d)", varname, idx);
4331         if (!namebuf)
4332                 return JIM_ERR;
4333
4334         nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
4335         if (!nameObjPtr) {
4336                 free(namebuf);
4337                 return JIM_ERR;
4338         }
4339
4340         Jim_IncrRefCount(nameObjPtr);
4341         valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
4342         Jim_DecrRefCount(interp, nameObjPtr);
4343         free(namebuf);
4344         if (valObjPtr == NULL)
4345                 return JIM_ERR;
4346
4347         result = Jim_GetLong(interp, valObjPtr, &l);
4348         /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4349         *val = l;
4350         return result;
4351 }
4352
4353 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4354 {
4355         struct command_context *context;
4356         struct target *target;
4357
4358         context = current_command_context(interp);
4359         assert(context != NULL);
4360
4361         target = get_current_target(context);
4362         if (target == NULL) {
4363                 LOG_ERROR("array2mem: no current target");
4364                 return JIM_ERR;
4365         }
4366
4367         return target_array2mem(interp, target, argc-1, argv + 1);
4368 }
4369
4370 static int target_array2mem(Jim_Interp *interp, struct target *target,
4371                 int argc, Jim_Obj *const *argv)
4372 {
4373         long l;
4374         uint32_t width;
4375         int len;
4376         uint32_t addr;
4377         uint32_t count;
4378         uint32_t v;
4379         const char *varname;
4380         const char *phys;
4381         bool is_phys;
4382         int  n, e, retval;
4383         uint32_t i;
4384
4385         /* argv[1] = name of array to get the data
4386          * argv[2] = desired width
4387          * argv[3] = memory address
4388          * argv[4] = count to write
4389          */
4390         if (argc < 4 || argc > 5) {
4391                 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems [phys]");
4392                 return JIM_ERR;
4393         }
4394         varname = Jim_GetString(argv[0], &len);
4395         /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4396
4397         e = Jim_GetLong(interp, argv[1], &l);
4398         width = l;
4399         if (e != JIM_OK)
4400                 return e;
4401
4402         e = Jim_GetLong(interp, argv[2], &l);
4403         addr = l;
4404         if (e != JIM_OK)
4405                 return e;
4406         e = Jim_GetLong(interp, argv[3], &l);
4407         len = l;
4408         if (e != JIM_OK)
4409                 return e;
4410         is_phys = false;
4411         if (argc > 4) {
4412                 phys = Jim_GetString(argv[4], &n);
4413                 if (!strncmp(phys, "phys", n))
4414                         is_phys = true;
4415                 else
4416                         return JIM_ERR;
4417         }
4418         switch (width) {
4419                 case 8:
4420                         width = 1;
4421                         break;
4422                 case 16:
4423                         width = 2;
4424                         break;
4425                 case 32:
4426                         width = 4;
4427                         break;
4428                 default:
4429                         Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4430                         Jim_AppendStrings(interp, Jim_GetResult(interp),
4431                                         "Invalid width param, must be 8/16/32", NULL);
4432                         return JIM_ERR;
4433         }
4434         if (len == 0) {
4435                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4436                 Jim_AppendStrings(interp, Jim_GetResult(interp),
4437                                 "array2mem: zero width read?", NULL);
4438                 return JIM_ERR;
4439         }
4440         if ((addr + (len * width)) < addr) {
4441                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4442                 Jim_AppendStrings(interp, Jim_GetResult(interp),
4443                                 "array2mem: addr + len - wraps to zero?", NULL);
4444                 return JIM_ERR;
4445         }
4446         /* absurd transfer size? */
4447         if (len > 65536) {
4448                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4449                 Jim_AppendStrings(interp, Jim_GetResult(interp),
4450                                 "array2mem: absurd > 64K item request", NULL);
4451                 return JIM_ERR;
4452         }
4453
4454         if ((width == 1) ||
4455                 ((width == 2) && ((addr & 1) == 0)) ||
4456                 ((width == 4) && ((addr & 3) == 0))) {
4457                 /* all is well */
4458         } else {
4459                 char buf[100];
4460                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4461                 sprintf(buf, "array2mem address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
4462                                 addr,
4463                                 width);
4464                 Jim_AppendStrings(interp, Jim_GetResult(interp), buf, NULL);
4465                 return JIM_ERR;
4466         }
4467
4468         /* Transfer loop */
4469
4470         /* index counter */
4471         n = 0;
4472         /* assume ok */
4473         e = JIM_OK;
4474
4475         size_t buffersize = 4096;
4476         uint8_t *buffer = malloc(buffersize);
4477         if (buffer == NULL)
4478                 return JIM_ERR;
4479
4480         while (len) {
4481                 /* Slurp... in buffer size chunks */
4482
4483                 count = len; /* in objects.. */
4484                 if (count > (buffersize / width))
4485                         count = (buffersize / width);
4486
4487                 v = 0; /* shut up gcc */
4488                 for (i = 0; i < count; i++, n++) {
4489                         get_int_array_element(interp, varname, n, &v);
4490                         switch (width) {
4491                         case 4:
4492                                 target_buffer_set_u32(target, &buffer[i * width], v);
4493                                 break;
4494                         case 2:
4495                                 target_buffer_set_u16(target, &buffer[i * width], v);
4496                                 break;
4497                         case 1:
4498                                 buffer[i] = v & 0x0ff;
4499                                 break;
4500                         }
4501                 }
4502                 len -= count;
4503
4504                 if (is_phys)
4505                         retval = target_write_phys_memory(target, addr, width, count, buffer);
4506                 else
4507                         retval = target_write_memory(target, addr, width, count, buffer);
4508                 if (retval != ERROR_OK) {
4509                         /* BOO !*/
4510                         LOG_ERROR("array2mem: Write @ 0x%08" PRIx32 ", w=%" PRId32 ", cnt=%" PRId32 ", failed",
4511                                           addr,
4512                                           width,
4513                                           count);
4514                         Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4515                         Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
4516                         e = JIM_ERR;
4517                         break;
4518                 }
4519                 addr += count * width;
4520         }
4521
4522         free(buffer);
4523
4524         Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4525
4526         return e;
4527 }
4528
4529 /* FIX? should we propagate errors here rather than printing them
4530  * and continuing?
4531  */
4532 void target_handle_event(struct target *target, enum target_event e)
4533 {
4534         struct target_event_action *teap;
4535
4536         for (teap = target->event_action; teap != NULL; teap = teap->next) {
4537                 if (teap->event == e) {
4538                         LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
4539                                            target->target_number,
4540                                            target_name(target),
4541                                            target_type_name(target),
4542                                            e,
4543                                            Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
4544                                            Jim_GetString(teap->body, NULL));
4545
4546                         /* Override current target by the target an event
4547                          * is issued from (lot of scripts need it).
4548                          * Return back to previous override as soon
4549                          * as the handler processing is done */
4550                         struct command_context *cmd_ctx = current_command_context(teap->interp);
4551                         struct target *saved_target_override = cmd_ctx->current_target_override;
4552                         cmd_ctx->current_target_override = target;
4553
4554                         if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK) {
4555                                 Jim_MakeErrorMessage(teap->interp);
4556                                 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(teap->interp), NULL));
4557                         }
4558
4559                         cmd_ctx->current_target_override = saved_target_override;
4560                 }
4561         }
4562 }
4563
4564 /**
4565  * Returns true only if the target has a handler for the specified event.
4566  */
4567 bool target_has_event_action(struct target *target, enum target_event event)
4568 {
4569         struct target_event_action *teap;
4570
4571         for (teap = target->event_action; teap != NULL; teap = teap->next) {
4572                 if (teap->event == event)
4573                         return true;
4574         }
4575         return false;
4576 }
4577
4578 enum target_cfg_param {
4579         TCFG_TYPE,
4580         TCFG_EVENT,
4581         TCFG_WORK_AREA_VIRT,
4582         TCFG_WORK_AREA_PHYS,
4583         TCFG_WORK_AREA_SIZE,
4584         TCFG_WORK_AREA_BACKUP,
4585         TCFG_ENDIAN,
4586         TCFG_COREID,
4587         TCFG_CHAIN_POSITION,
4588         TCFG_DBGBASE,
4589         TCFG_RTOS,
4590         TCFG_DEFER_EXAMINE,
4591         TCFG_GDB_PORT,
4592 };
4593
4594 static Jim_Nvp nvp_config_opts[] = {
4595         { .name = "-type",             .value = TCFG_TYPE },
4596         { .name = "-event",            .value = TCFG_EVENT },
4597         { .name = "-work-area-virt",   .value = TCFG_WORK_AREA_VIRT },
4598         { .name = "-work-area-phys",   .value = TCFG_WORK_AREA_PHYS },
4599         { .name = "-work-area-size",   .value = TCFG_WORK_AREA_SIZE },
4600         { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
4601         { .name = "-endian" ,          .value = TCFG_ENDIAN },
4602         { .name = "-coreid",           .value = TCFG_COREID },
4603         { .name = "-chain-position",   .value = TCFG_CHAIN_POSITION },
4604         { .name = "-dbgbase",          .value = TCFG_DBGBASE },
4605         { .name = "-rtos",             .value = TCFG_RTOS },
4606         { .name = "-defer-examine",    .value = TCFG_DEFER_EXAMINE },
4607         { .name = "-gdb-port",         .value = TCFG_GDB_PORT },
4608         { .name = NULL, .value = -1 }
4609 };
4610
4611 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
4612 {
4613         Jim_Nvp *n;
4614         Jim_Obj *o;
4615         jim_wide w;
4616         int e;
4617
4618         /* parse config or cget options ... */
4619         while (goi->argc > 0) {
4620                 Jim_SetEmptyResult(goi->interp);
4621                 /* Jim_GetOpt_Debug(goi); */
4622
4623                 if (target->type->target_jim_configure) {
4624                         /* target defines a configure function */
4625                         /* target gets first dibs on parameters */
4626                         e = (*(target->type->target_jim_configure))(target, goi);
4627                         if (e == JIM_OK) {
4628                                 /* more? */
4629                                 continue;
4630                         }
4631                         if (e == JIM_ERR) {
4632                                 /* An error */
4633                                 return e;
4634                         }
4635                         /* otherwise we 'continue' below */
4636                 }
4637                 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
4638                 if (e != JIM_OK) {
4639                         Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
4640                         return e;
4641                 }
4642                 switch (n->value) {
4643                 case TCFG_TYPE:
4644                         /* not setable */
4645                         if (goi->isconfigure) {
4646                                 Jim_SetResultFormatted(goi->interp,
4647                                                 "not settable: %s", n->name);
4648                                 return JIM_ERR;
4649                         } else {
4650 no_params:
4651                                 if (goi->argc != 0) {
4652                                         Jim_WrongNumArgs(goi->interp,
4653                                                         goi->argc, goi->argv,
4654                                                         "NO PARAMS");
4655                                         return JIM_ERR;
4656                                 }
4657                         }
4658                         Jim_SetResultString(goi->interp,
4659                                         target_type_name(target), -1);
4660                         /* loop for more */
4661                         break;
4662                 case TCFG_EVENT:
4663                         if (goi->argc == 0) {
4664                                 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
4665                                 return JIM_ERR;
4666                         }
4667
4668                         e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
4669                         if (e != JIM_OK) {
4670                                 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
4671                                 return e;
4672                         }
4673
4674                         if (goi->isconfigure) {
4675                                 if (goi->argc != 1) {
4676                                         Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
4677                                         return JIM_ERR;
4678                                 }
4679                         } else {
4680                                 if (goi->argc != 0) {
4681                                         Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
4682                                         return JIM_ERR;
4683                                 }
4684                         }
4685
4686                         {
4687                                 struct target_event_action *teap;
4688
4689                                 teap = target->event_action;
4690                                 /* replace existing? */
4691                                 while (teap) {
4692                                         if (teap->event == (enum target_event)n->value)
4693                                                 break;
4694                                         teap = teap->next;
4695                                 }
4696
4697                                 if (goi->isconfigure) {
4698                                         bool replace = true;
4699                                         if (teap == NULL) {
4700                                                 /* create new */
4701                                                 teap = calloc(1, sizeof(*teap));
4702                                                 replace = false;
4703                                         }
4704                                         teap->event = n->value;
4705                                         teap->interp = goi->interp;
4706                                         Jim_GetOpt_Obj(goi, &o);
4707                                         if (teap->body)
4708                                                 Jim_DecrRefCount(teap->interp, teap->body);
4709                                         teap->body  = Jim_DuplicateObj(goi->interp, o);
4710                                         /*
4711                                          * FIXME:
4712                                          *     Tcl/TK - "tk events" have a nice feature.
4713                                          *     See the "BIND" command.
4714                                          *    We should support that here.
4715                                          *     You can specify %X and %Y in the event code.
4716                                          *     The idea is: %T - target name.
4717                                          *     The idea is: %N - target number
4718                                          *     The idea is: %E - event name.
4719                                          */
4720                                         Jim_IncrRefCount(teap->body);
4721
4722                                         if (!replace) {
4723                                                 /* add to head of event list */
4724                                                 teap->next = target->event_action;
4725                                                 target->event_action = teap;
4726                                         }
4727                                         Jim_SetEmptyResult(goi->interp);
4728                                 } else {
4729                                         /* get */
4730                                         if (teap == NULL)
4731                                                 Jim_SetEmptyResult(goi->interp);
4732                                         else
4733                                                 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
4734                                 }
4735                         }
4736                         /* loop for more */
4737                         break;
4738
4739                 case TCFG_WORK_AREA_VIRT:
4740                         if (goi->isconfigure) {
4741                                 target_free_all_working_areas(target);
4742                                 e = Jim_GetOpt_Wide(goi, &w);
4743                                 if (e != JIM_OK)
4744                                         return e;
4745                                 target->working_area_virt = w;
4746                                 target->working_area_virt_spec = true;
4747                         } else {
4748                                 if (goi->argc != 0)
4749                                         goto no_params;
4750                         }
4751                         Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
4752                         /* loop for more */
4753                         break;
4754
4755                 case TCFG_WORK_AREA_PHYS:
4756                         if (goi->isconfigure) {
4757                                 target_free_all_working_areas(target);
4758                                 e = Jim_GetOpt_Wide(goi, &w);
4759                                 if (e != JIM_OK)
4760                                         return e;
4761                                 target->working_area_phys = w;
4762                                 target->working_area_phys_spec = true;
4763                         } else {
4764                                 if (goi->argc != 0)
4765                                         goto no_params;
4766                         }
4767                         Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
4768                         /* loop for more */
4769                         break;
4770
4771                 case TCFG_WORK_AREA_SIZE:
4772                         if (goi->isconfigure) {
4773                                 target_free_all_working_areas(target);
4774                                 e = Jim_GetOpt_Wide(goi, &w);
4775                                 if (e != JIM_OK)
4776                                         return e;
4777                                 target->working_area_size = w;
4778                         } else {
4779                                 if (goi->argc != 0)
4780                                         goto no_params;
4781                         }
4782                         Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4783                         /* loop for more */
4784                         break;
4785
4786                 case TCFG_WORK_AREA_BACKUP:
4787                         if (goi->isconfigure) {
4788                                 target_free_all_working_areas(target);
4789                                 e = Jim_GetOpt_Wide(goi, &w);
4790                                 if (e != JIM_OK)
4791                                         return e;
4792                                 /* make this exactly 1 or 0 */
4793                                 target->backup_working_area = (!!w);
4794                         } else {
4795                                 if (goi->argc != 0)
4796                                         goto no_params;
4797                         }
4798                         Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
4799                         /* loop for more e*/
4800                         break;
4801
4802
4803                 case TCFG_ENDIAN:
4804                         if (goi->isconfigure) {
4805                                 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
4806                                 if (e != JIM_OK) {
4807                                         Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
4808                                         return e;
4809                                 }
4810                                 target->endianness = n->value;
4811                         } else {
4812                                 if (goi->argc != 0)
4813                                         goto no_params;
4814                         }
4815                         n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4816                         if (n->name == NULL) {
4817                                 target->endianness = TARGET_LITTLE_ENDIAN;
4818                                 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4819                         }
4820                         Jim_SetResultString(goi->interp, n->name, -1);
4821                         /* loop for more */
4822                         break;
4823
4824                 case TCFG_COREID:
4825                         if (goi->isconfigure) {
4826                                 e = Jim_GetOpt_Wide(goi, &w);
4827                                 if (e != JIM_OK)
4828                                         return e;
4829                                 target->coreid = (int32_t)w;
4830                         } else {
4831                                 if (goi->argc != 0)
4832                                         goto no_params;
4833                         }
4834                         Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4835                         /* loop for more */
4836                         break;
4837
4838                 case TCFG_CHAIN_POSITION:
4839                         if (goi->isconfigure) {
4840                                 Jim_Obj *o_t;
4841                                 struct jtag_tap *tap;
4842
4843                                 if (target->has_dap) {
4844                                         Jim_SetResultString(goi->interp,
4845                                                 "target requires -dap parameter instead of -chain-position!", -1);
4846                                         return JIM_ERR;
4847                                 }
4848
4849                                 target_free_all_working_areas(target);
4850                                 e = Jim_GetOpt_Obj(goi, &o_t);
4851                                 if (e != JIM_OK)
4852                                         return e;
4853                                 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
4854                                 if (tap == NULL)
4855                                         return JIM_ERR;
4856                                 target->tap = tap;
4857                                 target->tap_configured = true;
4858                         } else {
4859                                 if (goi->argc != 0)
4860                                         goto no_params;
4861                         }
4862                         Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
4863                         /* loop for more e*/
4864                         break;
4865                 case TCFG_DBGBASE:
4866                         if (goi->isconfigure) {
4867                                 e = Jim_GetOpt_Wide(goi, &w);
4868                                 if (e != JIM_OK)
4869                                         return e;
4870                                 target->dbgbase = (uint32_t)w;
4871                                 target->dbgbase_set = true;
4872                         } else {
4873                                 if (goi->argc != 0)
4874                                         goto no_params;
4875                         }
4876                         Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
4877                         /* loop for more */
4878                         break;
4879                 case TCFG_RTOS:
4880                         /* RTOS */
4881                         {
4882                                 int result = rtos_create(goi, target);
4883                                 if (result != JIM_OK)
4884                                         return result;
4885                         }
4886                         /* loop for more */
4887                         break;
4888
4889                 case TCFG_DEFER_EXAMINE:
4890                         /* DEFER_EXAMINE */
4891                         target->defer_examine = true;
4892                         /* loop for more */
4893                         break;
4894
4895                 case TCFG_GDB_PORT:
4896                         if (goi->isconfigure) {
4897                                 const char *s;
4898                                 e = Jim_GetOpt_String(goi, &s, NULL);
4899                                 if (e != JIM_OK)
4900                                         return e;
4901                                 target->gdb_port_override = strdup(s);
4902                         } else {
4903                                 if (goi->argc != 0)
4904                                         goto no_params;
4905                         }
4906                         Jim_SetResultString(goi->interp, target->gdb_port_override ? : "undefined", -1);
4907                         /* loop for more */
4908                         break;
4909                 }
4910         } /* while (goi->argc) */
4911
4912
4913                 /* done - we return */
4914         return JIM_OK;
4915 }
4916
4917 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
4918 {
4919         Jim_GetOptInfo goi;
4920
4921         Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4922         goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
4923         if (goi.argc < 1) {
4924                 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4925                                  "missing: -option ...");
4926                 return JIM_ERR;
4927         }
4928         struct target *target = Jim_CmdPrivData(goi.interp);
4929         return target_configure(&goi, target);
4930 }
4931
4932 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4933 {
4934         const char *cmd_name = Jim_GetString(argv[0], NULL);
4935
4936         Jim_GetOptInfo goi;
4937         Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4938
4939         if (goi.argc < 2 || goi.argc > 4) {
4940                 Jim_SetResultFormatted(goi.interp,
4941                                 "usage: %s [phys] <address> <data> [<count>]", cmd_name);
4942                 return JIM_ERR;
4943         }
4944
4945         target_write_fn fn;
4946         fn = target_write_memory;
4947
4948         int e;
4949         if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0) {
4950                 /* consume it */
4951                 struct Jim_Obj *obj;
4952                 e = Jim_GetOpt_Obj(&goi, &obj);
4953                 if (e != JIM_OK)
4954                         return e;
4955
4956                 fn = target_write_phys_memory;
4957         }
4958
4959         jim_wide a;
4960         e = Jim_GetOpt_Wide(&goi, &a);
4961         if (e != JIM_OK)
4962                 return e;
4963
4964         jim_wide b;
4965         e = Jim_GetOpt_Wide(&goi, &b);
4966         if (e != JIM_OK)
4967                 return e;
4968
4969         jim_wide c = 1;
4970         if (goi.argc == 1) {
4971                 e = Jim_GetOpt_Wide(&goi, &c);
4972                 if (e != JIM_OK)
4973                         return e;
4974         }
4975
4976         /* all args must be consumed */
4977         if (goi.argc != 0)
4978                 return JIM_ERR;
4979
4980         struct target *target = Jim_CmdPrivData(goi.interp);
4981         unsigned data_size;
4982         if (strcasecmp(cmd_name, "mww") == 0)
4983                 data_size = 4;
4984         else if (strcasecmp(cmd_name, "mwh") == 0)
4985                 data_size = 2;
4986         else if (strcasecmp(cmd_name, "mwb") == 0)
4987                 data_size = 1;
4988         else {
4989                 LOG_ERROR("command '%s' unknown: ", cmd_name);
4990                 return JIM_ERR;
4991         }
4992
4993         return (target_fill_mem(target, a, fn, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
4994 }
4995
4996 /**
4997 *  @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4998 *
4999 *  Usage: mdw [phys] <address> [<count>] - for 32 bit reads
5000 *         mdh [phys] <address> [<count>] - for 16 bit reads
5001 *         mdb [phys] <address> [<count>] - for  8 bit reads
5002 *
5003 *  Count defaults to 1.
5004 *
5005 *  Calls target_read_memory or target_read_phys_memory depending on
5006 *  the presence of the "phys" argument
5007 *  Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
5008 *  to int representation in base16.
5009 *  Also outputs read data in a human readable form using command_print
5010 *
5011 *  @param phys if present target_read_phys_memory will be used instead of target_read_memory
5012 *  @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
5013 *  @param count optional count parameter to read an array of values. If not specified, defaults to 1.
5014 *  @returns:  JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
5015 *  on success, with [<count>] number of elements.
5016 *
5017 *  In case of little endian target:
5018 *      Example1: "mdw 0x00000000"  returns "10123456"
5019 *      Exmaple2: "mdh 0x00000000 1" returns "3456"
5020 *      Example3: "mdb 0x00000000" returns "56"
5021 *      Example4: "mdh 0x00000000 2" returns "3456 1012"
5022 *      Example5: "mdb 0x00000000 3" returns "56 34 12"
5023 **/
5024 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5025 {
5026         const char *cmd_name = Jim_GetString(argv[0], NULL);
5027
5028         Jim_GetOptInfo goi;
5029         Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5030
5031         if ((goi.argc < 1) || (goi.argc > 3)) {
5032                 Jim_SetResultFormatted(goi.interp,
5033                                 "usage: %s [phys] <address> [<count>]", cmd_name);
5034                 return JIM_ERR;
5035         }
5036
5037         int (*fn)(struct target *target,
5038                         target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer);
5039         fn = target_read_memory;
5040
5041         int e;
5042         if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0) {
5043                 /* consume it */
5044                 struct Jim_Obj *obj;
5045                 e = Jim_GetOpt_Obj(&goi, &obj);
5046                 if (e != JIM_OK)
5047                         return e;
5048
5049                 fn = target_read_phys_memory;
5050         }
5051
5052         /* Read address parameter */
5053         jim_wide addr;
5054         e = Jim_GetOpt_Wide(&goi, &addr);
5055         if (e != JIM_OK)
5056                 return JIM_ERR;
5057
5058         /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
5059         jim_wide count;
5060         if (goi.argc == 1) {
5061                 e = Jim_GetOpt_Wide(&goi, &count);
5062                 if (e != JIM_OK)
5063                         return JIM_ERR;
5064         } else
5065                 count = 1;
5066
5067         /* all args must be consumed */
5068         if (goi.argc != 0)
5069                 return JIM_ERR;
5070
5071         jim_wide dwidth = 1; /* shut up gcc */
5072         if (strcasecmp(cmd_name, "mdw") == 0)
5073                 dwidth = 4;
5074         else if (strcasecmp(cmd_name, "mdh") == 0)
5075                 dwidth = 2;
5076         else if (strcasecmp(cmd_name, "mdb") == 0)
5077                 dwidth = 1;
5078         else {
5079                 LOG_ERROR("command '%s' unknown: ", cmd_name);
5080                 return JIM_ERR;
5081         }
5082
5083         /* convert count to "bytes" */
5084         int bytes = count * dwidth;
5085
5086         struct target *target = Jim_CmdPrivData(goi.interp);
5087         uint8_t  target_buf[32];
5088         jim_wide x, y, z;
5089         while (bytes > 0) {
5090                 y = (bytes < 16) ? bytes : 16; /* y = min(bytes, 16); */
5091
5092                 /* Try to read out next block */
5093                 e = fn(target, addr, dwidth, y / dwidth, target_buf);
5094
5095                 if (e != ERROR_OK) {
5096                         Jim_SetResultFormatted(interp, "error reading target @ 0x%08lx", (long)addr);
5097                         return JIM_ERR;
5098                 }
5099
5100                 command_print_sameline(NULL, "0x%08x ", (int)(addr));
5101                 switch (dwidth) {
5102                 case 4:
5103                         for (x = 0; x < 16 && x < y; x += 4) {
5104                                 z = target_buffer_get_u32(target, &(target_buf[x]));
5105                                 command_print_sameline(NULL, "%08x ", (int)(z));
5106                         }
5107                         for (; (x < 16) ; x += 4)
5108                                 command_print_sameline(NULL, "         ");
5109                         break;
5110                 case 2:
5111                         for (x = 0; x < 16 && x < y; x += 2) {
5112                                 z = target_buffer_get_u16(target, &(target_buf[x]));
5113                                 command_print_sameline(NULL, "%04x ", (int)(z));
5114                         }
5115                         for (; (x < 16) ; x += 2)
5116                                 command_print_sameline(NULL, "     ");
5117                         break;
5118                 case 1:
5119                 default:
5120                         for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
5121                                 z = target_buffer_get_u8(target, &(target_buf[x]));
5122                                 command_print_sameline(NULL, "%02x ", (int)(z));
5123                         }
5124                         for (; (x < 16) ; x += 1)
5125                                 command_print_sameline(NULL, "   ");
5126                         break;
5127                 }
5128                 /* ascii-ify the bytes */
5129                 for (x = 0 ; x < y ; x++) {
5130                         if ((target_buf[x] >= 0x20) &&
5131                                 (target_buf[x] <= 0x7e)) {
5132                                 /* good */
5133                         } else {
5134                                 /* smack it */
5135                                 target_buf[x] = '.';
5136                         }
5137                 }
5138                 /* space pad  */
5139                 while (x < 16) {
5140                         target_buf[x] = ' ';
5141                         x++;
5142                 }
5143                 /* terminate */
5144                 target_buf[16] = 0;
5145                 /* print - with a newline */
5146                 command_print_sameline(NULL, "%s\n", target_buf);
5147                 /* NEXT... */
5148                 bytes -= 16;
5149                 addr += 16;
5150         }
5151         return JIM_OK;
5152 }
5153
5154 static int jim_target_mem2array(Jim_Interp *interp,
5155                 int argc, Jim_Obj *const *argv)
5156 {
5157         struct target *target = Jim_CmdPrivData(interp);
5158         return target_mem2array(interp, target, argc - 1, argv + 1);
5159 }
5160
5161 static int jim_target_array2mem(Jim_Interp *interp,
5162                 int argc, Jim_Obj *const *argv)
5163 {
5164         struct target *target = Jim_CmdPrivData(interp);
5165         return target_array2mem(interp, target, argc - 1, argv + 1);
5166 }
5167
5168 static int jim_target_tap_disabled(Jim_Interp *interp)
5169 {
5170         Jim_SetResultFormatted(interp, "[TAP is disabled]");
5171         return JIM_ERR;
5172 }
5173
5174 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5175 {
5176         bool allow_defer = false;
5177
5178         Jim_GetOptInfo goi;
5179         Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5180         if (goi.argc > 1) {
5181                 const char *cmd_name = Jim_GetString(argv[0], NULL);
5182                 Jim_SetResultFormatted(goi.interp,
5183                                 "usage: %s ['allow-defer']", cmd_name);
5184                 return JIM_ERR;
5185         }
5186         if (goi.argc > 0 &&
5187             strcmp(Jim_GetString(argv[1], NULL), "allow-defer") == 0) {
5188                 /* consume it */
5189                 struct Jim_Obj *obj;
5190                 int e = Jim_GetOpt_Obj(&goi, &obj);
5191                 if (e != JIM_OK)
5192                         return e;
5193                 allow_defer = true;
5194         }
5195
5196         struct target *target = Jim_CmdPrivData(interp);
5197         if (!target->tap->enabled)
5198                 return jim_target_tap_disabled(interp);
5199
5200         if (allow_defer && target->defer_examine) {
5201                 LOG_INFO("Deferring arp_examine of %s", target_name(target));
5202                 LOG_INFO("Use arp_examine command to examine it manually!");
5203                 return JIM_OK;
5204         }
5205
5206         int e = target->type->examine(target);
5207         if (e != ERROR_OK)
5208                 return JIM_ERR;
5209         return JIM_OK;
5210 }
5211
5212 static int jim_target_was_examined(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
5213 {
5214         struct target *target = Jim_CmdPrivData(interp);
5215
5216         Jim_SetResultBool(interp, target_was_examined(target));
5217         return JIM_OK;
5218 }
5219
5220 static int jim_target_examine_deferred(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
5221 {
5222         struct target *target = Jim_CmdPrivData(interp);
5223
5224         Jim_SetResultBool(interp, target->defer_examine);
5225         return JIM_OK;
5226 }
5227
5228 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5229 {
5230         if (argc != 1) {
5231                 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5232                 return JIM_ERR;
5233         }
5234         struct target *target = Jim_CmdPrivData(interp);
5235
5236         if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
5237                 return JIM_ERR;
5238
5239         return JIM_OK;
5240 }
5241
5242 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5243 {
5244         if (argc != 1) {
5245                 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5246                 return JIM_ERR;
5247         }
5248         struct target *target = Jim_CmdPrivData(interp);
5249         if (!target->tap->enabled)
5250                 return jim_target_tap_disabled(interp);
5251
5252         int e;
5253         if (!(target_was_examined(target)))
5254                 e = ERROR_TARGET_NOT_EXAMINED;
5255         else
5256                 e = target->type->poll(target);
5257         if (e != ERROR_OK)
5258                 return JIM_ERR;
5259         return JIM_OK;
5260 }
5261
5262 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5263 {
5264         Jim_GetOptInfo goi;
5265         Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5266
5267         if (goi.argc != 2) {
5268                 Jim_WrongNumArgs(interp, 0, argv,
5269                                 "([tT]|[fF]|assert|deassert) BOOL");
5270                 return JIM_ERR;
5271         }
5272
5273         Jim_Nvp *n;
5274         int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
5275         if (e != JIM_OK) {
5276                 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
5277                 return e;
5278         }
5279         /* the halt or not param */
5280         jim_wide a;
5281         e = Jim_GetOpt_Wide(&goi, &a);
5282         if (e != JIM_OK)
5283                 return e;
5284
5285         struct target *target = Jim_CmdPrivData(goi.interp);
5286         if (!target->tap->enabled)
5287                 return jim_target_tap_disabled(interp);
5288
5289         if (!target->type->assert_reset || !target->type->deassert_reset) {
5290                 Jim_SetResultFormatted(interp,
5291                                 "No target-specific reset for %s",
5292                                 target_name(target));
5293                 return JIM_ERR;
5294         }
5295
5296         if (target->defer_examine)
5297                 target_reset_examined(target);
5298
5299         /* determine if we should halt or not. */
5300         target->reset_halt = !!a;
5301         /* When this happens - all workareas are invalid. */
5302         target_free_all_working_areas_restore(target, 0);
5303
5304         /* do the assert */
5305         if (n->value == NVP_ASSERT)
5306                 e = target->type->assert_reset(target);
5307         else
5308                 e = target->type->deassert_reset(target);
5309         return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
5310 }
5311
5312 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5313 {
5314         if (argc != 1) {
5315                 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5316                 return JIM_ERR;
5317         }
5318         struct target *target = Jim_CmdPrivData(interp);
5319         if (!target->tap->enabled)
5320                 return jim_target_tap_disabled(interp);
5321         int e = target->type->halt(target);
5322         return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
5323 }
5324
5325 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5326 {
5327         Jim_GetOptInfo goi;
5328         Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5329
5330         /* params:  <name>  statename timeoutmsecs */
5331         if (goi.argc != 2) {
5332                 const char *cmd_name = Jim_GetString(argv[0], NULL);
5333                 Jim_SetResultFormatted(goi.interp,
5334                                 "%s <state_name> <timeout_in_msec>", cmd_name);
5335                 return JIM_ERR;
5336         }
5337
5338         Jim_Nvp *n;
5339         int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
5340         if (e != JIM_OK) {
5341                 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state, 1);
5342                 return e;
5343         }
5344         jim_wide a;
5345         e = Jim_GetOpt_Wide(&goi, &a);
5346         if (e != JIM_OK)
5347                 return e;
5348         struct target *target = Jim_CmdPrivData(interp);
5349         if (!target->tap->enabled)
5350                 return jim_target_tap_disabled(interp);
5351
5352         e = target_wait_state(target, n->value, a);
5353         if (e != ERROR_OK) {
5354                 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
5355                 Jim_SetResultFormatted(goi.interp,
5356                                 "target: %s wait %s fails (%#s) %s",
5357                                 target_name(target), n->name,
5358                                 eObj, target_strerror_safe(e));
5359                 Jim_FreeNewObj(interp, eObj);
5360                 return JIM_ERR;
5361         }
5362         return JIM_OK;
5363 }
5364 /* List for human, Events defined for this target.
5365  * scripts/programs should use 'name cget -event NAME'
5366  */
5367 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5368 {
5369         struct command_context *cmd_ctx = current_command_context(interp);
5370         assert(cmd_ctx != NULL);
5371
5372         struct target *target = Jim_CmdPrivData(interp);
5373         struct target_event_action *teap = target->event_action;
5374         command_print(cmd_ctx, "Event actions for target (%d) %s\n",
5375                                    target->target_number,
5376                                    target_name(target));
5377         command_print(cmd_ctx, "%-25s | Body", "Event");
5378         command_print(cmd_ctx, "------------------------- | "
5379                         "----------------------------------------");
5380         while (teap) {
5381                 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
5382                 command_print(cmd_ctx, "%-25s | %s",
5383                                 opt->name, Jim_GetString(teap->body, NULL));
5384                 teap = teap->next;
5385         }
5386         command_print(cmd_ctx, "***END***");
5387         return JIM_OK;
5388 }
5389 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5390 {
5391         if (argc != 1) {
5392                 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
5393                 return JIM_ERR;
5394         }
5395         struct target *target = Jim_CmdPrivData(interp);
5396         Jim_SetResultString(interp, target_state_name(target), -1);
5397         return JIM_OK;
5398 }
5399 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5400 {
5401         Jim_GetOptInfo goi;
5402         Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5403         if (goi.argc != 1) {
5404                 const char *cmd_name = Jim_GetString(argv[0], NULL);
5405                 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
5406                 return JIM_ERR;
5407         }
5408         Jim_Nvp *n;
5409         int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
5410         if (e != JIM_OK) {
5411                 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
5412                 return e;
5413         }
5414         struct target *target = Jim_CmdPrivData(interp);
5415         target_handle_event(target, n->value);
5416         return JIM_OK;
5417 }
5418
5419 static const struct command_registration target_instance_command_handlers[] = {
5420         {
5421                 .name = "configure",
5422                 .mode = COMMAND_CONFIG,
5423                 .jim_handler = jim_target_configure,
5424                 .help  = "configure a new target for use",
5425                 .usage = "[target_attribute ...]",
5426         },
5427         {
5428                 .name = "cget",
5429                 .mode = COMMAND_ANY,
5430                 .jim_handler = jim_target_configure,
5431                 .help  = "returns the specified target attribute",
5432                 .usage = "target_attribute",
5433         },
5434         {
5435                 .name = "mww",
5436                 .mode = COMMAND_EXEC,
5437                 .jim_handler = jim_target_mw,
5438                 .help = "Write 32-bit word(s) to target memory",
5439                 .usage = "address data [count]",
5440         },
5441         {
5442                 .name = "mwh",
5443                 .mode = COMMAND_EXEC,
5444                 .jim_handler = jim_target_mw,
5445                 .help = "Write 16-bit half-word(s) to target memory",
5446                 .usage = "address data [count]",
5447         },
5448         {
5449                 .name = "mwb",
5450                 .mode = COMMAND_EXEC,
5451                 .jim_handler = jim_target_mw,
5452                 .help = "Write byte(s) to target memory",
5453                 .usage = "address data [count]",
5454         },
5455         {
5456                 .name = "mdw",
5457                 .mode = COMMAND_EXEC,
5458                 .jim_handler = jim_target_md,
5459                 .help = "Display target memory as 32-bit words",
5460                 .usage = "address [count]",
5461         },
5462         {
5463                 .name = "mdh",
5464                 .mode = COMMAND_EXEC,
5465                 .jim_handler = jim_target_md,
5466                 .help = "Display target memory as 16-bit half-words",
5467                 .usage = "address [count]",
5468         },
5469         {
5470                 .name = "mdb",
5471                 .mode = COMMAND_EXEC,
5472                 .jim_handler = jim_target_md,
5473                 .help = "Display target memory as 8-bit bytes",
5474                 .usage = "address [count]",
5475         },
5476         {
5477                 .name = "array2mem",
5478                 .mode = COMMAND_EXEC,
5479                 .jim_handler = jim_target_array2mem,
5480                 .help = "Writes Tcl array of 8/16/32 bit numbers "
5481                         "to target memory",
5482                 .usage = "arrayname bitwidth address count",
5483         },
5484         {
5485                 .name = "mem2array",
5486                 .mode = COMMAND_EXEC,
5487                 .jim_handler = jim_target_mem2array,
5488                 .help = "Loads Tcl array of 8/16/32 bit numbers "
5489                         "from target memory",
5490                 .usage = "arrayname bitwidth address count",
5491         },
5492         {
5493                 .name = "eventlist",
5494                 .mode = COMMAND_EXEC,
5495                 .jim_handler = jim_target_event_list,
5496                 .help = "displays a table of events defined for this target",
5497         },
5498         {
5499                 .name = "curstate",
5500                 .mode = COMMAND_EXEC,
5501                 .jim_handler = jim_target_current_state,
5502                 .help = "displays the current state of this target",
5503         },
5504         {
5505                 .name = "arp_examine",
5506                 .mode = COMMAND_EXEC,
5507                 .jim_handler = jim_target_examine,
5508                 .help = "used internally for reset processing",
5509                 .usage = "['allow-defer']",
5510         },
5511         {
5512                 .name = "was_examined",
5513                 .mode = COMMAND_EXEC,
5514                 .jim_handler = jim_target_was_examined,
5515                 .help = "used internally for reset processing",
5516         },
5517         {
5518                 .name = "examine_deferred",
5519                 .mode = COMMAND_EXEC,
5520                 .jim_handler = jim_target_examine_deferred,
5521                 .help = "used internally for reset processing",
5522         },
5523         {
5524                 .name = "arp_halt_gdb",
5525                 .mode = COMMAND_EXEC,
5526                 .jim_handler = jim_target_halt_gdb,
5527                 .help = "used internally for reset processing to halt GDB",
5528         },
5529         {
5530                 .name = "arp_poll",
5531                 .mode = COMMAND_EXEC,
5532                 .jim_handler = jim_target_poll,
5533                 .help = "used internally for reset processing",
5534         },
5535         {
5536                 .name = "arp_reset",
5537                 .mode = COMMAND_EXEC,
5538                 .jim_handler = jim_target_reset,
5539                 .help = "used internally for reset processing",
5540         },
5541         {
5542                 .name = "arp_halt",
5543                 .mode = COMMAND_EXEC,
5544                 .jim_handler = jim_target_halt,
5545                 .help = "used internally for reset processing",
5546         },
5547         {
5548                 .name = "arp_waitstate",
5549                 .mode = COMMAND_EXEC,
5550                 .jim_handler = jim_target_wait_state,
5551                 .help = "used internally for reset processing",
5552         },
5553         {
5554                 .name = "invoke-event",
5555                 .mode = COMMAND_EXEC,
5556                 .jim_handler = jim_target_invoke_event,
5557                 .help = "invoke handler for specified event",
5558                 .usage = "event_name",
5559         },
5560         COMMAND_REGISTRATION_DONE
5561 };
5562
5563 static int target_create(Jim_GetOptInfo *goi)
5564 {
5565         Jim_Obj *new_cmd;
5566         Jim_Cmd *cmd;
5567         const char *cp;
5568         int e;
5569         int x;
5570         struct target *target;
5571         struct command_context *cmd_ctx;
5572
5573         cmd_ctx = current_command_context(goi->interp);
5574         assert(cmd_ctx != NULL);
5575
5576         if (goi->argc < 3) {
5577                 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
5578                 return JIM_ERR;
5579         }
5580
5581         /* COMMAND */
5582         Jim_GetOpt_Obj(goi, &new_cmd);
5583         /* does this command exist? */
5584         cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
5585         if (cmd) {
5586                 cp = Jim_GetString(new_cmd, NULL);
5587                 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
5588                 return JIM_ERR;
5589         }
5590
5591         /* TYPE */
5592         e = Jim_GetOpt_String(goi, &cp, NULL);
5593         if (e != JIM_OK)
5594                 return e;
5595         struct transport *tr = get_current_transport();
5596         if (tr->override_target) {
5597                 e = tr->override_target(&cp);
5598                 if (e != ERROR_OK) {
5599                         LOG_ERROR("The selected transport doesn't support this target");
5600                         return JIM_ERR;
5601                 }
5602                 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5603         }
5604         /* now does target type exist */
5605         for (x = 0 ; target_types[x] ; x++) {
5606                 if (0 == strcmp(cp, target_types[x]->name)) {
5607                         /* found */
5608                         break;
5609                 }
5610
5611                 /* check for deprecated name */
5612                 if (target_types[x]->deprecated_name) {
5613                         if (0 == strcmp(cp, target_types[x]->deprecated_name)) {
5614                                 /* found */
5615                                 LOG_WARNING("target name is deprecated use: \'%s\'", target_types[x]->name);
5616                                 break;
5617                         }
5618                 }
5619         }
5620         if (target_types[x] == NULL) {
5621                 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
5622                 for (x = 0 ; target_types[x] ; x++) {
5623                         if (target_types[x + 1]) {
5624                                 Jim_AppendStrings(goi->interp,
5625                                                                    Jim_GetResult(goi->interp),
5626                                                                    target_types[x]->name,
5627                                                                    ", ", NULL);
5628                         } else {
5629                                 Jim_AppendStrings(goi->interp,
5630                                                                    Jim_GetResult(goi->interp),
5631                                                                    " or ",
5632                                                                    target_types[x]->name, NULL);
5633                         }
5634                 }
5635                 return JIM_ERR;
5636         }
5637
5638         /* Create it */
5639         target = calloc(1, sizeof(struct target));
5640         /* set target number */
5641         target->target_number = new_target_number();
5642         cmd_ctx->current_target = target;
5643
5644         /* allocate memory for each unique target type */
5645         target->type = calloc(1, sizeof(struct target_type));
5646
5647         memcpy(target->type, target_types[x], sizeof(struct target_type));
5648
5649         /* will be set by "-endian" */
5650         target->endianness = TARGET_ENDIAN_UNKNOWN;
5651
5652         /* default to first core, override with -coreid */
5653         target->coreid = 0;
5654
5655         target->working_area        = 0x0;
5656         target->working_area_size   = 0x0;
5657         target->working_areas       = NULL;
5658         target->backup_working_area = 0;
5659
5660         target->state               = TARGET_UNKNOWN;
5661         target->debug_reason        = DBG_REASON_UNDEFINED;
5662         target->reg_cache           = NULL;
5663         target->breakpoints         = NULL;
5664         target->watchpoints         = NULL;
5665         target->next                = NULL;
5666         target->arch_info           = NULL;
5667
5668         target->verbose_halt_msg        = true;
5669
5670         target->halt_issued                     = false;
5671
5672         /* initialize trace information */
5673         target->trace_info = calloc(1, sizeof(struct trace));
5674
5675         target->dbgmsg          = NULL;
5676         target->dbg_msg_enabled = 0;
5677
5678         target->endianness = TARGET_ENDIAN_UNKNOWN;
5679
5680         target->rtos = NULL;
5681         target->rtos_auto_detect = false;
5682
5683         target->gdb_port_override = NULL;
5684
5685         /* Do the rest as "configure" options */
5686         goi->isconfigure = 1;
5687         e = target_configure(goi, target);
5688
5689         if (e == JIM_OK) {
5690                 if (target->has_dap) {
5691                         if (!target->dap_configured) {
5692                                 Jim_SetResultString(goi->interp, "-dap ?name? required when creating target", -1);
5693                                 e = JIM_ERR;
5694                         }
5695                 } else {
5696                         if (!target->tap_configured) {
5697                                 Jim_SetResultString(goi->interp, "-chain-position ?name? required when creating target", -1);
5698                                 e = JIM_ERR;
5699                         }
5700                 }
5701                 /* tap must be set after target was configured */
5702                 if (target->tap == NULL)
5703                         e = JIM_ERR;
5704         }
5705
5706         if (e != JIM_OK) {
5707                 free(target->gdb_port_override);
5708                 free(target->type);
5709                 free(target);
5710                 return e;
5711         }
5712
5713         if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
5714                 /* default endian to little if not specified */
5715                 target->endianness = TARGET_LITTLE_ENDIAN;
5716         }
5717
5718         cp = Jim_GetString(new_cmd, NULL);
5719         target->cmd_name = strdup(cp);
5720
5721         if (target->type->target_create) {
5722                 e = (*(target->type->target_create))(target, goi->interp);
5723                 if (e != ERROR_OK) {
5724                         LOG_DEBUG("target_create failed");
5725                         free(target->gdb_port_override);
5726                         free(target->type);
5727                         free(target->cmd_name);
5728                         free(target);
5729                         return JIM_ERR;
5730                 }
5731         }
5732
5733         /* create the target specific commands */
5734         if (target->type->commands) {
5735                 e = register_commands(cmd_ctx, NULL, target->type->commands);
5736                 if (ERROR_OK != e)
5737                         LOG_ERROR("unable to register '%s' commands", cp);
5738         }
5739
5740         /* append to end of list */
5741         {
5742                 struct target **tpp;
5743                 tpp = &(all_targets);
5744                 while (*tpp)
5745                         tpp = &((*tpp)->next);
5746                 *tpp = target;
5747         }
5748
5749         /* now - create the new target name command */
5750         const struct command_registration target_subcommands[] = {
5751                 {
5752                         .chain = target_instance_command_handlers,
5753                 },
5754                 {
5755                         .chain = target->type->commands,
5756                 },
5757                 COMMAND_REGISTRATION_DONE
5758         };
5759         const struct command_registration target_commands[] = {
5760                 {
5761                         .name = cp,
5762                         .mode = COMMAND_ANY,
5763                         .help = "target command group",
5764                         .usage = "",
5765                         .chain = target_subcommands,
5766                 },
5767                 COMMAND_REGISTRATION_DONE
5768         };
5769         e = register_commands(cmd_ctx, NULL, target_commands);
5770         if (ERROR_OK != e)
5771                 return JIM_ERR;
5772
5773         struct command *c = command_find_in_context(cmd_ctx, cp);
5774         assert(c);
5775         command_set_handler_data(c, target);
5776
5777         return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
5778 }
5779
5780 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5781 {
5782         if (argc != 1) {
5783                 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5784                 return JIM_ERR;
5785         }
5786         struct command_context *cmd_ctx = current_command_context(interp);
5787         assert(cmd_ctx != NULL);
5788
5789         Jim_SetResultString(interp, target_name(get_current_target(cmd_ctx)), -1);
5790         return JIM_OK;
5791 }
5792
5793 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5794 {
5795         if (argc != 1) {
5796                 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5797                 return JIM_ERR;
5798         }
5799         Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5800         for (unsigned x = 0; NULL != target_types[x]; x++) {
5801                 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5802                         Jim_NewStringObj(interp, target_types[x]->name, -1));
5803         }
5804         return JIM_OK;
5805 }
5806
5807 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5808 {
5809         if (argc != 1) {
5810                 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5811                 return JIM_ERR;
5812         }
5813         Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5814         struct target *target = all_targets;
5815         while (target) {
5816                 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5817                         Jim_NewStringObj(interp, target_name(target), -1));
5818                 target = target->next;
5819         }
5820         return JIM_OK;
5821 }
5822
5823 static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5824 {
5825         int i;
5826         const char *targetname;
5827         int retval, len;
5828         struct target *target = (struct target *) NULL;
5829         struct target_list *head, *curr, *new;
5830         curr = (struct target_list *) NULL;
5831         head = (struct target_list *) NULL;
5832
5833         retval = 0;
5834         LOG_DEBUG("%d", argc);
5835         /* argv[1] = target to associate in smp
5836          * argv[2] = target to assoicate in smp
5837          * argv[3] ...
5838          */
5839
5840         for (i = 1; i < argc; i++) {
5841
5842                 targetname = Jim_GetString(argv[i], &len);
5843                 target = get_target(targetname);
5844                 LOG_DEBUG("%s ", targetname);
5845                 if (target) {
5846                         new = malloc(sizeof(struct target_list));
5847                         new->target = target;
5848                         new->next = (struct target_list *)NULL;
5849                         if (head == (struct target_list *)NULL) {
5850                                 head = new;
5851                                 curr = head;
5852                         } else {
5853                                 curr->next = new;
5854                                 curr = new;
5855                         }
5856                 }
5857         }
5858         /*  now parse the list of cpu and put the target in smp mode*/
5859         curr = head;
5860
5861         while (curr != (struct target_list *)NULL) {
5862                 target = curr->target;
5863                 target->smp = 1;
5864                 target->head = head;
5865                 curr = curr->next;
5866         }
5867
5868         if (target && target->rtos)
5869                 retval = rtos_smp_init(head->target);
5870
5871         return retval;
5872 }
5873
5874
5875 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5876 {
5877         Jim_GetOptInfo goi;
5878         Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5879         if (goi.argc < 3) {
5880                 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
5881                         "<name> <target_type> [<target_options> ...]");
5882                 return JIM_ERR;
5883         }
5884         return target_create(&goi);
5885 }
5886
5887 static const struct command_registration target_subcommand_handlers[] = {
5888         {
5889                 .name = "init",
5890                 .mode = COMMAND_CONFIG,
5891                 .handler = handle_target_init_command,
5892                 .help = "initialize targets",
5893         },
5894         {
5895                 .name = "create",
5896                 .mode = COMMAND_CONFIG,
5897                 .jim_handler = jim_target_create,
5898                 .usage = "name type '-chain-position' name [options ...]",
5899                 .help = "Creates and selects a new target",
5900         },
5901         {
5902                 .name = "current",
5903                 .mode = COMMAND_ANY,
5904                 .jim_handler = jim_target_current,
5905                 .help = "Returns the currently selected target",
5906         },
5907         {
5908                 .name = "types",
5909                 .mode = COMMAND_ANY,
5910                 .jim_handler = jim_target_types,
5911                 .help = "Returns the available target types as "
5912                                 "a list of strings",
5913         },
5914         {
5915                 .name = "names",
5916                 .mode = COMMAND_ANY,
5917                 .jim_handler = jim_target_names,
5918                 .help = "Returns the names of all targets as a list of strings",
5919         },
5920         {
5921                 .name = "smp",
5922                 .mode = COMMAND_ANY,
5923                 .jim_handler = jim_target_smp,
5924                 .usage = "targetname1 targetname2 ...",
5925                 .help = "gather several target in a smp list"
5926         },
5927
5928         COMMAND_REGISTRATION_DONE
5929 };
5930
5931 struct FastLoad {
5932         target_addr_t address;
5933         uint8_t *data;
5934         int length;
5935
5936 };
5937
5938 static int fastload_num;
5939 static struct FastLoad *fastload;
5940
5941 static void free_fastload(void)
5942 {
5943         if (fastload != NULL) {
5944                 int i;
5945                 for (i = 0; i < fastload_num; i++) {
5946                         if (fastload[i].data)
5947                                 free(fastload[i].data);
5948                 }
5949                 free(fastload);
5950                 fastload = NULL;
5951         }
5952 }
5953
5954 COMMAND_HANDLER(handle_fast_load_image_command)
5955 {
5956         uint8_t *buffer;
5957         size_t buf_cnt;
5958         uint32_t image_size;
5959         target_addr_t min_address = 0;
5960         target_addr_t max_address = -1;
5961         int i;
5962
5963         struct image image;
5964
5965         int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
5966                         &image, &min_address, &max_address);
5967         if (ERROR_OK != retval)
5968                 return retval;
5969
5970         struct duration bench;
5971         duration_start(&bench);
5972
5973         retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
5974         if (retval != ERROR_OK)
5975                 return retval;
5976
5977         image_size = 0x0;
5978         retval = ERROR_OK;
5979         fastload_num = image.num_sections;
5980         fastload = malloc(sizeof(struct FastLoad)*image.num_sections);
5981         if (fastload == NULL) {
5982                 command_print(CMD_CTX, "out of memory");
5983                 image_close(&image);
5984                 return ERROR_FAIL;
5985         }
5986         memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
5987         for (i = 0; i < image.num_sections; i++) {
5988                 buffer = malloc(image.sections[i].size);
5989                 if (buffer == NULL) {
5990                         command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5991                                                   (int)(image.sections[i].size));
5992                         retval = ERROR_FAIL;
5993                         break;
5994                 }
5995
5996                 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
5997                 if (retval != ERROR_OK) {
5998                         free(buffer);
5999                         break;
6000                 }
6001
6002                 uint32_t offset = 0;
6003                 uint32_t length = buf_cnt;
6004
6005                 /* DANGER!!! beware of unsigned comparision here!!! */
6006
6007                 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
6008                                 (image.sections[i].base_address < max_address)) {
6009                         if (image.sections[i].base_address < min_address) {
6010                                 /* clip addresses below */
6011                                 offset += min_address-image.sections[i].base_address;
6012                                 length -= offset;
6013                         }
6014
6015                         if (image.sections[i].base_address + buf_cnt > max_address)
6016                                 length -= (image.sections[i].base_address + buf_cnt)-max_address;
6017
6018                         fastload[i].address = image.sections[i].base_address + offset;
6019                         fastload[i].data = malloc(length);
6020                         if (fastload[i].data == NULL) {
6021                                 free(buffer);
6022                                 command_print(CMD_CTX, "error allocating buffer for section (%" PRIu32 " bytes)",
6023                                                           length);
6024                                 retval = ERROR_FAIL;
6025                                 break;
6026                         }
6027                         memcpy(fastload[i].data, buffer + offset, length);
6028                         fastload[i].length = length;
6029
6030                         image_size += length;
6031                         command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
6032                                                   (unsigned int)length,
6033                                                   ((unsigned int)(image.sections[i].base_address + offset)));
6034                 }
6035
6036                 free(buffer);
6037         }
6038
6039         if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
6040                 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
6041                                 "in %fs (%0.3f KiB/s)", image_size,
6042                                 duration_elapsed(&bench), duration_kbps(&bench, image_size));
6043
6044                 command_print(CMD_CTX,
6045                                 "WARNING: image has not been loaded to target!"
6046                                 "You can issue a 'fast_load' to finish loading.");
6047         }
6048
6049         image_close(&image);
6050
6051         if (retval != ERROR_OK)
6052                 free_fastload();
6053
6054         return retval;
6055 }
6056
6057 COMMAND_HANDLER(handle_fast_load_command)
6058 {
6059         if (CMD_ARGC > 0)
6060                 return ERROR_COMMAND_SYNTAX_ERROR;
6061         if (fastload == NULL) {
6062                 LOG_ERROR("No image in memory");
6063                 return ERROR_FAIL;
6064         }
6065         int i;
6066         int64_t ms = timeval_ms();
6067         int size = 0;
6068         int retval = ERROR_OK;
6069         for (i = 0; i < fastload_num; i++) {
6070                 struct target *target = get_current_target(CMD_CTX);
6071                 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
6072                                           (unsigned int)(fastload[i].address),
6073                                           (unsigned int)(fastload[i].length));
6074                 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
6075                 if (retval != ERROR_OK)
6076                         break;
6077                 size += fastload[i].length;
6078         }
6079         if (retval == ERROR_OK) {
6080                 int64_t after = timeval_ms();
6081                 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
6082         }
6083         return retval;
6084 }
6085
6086 static const struct command_registration target_command_handlers[] = {
6087         {
6088                 .name = "targets",
6089                 .handler = handle_targets_command,
6090                 .mode = COMMAND_ANY,
6091                 .help = "change current default target (one parameter) "
6092                         "or prints table of all targets (no parameters)",
6093                 .usage = "[target]",
6094         },
6095         {
6096                 .name = "target",
6097                 .mode = COMMAND_CONFIG,
6098                 .help = "configure target",
6099                 .chain = target_subcommand_handlers,
6100                 .usage = "",
6101         },
6102         COMMAND_REGISTRATION_DONE
6103 };
6104
6105 int target_register_commands(struct command_context *cmd_ctx)
6106 {
6107         return register_commands(cmd_ctx, NULL, target_command_handlers);
6108 }
6109
6110 static bool target_reset_nag = true;
6111
6112 bool get_target_reset_nag(void)
6113 {
6114         return target_reset_nag;
6115 }
6116
6117 COMMAND_HANDLER(handle_target_reset_nag)
6118 {
6119         return CALL_COMMAND_HANDLER(handle_command_parse_bool,
6120                         &target_reset_nag, "Nag after each reset about options to improve "
6121                         "performance");
6122 }
6123
6124 COMMAND_HANDLER(handle_ps_command)
6125 {
6126         struct target *target = get_current_target(CMD_CTX);
6127         char *display;
6128         if (target->state != TARGET_HALTED) {
6129                 LOG_INFO("target not halted !!");
6130                 return ERROR_OK;
6131         }
6132
6133         if ((target->rtos) && (target->rtos->type)
6134                         && (target->rtos->type->ps_command)) {
6135                 display = target->rtos->type->ps_command(target);
6136                 command_print(CMD_CTX, "%s", display);
6137                 free(display);
6138                 return ERROR_OK;
6139         } else {
6140                 LOG_INFO("failed");
6141                 return ERROR_TARGET_FAILURE;
6142         }
6143 }
6144
6145 static void binprint(struct command_context *cmd_ctx, const char *text, const uint8_t *buf, int size)
6146 {
6147         if (text != NULL)
6148                 command_print_sameline(cmd_ctx, "%s", text);
6149         for (int i = 0; i < size; i++)
6150                 command_print_sameline(cmd_ctx, " %02x", buf[i]);
6151         command_print(cmd_ctx, " ");
6152 }
6153
6154 COMMAND_HANDLER(handle_test_mem_access_command)
6155 {
6156         struct target *target = get_current_target(CMD_CTX);
6157         uint32_t test_size;
6158         int retval = ERROR_OK;
6159
6160         if (target->state != TARGET_HALTED) {
6161                 LOG_INFO("target not halted !!");
6162                 return ERROR_FAIL;
6163         }
6164
6165         if (CMD_ARGC != 1)
6166                 return ERROR_COMMAND_SYNTAX_ERROR;
6167
6168         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], test_size);
6169
6170         /* Test reads */
6171         size_t num_bytes = test_size + 4;
6172
6173         struct working_area *wa = NULL;
6174         retval = target_alloc_working_area(target, num_bytes, &wa);
6175         if (retval != ERROR_OK) {
6176                 LOG_ERROR("Not enough working area");
6177                 return ERROR_FAIL;
6178         }
6179
6180         uint8_t *test_pattern = malloc(num_bytes);
6181
6182         for (size_t i = 0; i < num_bytes; i++)
6183                 test_pattern[i] = rand();
6184
6185         retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6186         if (retval != ERROR_OK) {
6187                 LOG_ERROR("Test pattern write failed");
6188                 goto out;
6189         }
6190
6191         for (int host_offset = 0; host_offset <= 1; host_offset++) {
6192                 for (int size = 1; size <= 4; size *= 2) {
6193                         for (int offset = 0; offset < 4; offset++) {
6194                                 uint32_t count = test_size / size;
6195                                 size_t host_bufsiz = (count + 2) * size + host_offset;
6196                                 uint8_t *read_ref = malloc(host_bufsiz);
6197                                 uint8_t *read_buf = malloc(host_bufsiz);
6198
6199                                 for (size_t i = 0; i < host_bufsiz; i++) {
6200                                         read_ref[i] = rand();
6201                                         read_buf[i] = read_ref[i];
6202                                 }
6203                                 command_print_sameline(CMD_CTX,
6204                                                 "Test read %" PRIu32 " x %d @ %d to %saligned buffer: ", count,
6205                                                 size, offset, host_offset ? "un" : "");
6206
6207                                 struct duration bench;
6208                                 duration_start(&bench);
6209
6210                                 retval = target_read_memory(target, wa->address + offset, size, count,
6211                                                 read_buf + size + host_offset);
6212
6213                                 duration_measure(&bench);
6214
6215                                 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6216                                         command_print(CMD_CTX, "Unsupported alignment");
6217                                         goto next;
6218                                 } else if (retval != ERROR_OK) {
6219                                         command_print(CMD_CTX, "Memory read failed");
6220                                         goto next;
6221                                 }
6222
6223                                 /* replay on host */
6224                                 memcpy(read_ref + size + host_offset, test_pattern + offset, count * size);
6225
6226                                 /* check result */
6227                                 int result = memcmp(read_ref, read_buf, host_bufsiz);
6228                                 if (result == 0) {
6229                                         command_print(CMD_CTX, "Pass in %fs (%0.3f KiB/s)",
6230                                                         duration_elapsed(&bench),
6231                                                         duration_kbps(&bench, count * size));
6232                                 } else {
6233                                         command_print(CMD_CTX, "Compare failed");
6234                                         binprint(CMD_CTX, "ref:", read_ref, host_bufsiz);
6235                                         binprint(CMD_CTX, "buf:", read_buf, host_bufsiz);
6236                                 }
6237 next:
6238                                 free(read_ref);
6239                                 free(read_buf);
6240                         }
6241                 }
6242         }
6243
6244 out:
6245         free(test_pattern);
6246
6247         if (wa != NULL)
6248                 target_free_working_area(target, wa);
6249
6250         /* Test writes */
6251         num_bytes = test_size + 4 + 4 + 4;
6252
6253         retval = target_alloc_working_area(target, num_bytes, &wa);
6254         if (retval != ERROR_OK) {
6255                 LOG_ERROR("Not enough working area");
6256                 return ERROR_FAIL;
6257         }
6258
6259         test_pattern = malloc(num_bytes);
6260
6261         for (size_t i = 0; i < num_bytes; i++)
6262                 test_pattern[i] = rand();
6263
6264         for (int host_offset = 0; host_offset <= 1; host_offset++) {
6265                 for (int size = 1; size <= 4; size *= 2) {
6266                         for (int offset = 0; offset < 4; offset++) {
6267                                 uint32_t count = test_size / size;
6268                                 size_t host_bufsiz = count * size + host_offset;
6269                                 uint8_t *read_ref = malloc(num_bytes);
6270                                 uint8_t *read_buf = malloc(num_bytes);
6271                                 uint8_t *write_buf = malloc(host_bufsiz);
6272
6273                                 for (size_t i = 0; i < host_bufsiz; i++)
6274                                         write_buf[i] = rand();
6275                                 command_print_sameline(CMD_CTX,
6276                                                 "Test write %" PRIu32 " x %d @ %d from %saligned buffer: ", count,
6277                                                 size, offset, host_offset ? "un" : "");
6278
6279                                 retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
6280                                 if (retval != ERROR_OK) {
6281                                         command_print(CMD_CTX, "Test pattern write failed");
6282                                         goto nextw;
6283                                 }
6284
6285                                 /* replay on host */
6286                                 memcpy(read_ref, test_pattern, num_bytes);
6287                                 memcpy(read_ref + size + offset, write_buf + host_offset, count * size);
6288
6289                                 struct duration bench;
6290                                 duration_start(&bench);
6291
6292                                 retval = target_write_memory(target, wa->address + size + offset, size, count,
6293                                                 write_buf + host_offset);
6294
6295                                 duration_measure(&bench);
6296
6297                                 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
6298                                         command_print(CMD_CTX, "Unsupported alignment");
6299                                         goto nextw;
6300                                 } else if (retval != ERROR_OK) {
6301                                         command_print(CMD_CTX, "Memory write failed");
6302                                         goto nextw;
6303                                 }
6304
6305                                 /* read back */
6306                                 retval = target_read_memory(target, wa->address, 1, num_bytes, read_buf);
6307                                 if (retval != ERROR_OK) {
6308                                         command_print(CMD_CTX, "Test pattern write failed");
6309                                         goto nextw;
6310                                 }
6311
6312                                 /* check result */
6313                                 int result = memcmp(read_ref, read_buf, num_bytes);
6314                                 if (result == 0) {
6315                                         command_print(CMD_CTX, "Pass in %fs (%0.3f KiB/s)",
6316                                                         duration_elapsed(&bench),
6317                                                         duration_kbps(&bench, count * size));
6318                                 } else {
6319                                         command_print(CMD_CTX, "Compare failed");
6320                                         binprint(CMD_CTX, "ref:", read_ref, num_bytes);
6321                                         binprint(CMD_CTX, "buf:", read_buf, num_bytes);
6322                                 }
6323 nextw:
6324                                 free(read_ref);
6325                                 free(read_buf);
6326                         }
6327                 }
6328         }
6329
6330         free(test_pattern);
6331
6332         if (wa != NULL)
6333                 target_free_working_area(target, wa);
6334         return retval;
6335 }
6336
6337 static const struct command_registration target_exec_command_handlers[] = {
6338         {
6339                 .name = "fast_load_image",
6340                 .handler = handle_fast_load_image_command,
6341                 .mode = COMMAND_ANY,
6342                 .help = "Load image into server memory for later use by "
6343                         "fast_load; primarily for profiling",
6344                 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
6345                         "[min_address [max_length]]",
6346         },
6347         {
6348                 .name = "fast_load",
6349                 .handler = handle_fast_load_command,
6350                 .mode = COMMAND_EXEC,
6351                 .help = "loads active fast load image to current target "
6352                         "- mainly for profiling purposes",
6353                 .usage = "",
6354         },
6355         {
6356                 .name = "profile",
6357                 .handler = handle_profile_command,
6358                 .mode = COMMAND_EXEC,
6359                 .usage = "seconds filename [start end]",
6360                 .help = "profiling samples the CPU PC",
6361         },
6362         /** @todo don't register virt2phys() unless target supports it */
6363         {
6364                 .name = "virt2phys",
6365                 .handler = handle_virt2phys_command,
6366                 .mode = COMMAND_ANY,
6367                 .help = "translate a virtual address into a physical address",
6368                 .usage = "virtual_address",
6369         },
6370         {
6371                 .name = "reg",
6372                 .handler = handle_reg_command,
6373                 .mode = COMMAND_EXEC,
6374                 .help = "display (reread from target with \"force\") or set a register; "
6375                         "with no arguments, displays all registers and their values",
6376                 .usage = "[(register_number|register_name) [(value|'force')]]",
6377         },
6378         {
6379                 .name = "poll",
6380                 .handler = handle_poll_command,
6381                 .mode = COMMAND_EXEC,
6382                 .help = "poll target state; or reconfigure background polling",
6383                 .usage = "['on'|'off']",
6384         },
6385         {
6386                 .name = "wait_halt",
6387                 .handler = handle_wait_halt_command,
6388                 .mode = COMMAND_EXEC,
6389                 .help = "wait up to the specified number of milliseconds "
6390                         "(default 5000) for a previously requested halt",
6391                 .usage = "[milliseconds]",
6392         },
6393         {
6394                 .name = "halt",
6395                 .handler = handle_halt_command,
6396                 .mode = COMMAND_EXEC,
6397                 .help = "request target to halt, then wait up to the specified"
6398                         "number of milliseconds (default 5000) for it to complete",
6399                 .usage = "[milliseconds]",
6400         },
6401         {
6402                 .name = "resume",
6403                 .handler = handle_resume_command,
6404                 .mode = COMMAND_EXEC,
6405                 .help = "resume target execution from current PC or address",
6406                 .usage = "[address]",
6407         },
6408         {
6409                 .name = "reset",
6410                 .handler = handle_reset_command,
6411                 .mode = COMMAND_EXEC,
6412                 .usage = "[run|halt|init]",
6413                 .help = "Reset all targets into the specified mode."
6414                         "Default reset mode is run, if not given.",
6415         },
6416         {
6417                 .name = "soft_reset_halt",
6418                 .handler = handle_soft_reset_halt_command,
6419                 .mode = COMMAND_EXEC,
6420                 .usage = "",
6421                 .help = "halt the target and do a soft reset",
6422         },
6423         {
6424                 .name = "step",
6425                 .handler = handle_step_command,
6426                 .mode = COMMAND_EXEC,
6427                 .help = "step one instruction from current PC or address",
6428                 .usage = "[address]",
6429         },
6430         {
6431                 .name = "mdd",
6432                 .handler = handle_md_command,
6433                 .mode = COMMAND_EXEC,
6434                 .help = "display memory words",
6435                 .usage = "['phys'] address [count]",
6436         },
6437         {
6438                 .name = "mdw",
6439                 .handler = handle_md_command,
6440                 .mode = COMMAND_EXEC,
6441                 .help = "display memory words",
6442                 .usage = "['phys'] address [count]",
6443         },
6444         {
6445                 .name = "mdh",
6446                 .handler = handle_md_command,
6447                 .mode = COMMAND_EXEC,
6448                 .help = "display memory half-words",
6449                 .usage = "['phys'] address [count]",
6450         },
6451         {
6452                 .name = "mdb",
6453                 .handler = handle_md_command,
6454                 .mode = COMMAND_EXEC,
6455                 .help = "display memory bytes",
6456                 .usage = "['phys'] address [count]",
6457         },
6458         {
6459                 .name = "mwd",
6460                 .handler = handle_mw_command,
6461                 .mode = COMMAND_EXEC,
6462                 .help = "write memory word",
6463                 .usage = "['phys'] address value [count]",
6464         },
6465         {
6466                 .name = "mww",
6467                 .handler = handle_mw_command,
6468                 .mode = COMMAND_EXEC,
6469                 .help = "write memory word",
6470                 .usage = "['phys'] address value [count]",
6471         },
6472         {
6473                 .name = "mwh",
6474                 .handler = handle_mw_command,
6475                 .mode = COMMAND_EXEC,
6476                 .help = "write memory half-word",
6477                 .usage = "['phys'] address value [count]",
6478         },
6479         {
6480                 .name = "mwb",
6481                 .handler = handle_mw_command,
6482                 .mode = COMMAND_EXEC,
6483                 .help = "write memory byte",
6484                 .usage = "['phys'] address value [count]",
6485         },
6486         {
6487                 .name = "bp",
6488                 .handler = handle_bp_command,
6489                 .mode = COMMAND_EXEC,
6490                 .help = "list or set hardware or software breakpoint",
6491                 .usage = "<address> [<asid>] <length> ['hw'|'hw_ctx']",
6492         },
6493         {
6494                 .name = "rbp",
6495                 .handler = handle_rbp_command,
6496                 .mode = COMMAND_EXEC,
6497                 .help = "remove breakpoint",
6498                 .usage = "address",
6499         },
6500         {
6501                 .name = "wp",
6502                 .handler = handle_wp_command,
6503                 .mode = COMMAND_EXEC,
6504                 .help = "list (no params) or create watchpoints",
6505                 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
6506         },
6507         {
6508                 .name = "rwp",
6509                 .handler = handle_rwp_command,
6510                 .mode = COMMAND_EXEC,
6511                 .help = "remove watchpoint",
6512                 .usage = "address",
6513         },
6514         {
6515                 .name = "load_image",
6516                 .handler = handle_load_image_command,
6517                 .mode = COMMAND_EXEC,
6518                 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
6519                         "[min_address] [max_length]",
6520         },
6521         {
6522                 .name = "dump_image",
6523                 .handler = handle_dump_image_command,
6524                 .mode = COMMAND_EXEC,
6525                 .usage = "filename address size",
6526         },
6527         {
6528                 .name = "verify_image_checksum",
6529                 .handler = handle_verify_image_checksum_command,
6530                 .mode = COMMAND_EXEC,
6531                 .usage = "filename [offset [type]]",
6532         },
6533         {
6534                 .name = "verify_image",
6535                 .handler = handle_verify_image_command,
6536                 .mode = COMMAND_EXEC,
6537                 .usage = "filename [offset [type]]",
6538         },
6539         {
6540                 .name = "test_image",
6541                 .handler = handle_test_image_command,
6542                 .mode = COMMAND_EXEC,
6543                 .usage = "filename [offset [type]]",
6544         },
6545         {
6546                 .name = "mem2array",
6547                 .mode = COMMAND_EXEC,
6548                 .jim_handler = jim_mem2array,
6549                 .help = "read 8/16/32 bit memory and return as a TCL array "
6550                         "for script processing",
6551                 .usage = "arrayname bitwidth address count",
6552         },
6553         {
6554                 .name = "array2mem",
6555                 .mode = COMMAND_EXEC,
6556                 .jim_handler = jim_array2mem,
6557                 .help = "convert a TCL array to memory locations "
6558                         "and write the 8/16/32 bit values",
6559                 .usage = "arrayname bitwidth address count",
6560         },
6561         {
6562                 .name = "reset_nag",
6563                 .handler = handle_target_reset_nag,
6564                 .mode = COMMAND_ANY,
6565                 .help = "Nag after each reset about options that could have been "
6566                                 "enabled to improve performance. ",
6567                 .usage = "['enable'|'disable']",
6568         },
6569         {
6570                 .name = "ps",
6571                 .handler = handle_ps_command,
6572                 .mode = COMMAND_EXEC,
6573                 .help = "list all tasks ",
6574                 .usage = " ",
6575         },
6576         {
6577                 .name = "test_mem_access",
6578                 .handler = handle_test_mem_access_command,
6579                 .mode = COMMAND_EXEC,
6580                 .help = "Test the target's memory access functions",
6581                 .usage = "size",
6582         },
6583
6584         COMMAND_REGISTRATION_DONE
6585 };
6586 static int target_register_user_commands(struct command_context *cmd_ctx)
6587 {
6588         int retval = ERROR_OK;
6589         retval = target_request_register_commands(cmd_ctx);
6590         if (retval != ERROR_OK)
6591                 return retval;
6592
6593         retval = trace_register_commands(cmd_ctx);
6594         if (retval != ERROR_OK)
6595                 return retval;
6596
6597
6598         return register_commands(cmd_ctx, NULL, target_exec_command_handlers);
6599 }