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