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