]> git.sur5r.net Git - openocd/blob - src/target/target.c
retired reset run_and_init/halt
[openocd] / src / target / target.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                      *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "replacements.h"
28 #include "target.h"
29 #include "target_request.h"
30
31 #include "log.h"
32 #include "configuration.h"
33 #include "binarybuffer.h"
34 #include "jtag.h"
35
36 #include <string.h>
37 #include <stdlib.h>
38 #include <inttypes.h>
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 #include <errno.h>
44
45 #include <sys/time.h>
46 #include <time.h>
47
48 #include <time_support.h>
49
50 #include <fileio.h>
51 #include <image.h>
52
53 int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv);
54
55 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
56 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
57
58 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
59
60 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
61 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
62 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
63 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
64 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
65 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
66 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
67 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
70 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
72 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
77 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc);
78 int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
79 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
80 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
81
82
83 /* targets */
84 extern target_type_t arm7tdmi_target;
85 extern target_type_t arm720t_target;
86 extern target_type_t arm9tdmi_target;
87 extern target_type_t arm920t_target;
88 extern target_type_t arm966e_target;
89 extern target_type_t arm926ejs_target;
90 extern target_type_t feroceon_target;
91 extern target_type_t xscale_target;
92 extern target_type_t cortexm3_target;
93 extern target_type_t arm11_target;
94 extern target_type_t mips_m4k_target;
95
96 target_type_t *target_types[] =
97 {
98         &arm7tdmi_target,
99         &arm9tdmi_target,
100         &arm920t_target,
101         &arm720t_target,
102         &arm966e_target,
103         &arm926ejs_target,
104         &feroceon_target,
105         &xscale_target,
106         &cortexm3_target,
107         &arm11_target,
108         &mips_m4k_target,
109         NULL,
110 };
111
112 target_t *targets = NULL;
113 target_event_callback_t *target_event_callbacks = NULL;
114 target_timer_callback_t *target_timer_callbacks = NULL;
115
116 char *target_state_strings[] =
117 {
118         "unknown",
119         "running",
120         "halted",
121         "reset",
122         "debug_running",
123 };
124
125 char *target_debug_reason_strings[] =
126 {
127         "debug request", "breakpoint", "watchpoint",
128         "watchpoint and breakpoint", "single step",
129         "target not halted", "undefined"
130 };
131
132 char *target_endianess_strings[] =
133 {
134         "big endian",
135         "little endian",
136 };
137
138 static int target_continous_poll = 1;
139
140 /* read a u32 from a buffer in target memory endianness */
141 u32 target_buffer_get_u32(target_t *target, u8 *buffer)
142 {
143         if (target->endianness == TARGET_LITTLE_ENDIAN)
144                 return le_to_h_u32(buffer);
145         else
146                 return be_to_h_u32(buffer);
147 }
148
149 /* read a u16 from a buffer in target memory endianness */
150 u16 target_buffer_get_u16(target_t *target, u8 *buffer)
151 {
152         if (target->endianness == TARGET_LITTLE_ENDIAN)
153                 return le_to_h_u16(buffer);
154         else
155                 return be_to_h_u16(buffer);
156 }
157
158 /* write a u32 to a buffer in target memory endianness */
159 void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
160 {
161         if (target->endianness == TARGET_LITTLE_ENDIAN)
162                 h_u32_to_le(buffer, value);
163         else
164                 h_u32_to_be(buffer, value);
165 }
166
167 /* write a u16 to a buffer in target memory endianness */
168 void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
169 {
170         if (target->endianness == TARGET_LITTLE_ENDIAN)
171                 h_u16_to_le(buffer, value);
172         else
173                 h_u16_to_be(buffer, value);
174 }
175
176 /* returns a pointer to the n-th configured target */
177 target_t* get_target_by_num(int num)
178 {
179         target_t *target = targets;
180         int i = 0;
181
182         while (target)
183         {
184                 if (num == i)
185                         return target;
186                 target = target->next;
187                 i++;
188         }
189
190         return NULL;
191 }
192
193 int get_num_by_target(target_t *query_target)
194 {
195         target_t *target = targets;
196         int i = 0;
197
198         while (target)
199         {
200                 if (target == query_target)
201                         return i;
202                 target = target->next;
203                 i++;
204         }
205
206         return -1;
207 }
208
209 target_t* get_current_target(command_context_t *cmd_ctx)
210 {
211         target_t *target = get_target_by_num(cmd_ctx->current_target);
212
213         if (target == NULL)
214         {
215                 LOG_ERROR("BUG: current_target out of bounds");
216                 exit(-1);
217         }
218
219         return target;
220 }
221
222
223 int target_poll(struct target_s *target)
224 {
225         /* We can't poll until after examine */
226         if (!target->type->examined)
227         {
228                 /* Fail silently lest we pollute the log */
229                 return ERROR_FAIL;
230         }
231         return target->type->poll(target);
232 }
233
234 int target_halt(struct target_s *target)
235 {
236         /* We can't poll until after examine */
237         if (!target->type->examined)
238         {
239                 LOG_ERROR("Target not examined yet");
240                 return ERROR_FAIL;
241         }
242         return target->type->halt(target);
243 }
244
245 int target_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
246 {
247         int retval;
248
249         /* We can't poll until after examine */
250         if (!target->type->examined)
251         {
252                 LOG_ERROR("Target not examined yet");
253                 return ERROR_FAIL;
254         }
255
256         /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
257          * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
258          * the application.
259          */
260         if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
261                 return retval;
262
263         return retval;
264 }
265
266 int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
267 {
268         int retval = ERROR_OK;
269         target_t *target;
270         struct timeval timeout, now;
271
272         target = targets;
273         while (target)
274         {
275                 target_invoke_script(cmd_ctx, target, "pre_reset");
276                 target = target->next;
277         }
278
279         if ((retval = jtag_init_reset(cmd_ctx)) != ERROR_OK)
280                 return retval;
281
282         keep_alive(); /* we might be running on a very slow JTAG clk */
283
284         /* First time this is executed after launching OpenOCD, it will read out
285          * the type of CPU, etc. and init Embedded ICE registers in host
286          * memory.
287          *
288          * It will also set up ICE registers in the target.
289          *
290          * However, if we assert TRST later, we need to set up the registers again.
291          *
292          * For the "reset halt/init" case we must only set up the registers here.
293          */
294         if ((retval = target_examine(cmd_ctx)) != ERROR_OK)
295                 return retval;
296
297         keep_alive(); /* we might be running on a very slow JTAG clk */
298
299         target = targets;
300         while (target)
301         {
302                 /* we have no idea what state the target is in, so we
303                  * have to drop working areas
304                  */
305                 target_free_all_working_areas_restore(target, 0);
306                 target->reset_halt=((reset_mode==RESET_HALT)||(reset_mode==RESET_INIT));
307                 target->type->assert_reset(target);
308                 target = target->next;
309         }
310         if ((retval = jtag_execute_queue()) != ERROR_OK)
311         {
312                 LOG_WARNING("JTAG communication failed asserting reset.");
313                 retval = ERROR_OK;
314         }
315
316         /* request target halt if necessary, and schedule further action */
317         target = targets;
318         while (target)
319         {
320                 if (reset_mode!=RESET_RUN)
321                 {
322                         if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
323                                 target_halt(target);
324                 }
325                 target = target->next;
326         }
327
328         if ((retval = jtag_execute_queue()) != ERROR_OK)
329         {
330                 LOG_WARNING("JTAG communication failed while reset was asserted. Consider using srst_only for reset_config.");
331                 retval = ERROR_OK;
332         }
333
334         target = targets;
335         while (target)
336         {
337                 target->type->deassert_reset(target);
338                 /* We can fail to bring the target into the halted state  */
339                 target_poll(target);
340                 if (target->reset_halt&&((target->state != TARGET_HALTED)))
341                 {
342                         LOG_WARNING("Failed to reset target into halted mode - issuing halt");
343                         target->type->halt(target);
344                 }
345
346                 target = target->next;
347         }
348
349         if ((retval = jtag_execute_queue()) != ERROR_OK)
350         {
351                 LOG_WARNING("JTAG communication failed while deasserting reset.");
352                 retval = ERROR_OK;
353         }
354
355         if (jtag_reset_config & RESET_SRST_PULLS_TRST)
356         {
357                 /* If TRST was asserted we need to set up registers again */
358                 if ((retval = target_examine(cmd_ctx)) != ERROR_OK)
359                         return retval;
360         }
361
362         LOG_DEBUG("Waiting for halted stated as appropriate");
363
364         if ((reset_mode == RESET_HALT) || (reset_mode == RESET_INIT))
365         {
366                 /* Wait for reset to complete, maximum 5 seconds. */
367                 if (((retval=target_wait_state(target, TARGET_HALTED, 5000)))==ERROR_OK)
368                 {
369                         if (reset_mode == RESET_INIT)
370                                 target_invoke_script(cmd_ctx, target, "post_reset");
371                 }
372         }
373
374         /* We want any events to be processed before the prompt */
375         target_call_timer_callbacks_now();
376
377         return retval;
378 }
379
380 static int default_virt2phys(struct target_s *target, u32 virtual, u32 *physical)
381 {
382         *physical = virtual;
383         return ERROR_OK;
384 }
385
386 static int default_mmu(struct target_s *target, int *enabled)
387 {
388         *enabled = 0;
389         return ERROR_OK;
390 }
391
392 static int default_examine(struct command_context_s *cmd_ctx, struct target_s *target)
393 {
394         target->type->examined = 1;
395         return ERROR_OK;
396 }
397
398
399 /* Targets that correctly implement init+examine, i.e.
400  * no communication with target during init:
401  *
402  * XScale
403  */
404 int target_examine(struct command_context_s *cmd_ctx)
405 {
406         int retval = ERROR_OK;
407         target_t *target = targets;
408         while (target)
409         {
410                 if ((retval = target->type->examine(cmd_ctx, target))!=ERROR_OK)
411                         return retval;
412                 target = target->next;
413         }
414         return retval;
415 }
416
417 static int target_write_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
418 {
419         if (!target->type->examined)
420         {
421                 LOG_ERROR("Target not examined yet");
422                 return ERROR_FAIL;
423         }
424         return target->type->write_memory_imp(target, address, size, count, buffer);
425 }
426
427 static int target_read_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
428 {
429         if (!target->type->examined)
430         {
431                 LOG_ERROR("Target not examined yet");
432                 return ERROR_FAIL;
433         }
434         return target->type->read_memory_imp(target, address, size, count, buffer);
435 }
436
437 static int target_soft_reset_halt_imp(struct target_s *target)
438 {
439         if (!target->type->examined)
440         {
441                 LOG_ERROR("Target not examined yet");
442                 return ERROR_FAIL;
443         }
444         return target->type->soft_reset_halt_imp(target);
445 }
446
447 static int target_run_algorithm_imp(struct target_s *target, int num_mem_params, mem_param_t *mem_params, int num_reg_params, reg_param_t *reg_param, u32 entry_point, u32 exit_point, int timeout_ms, void *arch_info)
448 {
449         if (!target->type->examined)
450         {
451                 LOG_ERROR("Target not examined yet");
452                 return ERROR_FAIL;
453         }
454         return target->type->run_algorithm_imp(target, num_mem_params, mem_params, num_reg_params, reg_param, entry_point, exit_point, timeout_ms, arch_info);
455 }
456
457 int target_init(struct command_context_s *cmd_ctx)
458 {
459         target_t *target = targets;
460
461         while (target)
462         {
463                 target->type->examined = 0;
464                 if (target->type->examine == NULL)
465                 {
466                         target->type->examine = default_examine;
467                 }
468
469                 if (target->type->init_target(cmd_ctx, target) != ERROR_OK)
470                 {
471                         LOG_ERROR("target '%s' init failed", target->type->name);
472                         exit(-1);
473                 }
474
475                 /* Set up default functions if none are provided by target */
476                 if (target->type->virt2phys == NULL)
477                 {
478                         target->type->virt2phys = default_virt2phys;
479                 }
480                 target->type->virt2phys = default_virt2phys;
481                 /* a non-invasive way(in terms of patches) to add some code that
482                  * runs before the type->write/read_memory implementation
483                  */
484                 target->type->write_memory_imp = target->type->write_memory;
485                 target->type->write_memory = target_write_memory_imp;
486                 target->type->read_memory_imp = target->type->read_memory;
487                 target->type->read_memory = target_read_memory_imp;
488                 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
489                 target->type->soft_reset_halt = target_soft_reset_halt_imp;
490                 target->type->run_algorithm_imp = target->type->run_algorithm;
491                 target->type->run_algorithm = target_run_algorithm_imp;
492
493
494                 if (target->type->mmu == NULL)
495                 {
496                         target->type->mmu = default_mmu;
497                 }
498                 target = target->next;
499         }
500
501         if (targets)
502         {
503                 target_register_user_commands(cmd_ctx);
504                 target_register_timer_callback(handle_target, 100, 1, NULL);
505         }
506
507         return ERROR_OK;
508 }
509
510 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
511 {
512         target_event_callback_t **callbacks_p = &target_event_callbacks;
513
514         if (callback == NULL)
515         {
516                 return ERROR_INVALID_ARGUMENTS;
517         }
518
519         if (*callbacks_p)
520         {
521                 while ((*callbacks_p)->next)
522                         callbacks_p = &((*callbacks_p)->next);
523                 callbacks_p = &((*callbacks_p)->next);
524         }
525
526         (*callbacks_p) = malloc(sizeof(target_event_callback_t));
527         (*callbacks_p)->callback = callback;
528         (*callbacks_p)->priv = priv;
529         (*callbacks_p)->next = NULL;
530
531         return ERROR_OK;
532 }
533
534 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
535 {
536         target_timer_callback_t **callbacks_p = &target_timer_callbacks;
537         struct timeval now;
538
539         if (callback == NULL)
540         {
541                 return ERROR_INVALID_ARGUMENTS;
542         }
543
544         if (*callbacks_p)
545         {
546                 while ((*callbacks_p)->next)
547                         callbacks_p = &((*callbacks_p)->next);
548                 callbacks_p = &((*callbacks_p)->next);
549         }
550
551         (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
552         (*callbacks_p)->callback = callback;
553         (*callbacks_p)->periodic = periodic;
554         (*callbacks_p)->time_ms = time_ms;
555
556         gettimeofday(&now, NULL);
557         (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
558         time_ms -= (time_ms % 1000);
559         (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
560         if ((*callbacks_p)->when.tv_usec > 1000000)
561         {
562                 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
563                 (*callbacks_p)->when.tv_sec += 1;
564         }
565
566         (*callbacks_p)->priv = priv;
567         (*callbacks_p)->next = NULL;
568
569         return ERROR_OK;
570 }
571
572 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
573 {
574         target_event_callback_t **p = &target_event_callbacks;
575         target_event_callback_t *c = target_event_callbacks;
576
577         if (callback == NULL)
578         {
579                 return ERROR_INVALID_ARGUMENTS;
580         }
581
582         while (c)
583         {
584                 target_event_callback_t *next = c->next;
585                 if ((c->callback == callback) && (c->priv == priv))
586                 {
587                         *p = next;
588                         free(c);
589                         return ERROR_OK;
590                 }
591                 else
592                         p = &(c->next);
593                 c = next;
594         }
595
596         return ERROR_OK;
597 }
598
599 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
600 {
601         target_timer_callback_t **p = &target_timer_callbacks;
602         target_timer_callback_t *c = target_timer_callbacks;
603
604         if (callback == NULL)
605         {
606                 return ERROR_INVALID_ARGUMENTS;
607         }
608
609         while (c)
610         {
611                 target_timer_callback_t *next = c->next;
612                 if ((c->callback == callback) && (c->priv == priv))
613                 {
614                         *p = next;
615                         free(c);
616                         return ERROR_OK;
617                 }
618                 else
619                         p = &(c->next);
620                 c = next;
621         }
622
623         return ERROR_OK;
624 }
625
626 int target_call_event_callbacks(target_t *target, enum target_event event)
627 {
628         target_event_callback_t *callback = target_event_callbacks;
629         target_event_callback_t *next_callback;
630
631         LOG_DEBUG("target event %i", event);
632
633         while (callback)
634         {
635                 next_callback = callback->next;
636                 callback->callback(target, event, callback->priv);
637                 callback = next_callback;
638         }
639
640         return ERROR_OK;
641 }
642
643 static int target_call_timer_callbacks_check_time(int checktime)
644 {
645         target_timer_callback_t *callback = target_timer_callbacks;
646         target_timer_callback_t *next_callback;
647         struct timeval now;
648
649         keep_alive();
650
651         gettimeofday(&now, NULL);
652
653         while (callback)
654         {
655                 next_callback = callback->next;
656
657                 if ((!checktime&&callback->periodic)||
658                                 (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
659                                                 || (now.tv_sec > callback->when.tv_sec)))
660                 {
661                         if(callback->callback != NULL)
662                         {
663                                 callback->callback(callback->priv);
664                                 if (callback->periodic)
665                                 {
666                                         int time_ms = callback->time_ms;
667                                         callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
668                                         time_ms -= (time_ms % 1000);
669                                         callback->when.tv_sec = now.tv_sec + time_ms / 1000;
670                                         if (callback->when.tv_usec > 1000000)
671                                         {
672                                                 callback->when.tv_usec = callback->when.tv_usec - 1000000;
673                                                 callback->when.tv_sec += 1;
674                                         }
675                                 }
676                                 else
677                                         target_unregister_timer_callback(callback->callback, callback->priv);
678                         }
679                 }
680
681                 callback = next_callback;
682         }
683
684         return ERROR_OK;
685 }
686
687 int target_call_timer_callbacks()
688 {
689         return target_call_timer_callbacks_check_time(1);
690 }
691
692 /* invoke periodic callbacks immediately */
693 int target_call_timer_callbacks_now()
694 {
695         return target_call_timer_callbacks(0);
696 }
697
698 int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
699 {
700         working_area_t *c = target->working_areas;
701         working_area_t *new_wa = NULL;
702
703         /* Reevaluate working area address based on MMU state*/
704         if (target->working_areas == NULL)
705         {
706                 int retval;
707                 int enabled;
708                 retval = target->type->mmu(target, &enabled);
709                 if (retval != ERROR_OK)
710                 {
711                         return retval;
712                 }
713                 if (enabled)
714                 {
715                         target->working_area = target->working_area_virt;
716                 }
717                 else
718                 {
719                         target->working_area = target->working_area_phys;
720                 }
721         }
722
723         /* only allocate multiples of 4 byte */
724         if (size % 4)
725         {
726                 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
727                 size = CEIL(size, 4);
728         }
729
730         /* see if there's already a matching working area */
731         while (c)
732         {
733                 if ((c->free) && (c->size == size))
734                 {
735                         new_wa = c;
736                         break;
737                 }
738                 c = c->next;
739         }
740
741         /* if not, allocate a new one */
742         if (!new_wa)
743         {
744                 working_area_t **p = &target->working_areas;
745                 u32 first_free = target->working_area;
746                 u32 free_size = target->working_area_size;
747
748                 LOG_DEBUG("allocating new working area");
749
750                 c = target->working_areas;
751                 while (c)
752                 {
753                         first_free += c->size;
754                         free_size -= c->size;
755                         p = &c->next;
756                         c = c->next;
757                 }
758
759                 if (free_size < size)
760                 {
761                         LOG_WARNING("not enough working area available(requested %d, free %d)", size, free_size);
762                         return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
763                 }
764
765                 new_wa = malloc(sizeof(working_area_t));
766                 new_wa->next = NULL;
767                 new_wa->size = size;
768                 new_wa->address = first_free;
769
770                 if (target->backup_working_area)
771                 {
772                         new_wa->backup = malloc(new_wa->size);
773                         target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup);
774                 }
775                 else
776                 {
777                         new_wa->backup = NULL;
778                 }
779
780                 /* put new entry in list */
781                 *p = new_wa;
782         }
783
784         /* mark as used, and return the new (reused) area */
785         new_wa->free = 0;
786         *area = new_wa;
787
788         /* user pointer */
789         new_wa->user = area;
790
791         return ERROR_OK;
792 }
793
794 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
795 {
796         if (area->free)
797                 return ERROR_OK;
798
799         if (restore&&target->backup_working_area)
800                 target->type->write_memory(target, area->address, 4, area->size / 4, area->backup);
801
802         area->free = 1;
803
804         /* mark user pointer invalid */
805         *area->user = NULL;
806         area->user = NULL;
807
808         return ERROR_OK;
809 }
810
811 int target_free_working_area(struct target_s *target, working_area_t *area)
812 {
813         return target_free_working_area_restore(target, area, 1);
814 }
815
816 int target_free_all_working_areas_restore(struct target_s *target, int restore)
817 {
818         working_area_t *c = target->working_areas;
819
820         while (c)
821         {
822                 working_area_t *next = c->next;
823                 target_free_working_area_restore(target, c, restore);
824
825                 if (c->backup)
826                         free(c->backup);
827
828                 free(c);
829
830                 c = next;
831         }
832
833         target->working_areas = NULL;
834
835         return ERROR_OK;
836 }
837
838 int target_free_all_working_areas(struct target_s *target)
839 {
840         return target_free_all_working_areas_restore(target, 1);
841 }
842
843 int target_register_commands(struct command_context_s *cmd_ctx)
844 {
845         register_command(cmd_ctx, NULL, "target", handle_target_command, COMMAND_CONFIG, "target <cpu> [reset_init default - DEPRECATED] <chainpos> <endianness> <variant> [cpu type specifc args]");
846         register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, NULL);
847         register_command(cmd_ctx, NULL, "working_area", handle_working_area_command, COMMAND_ANY, "working_area <target#> <address> <size> <'backup'|'nobackup'> [virtual address]");
848         register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "virt2phys <virtual address>");
849         register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "PRELIMINARY! - profile <seconds> <gmon.out>");
850
851
852         /* script procedures */
853         register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array, "read memory and return as a TCL array for script processing");
854         register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem, "convert a TCL array to memory locations and write the values");
855         return ERROR_OK;
856 }
857
858 int target_arch_state(struct target_s *target)
859 {
860         int retval;
861         if (target==NULL)
862         {
863                 LOG_USER("No target has been configured");
864                 return ERROR_OK;
865         }
866
867         LOG_USER("target state: %s", target_state_strings[target->state]);
868
869         if (target->state!=TARGET_HALTED)
870                 return ERROR_OK;
871
872         retval=target->type->arch_state(target);
873         return retval;
874 }
875
876 /* Single aligned words are guaranteed to use 16 or 32 bit access
877  * mode respectively, otherwise data is handled as quickly as
878  * possible
879  */
880 int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
881 {
882         int retval;
883         LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
884
885         if (!target->type->examined)
886         {
887                 LOG_ERROR("Target not examined yet");
888                 return ERROR_FAIL;
889         }
890
891         if (address+size<address)
892         {
893                 /* GDB can request this when e.g. PC is 0xfffffffc*/
894                 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
895                 return ERROR_FAIL;
896         }
897
898         if (((address % 2) == 0) && (size == 2))
899         {
900                 return target->type->write_memory(target, address, 2, 1, buffer);
901         }
902
903         /* handle unaligned head bytes */
904         if (address % 4)
905         {
906                 int unaligned = 4 - (address % 4);
907
908                 if (unaligned > size)
909                         unaligned = size;
910
911                 if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
912                         return retval;
913
914                 buffer += unaligned;
915                 address += unaligned;
916                 size -= unaligned;
917         }
918
919         /* handle aligned words */
920         if (size >= 4)
921         {
922                 int aligned = size - (size % 4);
923
924                 /* use bulk writes above a certain limit. This may have to be changed */
925                 if (aligned > 128)
926                 {
927                         if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
928                                 return retval;
929                 }
930                 else
931                 {
932                         if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
933                                 return retval;
934                 }
935
936                 buffer += aligned;
937                 address += aligned;
938                 size -= aligned;
939         }
940
941         /* handle tail writes of less than 4 bytes */
942         if (size > 0)
943         {
944                 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
945                         return retval;
946         }
947
948         return ERROR_OK;
949 }
950
951
952 /* Single aligned words are guaranteed to use 16 or 32 bit access
953  * mode respectively, otherwise data is handled as quickly as
954  * possible
955  */
956 int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
957 {
958         int retval;
959         LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
960
961         if (!target->type->examined)
962         {
963                 LOG_ERROR("Target not examined yet");
964                 return ERROR_FAIL;
965         }
966
967         if (address+size<address)
968         {
969                 /* GDB can request this when e.g. PC is 0xfffffffc*/
970                 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
971                 return ERROR_FAIL;
972         }
973
974         if (((address % 2) == 0) && (size == 2))
975         {
976                 return target->type->read_memory(target, address, 2, 1, buffer);
977         }
978
979         /* handle unaligned head bytes */
980         if (address % 4)
981         {
982                 int unaligned = 4 - (address % 4);
983
984                 if (unaligned > size)
985                         unaligned = size;
986
987                 if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
988                         return retval;
989
990                 buffer += unaligned;
991                 address += unaligned;
992                 size -= unaligned;
993         }
994
995         /* handle aligned words */
996         if (size >= 4)
997         {
998                 int aligned = size - (size % 4);
999
1000                 if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1001                         return retval;
1002
1003                 buffer += aligned;
1004                 address += aligned;
1005                 size -= aligned;
1006         }
1007
1008         /* handle tail writes of less than 4 bytes */
1009         if (size > 0)
1010         {
1011                 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1012                         return retval;
1013         }
1014
1015         return ERROR_OK;
1016 }
1017
1018 int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* crc)
1019 {
1020         u8 *buffer;
1021         int retval;
1022         int i;
1023         u32 checksum = 0;
1024         if (!target->type->examined)
1025         {
1026                 LOG_ERROR("Target not examined yet");
1027                 return ERROR_FAIL;
1028         }
1029
1030         if ((retval = target->type->checksum_memory(target, address,
1031                 size, &checksum)) == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1032         {
1033                 buffer = malloc(size);
1034                 if (buffer == NULL)
1035                 {
1036                         LOG_ERROR("error allocating buffer for section (%d bytes)", size);
1037                         return ERROR_INVALID_ARGUMENTS;
1038                 }
1039                 retval = target_read_buffer(target, address, size, buffer);
1040                 if (retval != ERROR_OK)
1041                 {
1042                         free(buffer);
1043                         return retval;
1044                 }
1045
1046                 /* convert to target endianess */
1047                 for (i = 0; i < (size/sizeof(u32)); i++)
1048                 {
1049                         u32 target_data;
1050                         target_data = target_buffer_get_u32(target, &buffer[i*sizeof(u32)]);
1051                         target_buffer_set_u32(target, &buffer[i*sizeof(u32)], target_data);
1052                 }
1053
1054                 retval = image_calculate_checksum( buffer, size, &checksum );
1055                 free(buffer);
1056         }
1057
1058         *crc = checksum;
1059
1060         return retval;
1061 }
1062
1063 int target_blank_check_memory(struct target_s *target, u32 address, u32 size, u32* blank)
1064 {
1065         int retval;
1066         if (!target->type->examined)
1067         {
1068                 LOG_ERROR("Target not examined yet");
1069                 return ERROR_FAIL;
1070         }
1071
1072         if (target->type->blank_check_memory == 0)
1073                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1074
1075         retval = target->type->blank_check_memory(target, address, size, blank);
1076
1077         return retval;
1078 }
1079
1080 int target_read_u32(struct target_s *target, u32 address, u32 *value)
1081 {
1082         u8 value_buf[4];
1083         if (!target->type->examined)
1084         {
1085                 LOG_ERROR("Target not examined yet");
1086                 return ERROR_FAIL;
1087         }
1088
1089         int retval = target->type->read_memory(target, address, 4, 1, value_buf);
1090
1091         if (retval == ERROR_OK)
1092         {
1093                 *value = target_buffer_get_u32(target, value_buf);
1094                 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
1095         }
1096         else
1097         {
1098                 *value = 0x0;
1099                 LOG_DEBUG("address: 0x%8.8x failed", address);
1100         }
1101
1102         return retval;
1103 }
1104
1105 int target_read_u16(struct target_s *target, u32 address, u16 *value)
1106 {
1107         u8 value_buf[2];
1108         if (!target->type->examined)
1109         {
1110                 LOG_ERROR("Target not examined yet");
1111                 return ERROR_FAIL;
1112         }
1113
1114         int retval = target->type->read_memory(target, address, 2, 1, value_buf);
1115
1116         if (retval == ERROR_OK)
1117         {
1118                 *value = target_buffer_get_u16(target, value_buf);
1119                 LOG_DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
1120         }
1121         else
1122         {
1123                 *value = 0x0;
1124                 LOG_DEBUG("address: 0x%8.8x failed", address);
1125         }
1126
1127         return retval;
1128 }
1129
1130 int target_read_u8(struct target_s *target, u32 address, u8 *value)
1131 {
1132         int retval = target->type->read_memory(target, address, 1, 1, value);
1133         if (!target->type->examined)
1134         {
1135                 LOG_ERROR("Target not examined yet");
1136                 return ERROR_FAIL;
1137         }
1138
1139         if (retval == ERROR_OK)
1140         {
1141                 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
1142         }
1143         else
1144         {
1145                 *value = 0x0;
1146                 LOG_DEBUG("address: 0x%8.8x failed", address);
1147         }
1148
1149         return retval;
1150 }
1151
1152 int target_write_u32(struct target_s *target, u32 address, u32 value)
1153 {
1154         int retval;
1155         u8 value_buf[4];
1156         if (!target->type->examined)
1157         {
1158                 LOG_ERROR("Target not examined yet");
1159                 return ERROR_FAIL;
1160         }
1161
1162         LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1163
1164         target_buffer_set_u32(target, value_buf, value);
1165         if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1166         {
1167                 LOG_DEBUG("failed: %i", retval);
1168         }
1169
1170         return retval;
1171 }
1172
1173 int target_write_u16(struct target_s *target, u32 address, u16 value)
1174 {
1175         int retval;
1176         u8 value_buf[2];
1177         if (!target->type->examined)
1178         {
1179                 LOG_ERROR("Target not examined yet");
1180                 return ERROR_FAIL;
1181         }
1182
1183         LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1184
1185         target_buffer_set_u16(target, value_buf, value);
1186         if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1187         {
1188                 LOG_DEBUG("failed: %i", retval);
1189         }
1190
1191         return retval;
1192 }
1193
1194 int target_write_u8(struct target_s *target, u32 address, u8 value)
1195 {
1196         int retval;
1197         if (!target->type->examined)
1198         {
1199                 LOG_ERROR("Target not examined yet");
1200                 return ERROR_FAIL;
1201         }
1202
1203         LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
1204
1205         if ((retval = target->type->read_memory(target, address, 1, 1, &value)) != ERROR_OK)
1206         {
1207                 LOG_DEBUG("failed: %i", retval);
1208         }
1209
1210         return retval;
1211 }
1212
1213 int target_register_user_commands(struct command_context_s *cmd_ctx)
1214 {
1215         register_command(cmd_ctx,  NULL, "reg", handle_reg_command, COMMAND_EXEC, NULL);
1216         register_command(cmd_ctx,  NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1217         register_command(cmd_ctx,  NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1218         register_command(cmd_ctx,  NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1219         register_command(cmd_ctx,  NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1220         register_command(cmd_ctx,  NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1221         register_command(cmd_ctx,  NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init]");
1222         register_command(cmd_ctx,  NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1223
1224         register_command(cmd_ctx,  NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
1225         register_command(cmd_ctx,  NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
1226         register_command(cmd_ctx,  NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
1227
1228         register_command(cmd_ctx,  NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value> [count]");
1229         register_command(cmd_ctx,  NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value> [count]");
1230         register_command(cmd_ctx,  NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value> [count]");
1231
1232         register_command(cmd_ctx,  NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
1233         register_command(cmd_ctx,  NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
1234         register_command(cmd_ctx,  NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
1235         register_command(cmd_ctx,  NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
1236
1237         register_command(cmd_ctx,  NULL, "load_image", handle_load_image_command, COMMAND_EXEC, "load_image <file> <address> ['bin'|'ihex'|'elf'|'s19'] [min_address] [max_length]");
1238         register_command(cmd_ctx,  NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1239         register_command(cmd_ctx,  NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1240
1241         target_request_register_commands(cmd_ctx);
1242         trace_register_commands(cmd_ctx);
1243
1244         return ERROR_OK;
1245 }
1246
1247 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1248 {
1249         target_t *target = targets;
1250         int count = 0;
1251
1252         if (argc == 1)
1253         {
1254                 int num = strtoul(args[0], NULL, 0);
1255
1256                 while (target)
1257                 {
1258                         count++;
1259                         target = target->next;
1260                 }
1261
1262                 if (num < count)
1263                         cmd_ctx->current_target = num;
1264                 else
1265                         command_print(cmd_ctx, "%i is out of bounds, only %i targets are configured", num, count);
1266
1267                 return ERROR_OK;
1268         }
1269
1270         while (target)
1271         {
1272                 command_print(cmd_ctx, "%i: %s (%s), state: %s", count++, target->type->name, target_endianess_strings[target->endianness], target_state_strings[target->state]);
1273                 target = target->next;
1274         }
1275
1276         return ERROR_OK;
1277 }
1278
1279 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1280 {
1281         int i;
1282         int found = 0;
1283
1284         if (argc < 3)
1285         {
1286                 return ERROR_COMMAND_SYNTAX_ERROR;
1287         }
1288
1289         /* search for the specified target */
1290         if (args[0] && (args[0][0] != 0))
1291         {
1292                 for (i = 0; target_types[i]; i++)
1293                 {
1294                         if (strcmp(args[0], target_types[i]->name) == 0)
1295                         {
1296                                 target_t **last_target_p = &targets;
1297
1298                                 /* register target specific commands */
1299                                 if (target_types[i]->register_commands(cmd_ctx) != ERROR_OK)
1300                                 {
1301                                         LOG_ERROR("couldn't register '%s' commands", args[0]);
1302                                         exit(-1);
1303                                 }
1304
1305                                 if (*last_target_p)
1306                                 {
1307                                         while ((*last_target_p)->next)
1308                                                 last_target_p = &((*last_target_p)->next);
1309                                         last_target_p = &((*last_target_p)->next);
1310                                 }
1311
1312                                 *last_target_p = malloc(sizeof(target_t));
1313
1314                                 /* allocate memory for each unique target type */
1315                                 (*last_target_p)->type = (target_type_t*)malloc(sizeof(target_type_t));
1316                                 *((*last_target_p)->type) = *target_types[i];
1317
1318                                 if (strcmp(args[1], "big") == 0)
1319                                         (*last_target_p)->endianness = TARGET_BIG_ENDIAN;
1320                                 else if (strcmp(args[1], "little") == 0)
1321                                         (*last_target_p)->endianness = TARGET_LITTLE_ENDIAN;
1322                                 else
1323                                 {
1324                                         LOG_ERROR("endianness must be either 'little' or 'big', not '%s'", args[1]);
1325                                         return ERROR_COMMAND_SYNTAX_ERROR;
1326                                 }
1327
1328                                 if (strcmp(args[2], "reset_halt") == 0)
1329                                 {
1330                                         LOG_WARNING("reset_mode argument is obsolete.");
1331                                         return ERROR_COMMAND_SYNTAX_ERROR;
1332                                 }
1333                                 else if (strcmp(args[2], "reset_run") == 0)
1334                                 {
1335                                         LOG_WARNING("reset_mode argument is obsolete.");
1336                                         return ERROR_COMMAND_SYNTAX_ERROR;
1337                                 }
1338                                 else if (strcmp(args[2], "reset_init") == 0)
1339                                 {
1340                                         LOG_WARNING("reset_mode argument is obsolete.");
1341                                         return ERROR_COMMAND_SYNTAX_ERROR;
1342                                 }
1343                                 else if (strcmp(args[2], "run_and_halt") == 0)
1344                                 {
1345                                         LOG_WARNING("reset_mode argument is obsolete.");
1346                                         return ERROR_COMMAND_SYNTAX_ERROR;
1347                                 }
1348                                 else if (strcmp(args[2], "run_and_init") == 0)
1349                                 {
1350                                         LOG_WARNING("reset_mode argument is obsolete.");
1351                                         return ERROR_COMMAND_SYNTAX_ERROR;
1352                                 }
1353                                 else
1354                                 {
1355                                         /* Kludge! we want to make this reset arg optional while remaining compatible! */
1356                                         args--;
1357                                         argc++;
1358                                 }
1359
1360                                 (*last_target_p)->working_area = 0x0;
1361                                 (*last_target_p)->working_area_size = 0x0;
1362                                 (*last_target_p)->working_areas = NULL;
1363                                 (*last_target_p)->backup_working_area = 0;
1364
1365                                 (*last_target_p)->state = TARGET_UNKNOWN;
1366                                 (*last_target_p)->debug_reason = DBG_REASON_UNDEFINED;
1367                                 (*last_target_p)->reg_cache = NULL;
1368                                 (*last_target_p)->breakpoints = NULL;
1369                                 (*last_target_p)->watchpoints = NULL;
1370                                 (*last_target_p)->next = NULL;
1371                                 (*last_target_p)->arch_info = NULL;
1372
1373                                 /* initialize trace information */
1374                                 (*last_target_p)->trace_info = malloc(sizeof(trace_t));
1375                                 (*last_target_p)->trace_info->num_trace_points = 0;
1376                                 (*last_target_p)->trace_info->trace_points_size = 0;
1377                                 (*last_target_p)->trace_info->trace_points = NULL;
1378                                 (*last_target_p)->trace_info->trace_history_size = 0;
1379                                 (*last_target_p)->trace_info->trace_history = NULL;
1380                                 (*last_target_p)->trace_info->trace_history_pos = 0;
1381                                 (*last_target_p)->trace_info->trace_history_overflowed = 0;
1382
1383                                 (*last_target_p)->dbgmsg = NULL;
1384                                 (*last_target_p)->dbg_msg_enabled = 0;
1385
1386                                 (*last_target_p)->type->target_command(cmd_ctx, cmd, args, argc, *last_target_p);
1387
1388                                 found = 1;
1389                                 break;
1390                         }
1391                 }
1392         }
1393
1394         /* no matching target found */
1395         if (!found)
1396         {
1397                 LOG_ERROR("target '%s' not found", args[0]);
1398                 return ERROR_COMMAND_SYNTAX_ERROR;
1399         }
1400
1401         return ERROR_OK;
1402 }
1403
1404 int target_invoke_script(struct command_context_s *cmd_ctx, target_t *target, char *name)
1405 {
1406         return command_run_linef(cmd_ctx, " if {[catch {info body target_%d_%s} t]==0} {target_%d_%s}",
1407                         get_num_by_target(target), name,
1408                         get_num_by_target(target), name);
1409 }
1410
1411 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1412 {
1413         target_t *target = NULL;
1414
1415         if ((argc < 4) || (argc > 5))
1416         {
1417                 return ERROR_COMMAND_SYNTAX_ERROR;
1418         }
1419
1420         target = get_target_by_num(strtoul(args[0], NULL, 0));
1421         if (!target)
1422         {
1423                 return ERROR_COMMAND_SYNTAX_ERROR;
1424         }
1425         target_free_all_working_areas(target);
1426
1427         target->working_area_phys = target->working_area_virt = strtoul(args[1], NULL, 0);
1428         if (argc == 5)
1429         {
1430                 target->working_area_virt = strtoul(args[4], NULL, 0);
1431         }
1432         target->working_area_size = strtoul(args[2], NULL, 0);
1433
1434         if (strcmp(args[3], "backup") == 0)
1435         {
1436                 target->backup_working_area = 1;
1437         }
1438         else if (strcmp(args[3], "nobackup") == 0)
1439         {
1440                 target->backup_working_area = 0;
1441         }
1442         else
1443         {
1444                 LOG_ERROR("unrecognized <backup|nobackup> argument (%s)", args[3]);
1445                 return ERROR_COMMAND_SYNTAX_ERROR;
1446         }
1447
1448         return ERROR_OK;
1449 }
1450
1451
1452 /* process target state changes */
1453 int handle_target(void *priv)
1454 {
1455         target_t *target = targets;
1456
1457         while (target)
1458         {
1459                 if (target_continous_poll)
1460                 {
1461                         /* polling may fail silently until the target has been examined */
1462                         target_poll(target);
1463                 }
1464
1465                 target = target->next;
1466         }
1467
1468         return ERROR_OK;
1469 }
1470
1471 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1472 {
1473         target_t *target;
1474         reg_t *reg = NULL;
1475         int count = 0;
1476         char *value;
1477
1478         LOG_DEBUG("-");
1479
1480         target = get_current_target(cmd_ctx);
1481
1482         /* list all available registers for the current target */
1483         if (argc == 0)
1484         {
1485                 reg_cache_t *cache = target->reg_cache;
1486
1487                 count = 0;
1488                 while(cache)
1489                 {
1490                         int i;
1491                         for (i = 0; i < cache->num_regs; i++)
1492                         {
1493                                 value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
1494                                 command_print(cmd_ctx, "(%i) %s (/%i): 0x%s (dirty: %i, valid: %i)", count++, cache->reg_list[i].name, cache->reg_list[i].size, value, cache->reg_list[i].dirty, cache->reg_list[i].valid);
1495                                 free(value);
1496                         }
1497                         cache = cache->next;
1498                 }
1499
1500                 return ERROR_OK;
1501         }
1502
1503         /* access a single register by its ordinal number */
1504         if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1505         {
1506                 int num = strtoul(args[0], NULL, 0);
1507                 reg_cache_t *cache = target->reg_cache;
1508
1509                 count = 0;
1510                 while(cache)
1511                 {
1512                         int i;
1513                         for (i = 0; i < cache->num_regs; i++)
1514                         {
1515                                 if (count++ == num)
1516                                 {
1517                                         reg = &cache->reg_list[i];
1518                                         break;
1519                                 }
1520                         }
1521                         if (reg)
1522                                 break;
1523                         cache = cache->next;
1524                 }
1525
1526                 if (!reg)
1527                 {
1528                         command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1529                         return ERROR_OK;
1530                 }
1531         } else /* access a single register by its name */
1532         {
1533                 reg = register_get_by_name(target->reg_cache, args[0], 1);
1534
1535                 if (!reg)
1536                 {
1537                         command_print(cmd_ctx, "register %s not found in current target", args[0]);
1538                         return ERROR_OK;
1539                 }
1540         }
1541
1542         /* display a register */
1543         if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1544         {
1545                 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1546                         reg->valid = 0;
1547
1548                 if (reg->valid == 0)
1549                 {
1550                         reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1551                         if (arch_type == NULL)
1552                         {
1553                                 LOG_ERROR("BUG: encountered unregistered arch type");
1554                                 return ERROR_OK;
1555                         }
1556                         arch_type->get(reg);
1557                 }
1558                 value = buf_to_str(reg->value, reg->size, 16);
1559                 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1560                 free(value);
1561                 return ERROR_OK;
1562         }
1563
1564         /* set register value */
1565         if (argc == 2)
1566         {
1567                 u8 *buf = malloc(CEIL(reg->size, 8));
1568                 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1569
1570                 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1571                 if (arch_type == NULL)
1572                 {
1573                         LOG_ERROR("BUG: encountered unregistered arch type");
1574                         return ERROR_OK;
1575                 }
1576
1577                 arch_type->set(reg, buf);
1578
1579                 value = buf_to_str(reg->value, reg->size, 16);
1580                 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1581                 free(value);
1582
1583                 free(buf);
1584
1585                 return ERROR_OK;
1586         }
1587
1588         command_print(cmd_ctx, "usage: reg <#|name> [value]");
1589
1590         return ERROR_OK;
1591 }
1592
1593
1594 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1595 {
1596         target_t *target = get_current_target(cmd_ctx);
1597
1598         if (argc == 0)
1599         {
1600                 target_poll(target);
1601                 target_arch_state(target);
1602         }
1603         else
1604         {
1605                 if (strcmp(args[0], "on") == 0)
1606                 {
1607                         target_continous_poll = 1;
1608                 }
1609                 else if (strcmp(args[0], "off") == 0)
1610                 {
1611                         target_continous_poll = 0;
1612                 }
1613                 else
1614                 {
1615                         command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1616                 }
1617         }
1618
1619
1620         return ERROR_OK;
1621 }
1622
1623 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1624 {
1625         int ms = 5000;
1626
1627         if (argc > 0)
1628         {
1629                 char *end;
1630
1631                 ms = strtoul(args[0], &end, 0) * 1000;
1632                 if (*end)
1633                 {
1634                         command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1635                         return ERROR_OK;
1636                 }
1637         }
1638         target_t *target = get_current_target(cmd_ctx);
1639
1640         return target_wait_state(target, TARGET_HALTED, ms);
1641 }
1642
1643 int target_wait_state(target_t *target, enum target_state state, int ms)
1644 {
1645         int retval;
1646         struct timeval timeout, now;
1647         int once=1;
1648         gettimeofday(&timeout, NULL);
1649         timeval_add_time(&timeout, 0, ms * 1000);
1650
1651         for (;;)
1652         {
1653                 if ((retval=target_poll(target))!=ERROR_OK)
1654                         return retval;
1655                 target_call_timer_callbacks_now();
1656                 if (target->state == state)
1657                 {
1658                         break;
1659                 }
1660                 if (once)
1661                 {
1662                         once=0;
1663                         LOG_USER("waiting for target %s...", target_state_strings[state]);
1664                 }
1665
1666                 gettimeofday(&now, NULL);
1667                 if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
1668                 {
1669                         LOG_ERROR("timed out while waiting for target %s", target_state_strings[state]);
1670                         return ERROR_FAIL;
1671                 }
1672         }
1673
1674         return ERROR_OK;
1675 }
1676
1677 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1678 {
1679         int retval;
1680         target_t *target = get_current_target(cmd_ctx);
1681
1682         LOG_DEBUG("-");
1683
1684         if ((retval = target_halt(target)) != ERROR_OK)
1685         {
1686                 return retval;
1687         }
1688
1689         return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1690 }
1691
1692 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1693 {
1694         target_t *target = get_current_target(cmd_ctx);
1695
1696         LOG_USER("requesting target halt and executing a soft reset");
1697
1698         target->type->soft_reset_halt(target);
1699
1700         return ERROR_OK;
1701 }
1702
1703 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1704 {
1705         target_t *target = get_current_target(cmd_ctx);
1706         enum target_reset_mode reset_mode = RESET_RUN;
1707
1708         if (argc >= 1)
1709         {
1710                 if (strcmp("run", args[0]) == 0)
1711                         reset_mode = RESET_RUN;
1712                 else if (strcmp("halt", args[0]) == 0)
1713                         reset_mode = RESET_HALT;
1714                 else if (strcmp("init", args[0]) == 0)
1715                         reset_mode = RESET_INIT;
1716                 else
1717                 {
1718                         return ERROR_COMMAND_SYNTAX_ERROR;
1719                 }
1720         }
1721
1722         /* reset *all* targets */
1723         target_process_reset(cmd_ctx, reset_mode);
1724
1725         return ERROR_OK;
1726 }
1727
1728 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1729 {
1730         int retval;
1731         target_t *target = get_current_target(cmd_ctx);
1732
1733         target_invoke_script(cmd_ctx, target, "pre_resume");
1734
1735         if (argc == 0)
1736                 retval = target_resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1737         else if (argc == 1)
1738                 retval = target_resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1739         else
1740         {
1741                 return ERROR_COMMAND_SYNTAX_ERROR;
1742         }
1743
1744         return retval;
1745 }
1746
1747 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1748 {
1749         target_t *target = get_current_target(cmd_ctx);
1750
1751         LOG_DEBUG("-");
1752
1753         if (argc == 0)
1754                 target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1755
1756         if (argc == 1)
1757                 target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1758
1759         return ERROR_OK;
1760 }
1761
1762 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1763 {
1764         const int line_bytecnt = 32;
1765         int count = 1;
1766         int size = 4;
1767         u32 address = 0;
1768         int line_modulo;
1769         int i;
1770
1771         char output[128];
1772         int output_len;
1773
1774         int retval;
1775
1776         u8 *buffer;
1777         target_t *target = get_current_target(cmd_ctx);
1778
1779         if (argc < 1)
1780                 return ERROR_OK;
1781
1782         if (argc == 2)
1783                 count = strtoul(args[1], NULL, 0);
1784
1785         address = strtoul(args[0], NULL, 0);
1786
1787
1788         switch (cmd[2])
1789         {
1790                 case 'w':
1791                         size = 4; line_modulo = line_bytecnt / 4;
1792                         break;
1793                 case 'h':
1794                         size = 2; line_modulo = line_bytecnt / 2;
1795                         break;
1796                 case 'b':
1797                         size = 1; line_modulo = line_bytecnt / 1;
1798                         break;
1799                 default:
1800                         return ERROR_OK;
1801         }
1802
1803         buffer = calloc(count, size);
1804         retval  = target->type->read_memory(target, address, size, count, buffer);
1805         if (retval == ERROR_OK)
1806         {
1807                 output_len = 0;
1808
1809                 for (i = 0; i < count; i++)
1810                 {
1811                         if (i%line_modulo == 0)
1812                                 output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1813
1814                         switch (size)
1815                         {
1816                                 case 4:
1817                                         output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
1818                                         break;
1819                                 case 2:
1820                                         output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
1821                                         break;
1822                                 case 1:
1823                                         output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
1824                                         break;
1825                         }
1826
1827                         if ((i%line_modulo == line_modulo-1) || (i == count - 1))
1828                         {
1829                                 command_print(cmd_ctx, output);
1830                                 output_len = 0;
1831                         }
1832                 }
1833         }
1834
1835         free(buffer);
1836
1837         return retval;
1838 }
1839
1840 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1841 {
1842         u32 address = 0;
1843         u32 value = 0;
1844         int count = 1;
1845         int i;
1846         int wordsize;
1847         target_t *target = get_current_target(cmd_ctx);
1848         u8 value_buf[4];
1849
1850          if ((argc < 2) || (argc > 3))
1851                 return ERROR_COMMAND_SYNTAX_ERROR;
1852
1853         address = strtoul(args[0], NULL, 0);
1854         value = strtoul(args[1], NULL, 0);
1855         if (argc == 3)
1856                 count = strtoul(args[2], NULL, 0);
1857
1858         switch (cmd[2])
1859         {
1860                 case 'w':
1861                         wordsize = 4;
1862                         target_buffer_set_u32(target, value_buf, value);
1863                         break;
1864                 case 'h':
1865                         wordsize = 2;
1866                         target_buffer_set_u16(target, value_buf, value);
1867                         break;
1868                 case 'b':
1869                         wordsize = 1;
1870                         value_buf[0] = value;
1871                         break;
1872                 default:
1873                         return ERROR_COMMAND_SYNTAX_ERROR;
1874         }
1875         for (i=0; i<count; i++)
1876         {
1877                 int retval;
1878                 switch (wordsize)
1879                 {
1880                         case 4:
1881                                 retval = target->type->write_memory(target, address + i*wordsize, 4, 1, value_buf);
1882                                 break;
1883                         case 2:
1884                                 retval = target->type->write_memory(target, address + i*wordsize, 2, 1, value_buf);
1885                                 break;
1886                         case 1:
1887                                 retval = target->type->write_memory(target, address + i*wordsize, 1, 1, value_buf);
1888                         break;
1889                         default:
1890                         return ERROR_OK;
1891                 }
1892                 if (retval!=ERROR_OK)
1893                 {
1894                         return retval;
1895                 }
1896         }
1897
1898         return ERROR_OK;
1899
1900 }
1901
1902 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1903 {
1904         u8 *buffer;
1905         u32 buf_cnt;
1906         u32 image_size;
1907         u32 min_address=0;
1908         u32 max_address=0xffffffff;
1909         int i;
1910         int retval;
1911
1912         image_t image;
1913
1914         duration_t duration;
1915         char *duration_text;
1916
1917         target_t *target = get_current_target(cmd_ctx);
1918
1919         if ((argc < 1)||(argc > 5))
1920         {
1921                 return ERROR_COMMAND_SYNTAX_ERROR;
1922         }
1923
1924         /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
1925         if (argc >= 2)
1926         {
1927                 image.base_address_set = 1;
1928                 image.base_address = strtoul(args[1], NULL, 0);
1929         }
1930         else
1931         {
1932                 image.base_address_set = 0;
1933         }
1934
1935
1936         image.start_address_set = 0;
1937
1938         if (argc>=4)
1939         {
1940                 min_address=strtoul(args[3], NULL, 0);
1941         }
1942         if (argc>=5)
1943         {
1944                 max_address=strtoul(args[4], NULL, 0)+min_address;
1945         }
1946
1947         if (min_address>max_address)
1948         {
1949                 return ERROR_COMMAND_SYNTAX_ERROR;
1950         }
1951
1952
1953         duration_start_measure(&duration);
1954
1955         if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
1956         {
1957                 return ERROR_OK;
1958         }
1959
1960         image_size = 0x0;
1961         retval = ERROR_OK;
1962         for (i = 0; i < image.num_sections; i++)
1963         {
1964                 buffer = malloc(image.sections[i].size);
1965                 if (buffer == NULL)
1966                 {
1967                         command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
1968                         break;
1969                 }
1970
1971                 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
1972                 {
1973                         free(buffer);
1974                         break;
1975                 }
1976
1977                 u32 offset=0;
1978                 u32 length=buf_cnt;
1979
1980
1981                 /* DANGER!!! beware of unsigned comparision here!!! */
1982
1983                 if ((image.sections[i].base_address+buf_cnt>=min_address)&&
1984                                 (image.sections[i].base_address<max_address))
1985                 {
1986                         if (image.sections[i].base_address<min_address)
1987                         {
1988                                 /* clip addresses below */
1989                                 offset+=min_address-image.sections[i].base_address;
1990                                 length-=offset;
1991                         }
1992
1993                         if (image.sections[i].base_address+buf_cnt>max_address)
1994                         {
1995                                 length-=(image.sections[i].base_address+buf_cnt)-max_address;
1996                         }
1997
1998                         if ((retval = target_write_buffer(target, image.sections[i].base_address+offset, length, buffer+offset)) != ERROR_OK)
1999                         {
2000                                 free(buffer);
2001                                 break;
2002                         }
2003                         image_size += length;
2004                         command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
2005                 }
2006
2007                 free(buffer);
2008         }
2009
2010         duration_stop_measure(&duration, &duration_text);
2011         if (retval==ERROR_OK)
2012         {
2013                 command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
2014         }
2015         free(duration_text);
2016
2017         image_close(&image);
2018
2019         return retval;
2020
2021 }
2022
2023 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2024 {
2025         fileio_t fileio;
2026
2027         u32 address;
2028         u32 size;
2029         u8 buffer[560];
2030         int retval=ERROR_OK;
2031
2032         duration_t duration;
2033         char *duration_text;
2034
2035         target_t *target = get_current_target(cmd_ctx);
2036
2037         if (argc != 3)
2038         {
2039                 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2040                 return ERROR_OK;
2041         }
2042
2043         address = strtoul(args[1], NULL, 0);
2044         size = strtoul(args[2], NULL, 0);
2045
2046         if ((address & 3) || (size & 3))
2047         {
2048                 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
2049                 return ERROR_OK;
2050         }
2051
2052         if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2053         {
2054                 return ERROR_OK;
2055         }
2056
2057         duration_start_measure(&duration);
2058
2059         while (size > 0)
2060         {
2061                 u32 size_written;
2062                 u32 this_run_size = (size > 560) ? 560 : size;
2063
2064                 retval = target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
2065                 if (retval != ERROR_OK)
2066                 {
2067                         break;
2068                 }
2069
2070                 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2071                 if (retval != ERROR_OK)
2072                 {
2073                         break;
2074                 }
2075
2076                 size -= this_run_size;
2077                 address += this_run_size;
2078         }
2079
2080         fileio_close(&fileio);
2081
2082         duration_stop_measure(&duration, &duration_text);
2083         if (retval==ERROR_OK)
2084         {
2085                 command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
2086         }
2087         free(duration_text);
2088
2089         return ERROR_OK;
2090 }
2091
2092 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2093 {
2094         u8 *buffer;
2095         u32 buf_cnt;
2096         u32 image_size;
2097         int i;
2098         int retval;
2099         u32 checksum = 0;
2100         u32 mem_checksum = 0;
2101
2102         image_t image;
2103
2104         duration_t duration;
2105         char *duration_text;
2106
2107         target_t *target = get_current_target(cmd_ctx);
2108
2109         if (argc < 1)
2110         {
2111                 return ERROR_COMMAND_SYNTAX_ERROR;
2112         }
2113
2114         if (!target)
2115         {
2116                 LOG_ERROR("no target selected");
2117                 return ERROR_FAIL;
2118         }
2119
2120         duration_start_measure(&duration);
2121
2122         if (argc >= 2)
2123         {
2124                 image.base_address_set = 1;
2125                 image.base_address = strtoul(args[1], NULL, 0);
2126         }
2127         else
2128         {
2129                 image.base_address_set = 0;
2130                 image.base_address = 0x0;
2131         }
2132
2133         image.start_address_set = 0;
2134
2135         if ((retval=image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2136         {
2137                 return retval;
2138         }
2139
2140         image_size = 0x0;
2141         retval=ERROR_OK;
2142         for (i = 0; i < image.num_sections; i++)
2143         {
2144                 buffer = malloc(image.sections[i].size);
2145                 if (buffer == NULL)
2146                 {
2147                         command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2148                         break;
2149                 }
2150                 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2151                 {
2152                         free(buffer);
2153                         break;
2154                 }
2155
2156                 /* calculate checksum of image */
2157                 image_calculate_checksum( buffer, buf_cnt, &checksum );
2158
2159                 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2160                 if( retval != ERROR_OK )
2161                 {
2162                         free(buffer);
2163                         break;
2164                 }
2165
2166                 if( checksum != mem_checksum )
2167                 {
2168                         /* failed crc checksum, fall back to a binary compare */
2169                         u8 *data;
2170
2171                         command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2172
2173                         data = (u8*)malloc(buf_cnt);
2174
2175                         /* Can we use 32bit word accesses? */
2176                         int size = 1;
2177                         int count = buf_cnt;
2178                         if ((count % 4) == 0)
2179                         {
2180                                 size *= 4;
2181                                 count /= 4;
2182                         }
2183                         retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
2184                         if (retval == ERROR_OK)
2185                         {
2186                                 int t;
2187                                 for (t = 0; t < buf_cnt; t++)
2188                                 {
2189                                         if (data[t] != buffer[t])
2190                                         {
2191                                                 command_print(cmd_ctx, "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n", t + image.sections[i].base_address, data[t], buffer[t]);
2192                                                 free(data);
2193                                                 free(buffer);
2194                                                 retval=ERROR_FAIL;
2195                                                 goto done;
2196                                         }
2197                                 }
2198                         }
2199
2200                         free(data);
2201                 }
2202
2203                 free(buffer);
2204                 image_size += buf_cnt;
2205         }
2206 done:
2207         duration_stop_measure(&duration, &duration_text);
2208         if (retval==ERROR_OK)
2209         {
2210                 command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
2211         }
2212         free(duration_text);
2213
2214         image_close(&image);
2215
2216         return retval;
2217 }
2218
2219 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2220 {
2221         int retval;
2222         target_t *target = get_current_target(cmd_ctx);
2223
2224         if (argc == 0)
2225         {
2226                 breakpoint_t *breakpoint = target->breakpoints;
2227
2228                 while (breakpoint)
2229                 {
2230                         if (breakpoint->type == BKPT_SOFT)
2231                         {
2232                                 char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
2233                                 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
2234                                 free(buf);
2235                         }
2236                         else
2237                         {
2238                                 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
2239                         }
2240                         breakpoint = breakpoint->next;
2241                 }
2242         }
2243         else if (argc >= 2)
2244         {
2245                 int hw = BKPT_SOFT;
2246                 u32 length = 0;
2247
2248                 length = strtoul(args[1], NULL, 0);
2249
2250                 if (argc >= 3)
2251                         if (strcmp(args[2], "hw") == 0)
2252                                 hw = BKPT_HARD;
2253
2254                 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
2255                 {
2256                         LOG_ERROR("Failure setting breakpoints");
2257                 }
2258                 else
2259                 {
2260                         command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
2261                 }
2262         }
2263         else
2264         {
2265                 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2266         }
2267
2268         return ERROR_OK;
2269 }
2270
2271 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2272 {
2273         target_t *target = get_current_target(cmd_ctx);
2274
2275         if (argc > 0)
2276                 breakpoint_remove(target, strtoul(args[0], NULL, 0));
2277
2278         return ERROR_OK;
2279 }
2280
2281 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2282 {
2283         target_t *target = get_current_target(cmd_ctx);
2284         int retval;
2285
2286         if (argc == 0)
2287         {
2288                 watchpoint_t *watchpoint = target->watchpoints;
2289
2290                 while (watchpoint)
2291                 {
2292                         command_print(cmd_ctx, "address: 0x%8.8x, len: 0x%8.8x, r/w/a: %i, value: 0x%8.8x, mask: 0x%8.8x", watchpoint->address, watchpoint->length, watchpoint->rw, watchpoint->value, watchpoint->mask);
2293                         watchpoint = watchpoint->next;
2294                 }
2295         }
2296         else if (argc >= 2)
2297         {
2298                 enum watchpoint_rw type = WPT_ACCESS;
2299                 u32 data_value = 0x0;
2300                 u32 data_mask = 0xffffffff;
2301
2302                 if (argc >= 3)
2303                 {
2304                         switch(args[2][0])
2305                         {
2306                                 case 'r':
2307                                         type = WPT_READ;
2308                                         break;
2309                                 case 'w':
2310                                         type = WPT_WRITE;
2311                                         break;
2312                                 case 'a':
2313                                         type = WPT_ACCESS;
2314                                         break;
2315                                 default:
2316                                         command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2317                                         return ERROR_OK;
2318                         }
2319                 }
2320                 if (argc >= 4)
2321                 {
2322                         data_value = strtoul(args[3], NULL, 0);
2323                 }
2324                 if (argc >= 5)
2325                 {
2326                         data_mask = strtoul(args[4], NULL, 0);
2327                 }
2328
2329                 if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
2330                                 strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
2331                 {
2332                         LOG_ERROR("Failure setting breakpoints");
2333                 }
2334         }
2335         else
2336         {
2337                 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2338         }
2339
2340         return ERROR_OK;
2341 }
2342
2343 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2344 {
2345         target_t *target = get_current_target(cmd_ctx);
2346
2347         if (argc > 0)
2348                 watchpoint_remove(target, strtoul(args[0], NULL, 0));
2349
2350         return ERROR_OK;
2351 }
2352
2353 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
2354 {
2355         int retval;
2356         target_t *target = get_current_target(cmd_ctx);
2357         u32 va;
2358         u32 pa;
2359
2360         if (argc != 1)
2361         {
2362                 return ERROR_COMMAND_SYNTAX_ERROR;
2363         }
2364         va = strtoul(args[0], NULL, 0);
2365
2366         retval = target->type->virt2phys(target, va, &pa);
2367         if (retval == ERROR_OK)
2368         {
2369                 command_print(cmd_ctx, "Physical address 0x%08x", pa);
2370         }
2371         else
2372         {
2373                 /* lower levels will have logged a detailed error which is
2374                  * forwarded to telnet/GDB session.
2375                  */
2376         }
2377         return retval;
2378 }
2379 static void writeLong(FILE *f, int l)
2380 {
2381         int i;
2382         for (i=0; i<4; i++)
2383         {
2384                 char c=(l>>(i*8))&0xff;
2385                 fwrite(&c, 1, 1, f);
2386         }
2387
2388 }
2389 static void writeString(FILE *f, char *s)
2390 {
2391         fwrite(s, 1, strlen(s), f);
2392 }
2393
2394
2395
2396 // Dump a gmon.out histogram file.
2397 static void writeGmon(u32 *samples, int sampleNum, char *filename)
2398 {
2399         int i;
2400         FILE *f=fopen(filename, "w");
2401         if (f==NULL)
2402                 return;
2403         fwrite("gmon", 1, 4, f);
2404         writeLong(f, 0x00000001); // Version
2405         writeLong(f, 0); // padding
2406         writeLong(f, 0); // padding
2407         writeLong(f, 0); // padding
2408
2409         fwrite("", 1, 1, f);  // GMON_TAG_TIME_HIST
2410
2411         // figure out bucket size
2412         u32 min=samples[0];
2413         u32 max=samples[0];
2414         for (i=0; i<sampleNum; i++)
2415         {
2416                 if (min>samples[i])
2417                 {
2418                         min=samples[i];
2419                 }
2420                 if (max<samples[i])
2421                 {
2422                         max=samples[i];
2423                 }
2424         }
2425
2426         int addressSpace=(max-min+1);
2427
2428         static int const maxBuckets=256*1024; // maximum buckets.
2429         int length=addressSpace;
2430         if (length > maxBuckets)
2431         {
2432                 length=maxBuckets;
2433         }
2434         int *buckets=malloc(sizeof(int)*length);
2435         if (buckets==NULL)
2436         {
2437                 fclose(f);
2438                 return;
2439         }
2440         memset(buckets, 0, sizeof(int)*length);
2441         for (i=0; i<sampleNum;i++)
2442         {
2443                 u32 address=samples[i];
2444                 long long a=address-min;
2445                 long long b=length-1;
2446                 long long c=addressSpace-1;
2447                 int index=(a*b)/c; // danger!!!! int32 overflows
2448                 buckets[index]++;
2449         }
2450
2451         //                         append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr))
2452         writeLong(f, min);                                      // low_pc
2453         writeLong(f, max);              // high_pc
2454         writeLong(f, length);           // # of samples
2455         writeLong(f, 64000000);                         // 64MHz
2456         writeString(f, "seconds");
2457         for (i=0; i<(15-strlen("seconds")); i++)
2458         {
2459                 fwrite("", 1, 1, f);  // padding
2460         }
2461         writeString(f, "s");
2462
2463 //                         append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size)
2464
2465         char *data=malloc(2*length);
2466         if (data!=NULL)
2467         {
2468                 for (i=0; i<length;i++)
2469                 {
2470                         int val;
2471                         val=buckets[i];
2472                         if (val>65535)
2473                         {
2474                                 val=65535;
2475                         }
2476                         data[i*2]=val&0xff;
2477                         data[i*2+1]=(val>>8)&0xff;
2478                 }
2479                 free(buckets);
2480                 fwrite(data, 1, length*2, f);
2481                 free(data);
2482         } else
2483         {
2484                 free(buckets);
2485         }
2486
2487         fclose(f);
2488 }
2489
2490 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2491 int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2492 {
2493         target_t *target = get_current_target(cmd_ctx);
2494         struct timeval timeout, now;
2495
2496         gettimeofday(&timeout, NULL);
2497         if (argc!=2)
2498         {
2499                 return ERROR_COMMAND_SYNTAX_ERROR;
2500         }
2501         char *end;
2502         timeval_add_time(&timeout, strtoul(args[0], &end, 0), 0);
2503         if (*end)
2504         {
2505                 return ERROR_OK;
2506         }
2507
2508         command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
2509
2510         static const int maxSample=10000;
2511         u32 *samples=malloc(sizeof(u32)*maxSample);
2512         if (samples==NULL)
2513                 return ERROR_OK;
2514
2515         int numSamples=0;
2516         int retval=ERROR_OK;
2517         // hopefully it is safe to cache! We want to stop/restart as quickly as possible.
2518         reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
2519
2520         for (;;)
2521         {
2522                 target_poll(target);
2523                 if (target->state == TARGET_HALTED)
2524                 {
2525                         u32 t=*((u32 *)reg->value);
2526                         samples[numSamples++]=t;
2527                         retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2528                         target_poll(target);
2529                         usleep(10*1000); // sleep 10ms, i.e. <100 samples/second.
2530                 } else if (target->state == TARGET_RUNNING)
2531                 {
2532                         // We want to quickly sample the PC.
2533                         target_halt(target);
2534                 } else
2535                 {
2536                         command_print(cmd_ctx, "Target not halted or running");
2537                         retval=ERROR_OK;
2538                         break;
2539                 }
2540                 if (retval!=ERROR_OK)
2541                 {
2542                         break;
2543                 }
2544
2545                 gettimeofday(&now, NULL);
2546                 if ((numSamples>=maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
2547                 {
2548                         command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2549                         target_poll(target);
2550                         if (target->state == TARGET_HALTED)
2551                         {
2552                                 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2553                         }
2554                         target_poll(target);
2555                         writeGmon(samples, numSamples, args[1]);
2556                         command_print(cmd_ctx, "Wrote %s", args[1]);
2557                         break;
2558                 }
2559         }
2560         free(samples);
2561
2562         return ERROR_OK;
2563 }
2564
2565 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 val)
2566 {
2567         char *namebuf;
2568         Jim_Obj *nameObjPtr, *valObjPtr;
2569         int result;
2570
2571         namebuf = alloc_printf("%s(%d)", varname, idx);
2572         if (!namebuf)
2573                 return JIM_ERR;
2574
2575         nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2576         valObjPtr = Jim_NewIntObj(interp, val);
2577         if (!nameObjPtr || !valObjPtr)
2578         {
2579                 free(namebuf);
2580                 return JIM_ERR;
2581         }
2582
2583         Jim_IncrRefCount(nameObjPtr);
2584         Jim_IncrRefCount(valObjPtr);
2585         result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
2586         Jim_DecrRefCount(interp, nameObjPtr);
2587         Jim_DecrRefCount(interp, valObjPtr);
2588         free(namebuf);
2589         /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
2590         return result;
2591 }
2592
2593 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2594 {
2595         target_t *target;
2596         command_context_t *context;
2597         long l;
2598         u32 width;
2599         u32 len;
2600         u32 addr;
2601         u32 count;
2602         u32 v;
2603         const char *varname;
2604         u8 buffer[4096];
2605         int  i, n, e, retval;
2606
2607         /* argv[1] = name of array to receive the data
2608          * argv[2] = desired width
2609          * argv[3] = memory address
2610          * argv[4] = count of times to read
2611          */
2612         if (argc != 5) {
2613                 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2614                 return JIM_ERR;
2615         }
2616         varname = Jim_GetString(argv[1], &len);
2617         /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2618
2619         e = Jim_GetLong(interp, argv[2], &l);
2620         width = l;
2621         if (e != JIM_OK) {
2622                 return e;
2623         }
2624
2625         e = Jim_GetLong(interp, argv[3], &l);
2626         addr = l;
2627         if (e != JIM_OK) {
2628                 return e;
2629         }
2630         e = Jim_GetLong(interp, argv[4], &l);
2631         len = l;
2632         if (e != JIM_OK) {
2633                 return e;
2634         }
2635         switch (width) {
2636                 case 8:
2637                         width = 1;
2638                         break;
2639                 case 16:
2640                         width = 2;
2641                         break;
2642                 case 32:
2643                         width = 4;
2644                         break;
2645                 default:
2646                         Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2647                         Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2648                         return JIM_ERR;
2649         }
2650         if (len == 0) {
2651                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2652                 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
2653                 return JIM_ERR;
2654         }
2655         if ((addr + (len * width)) < addr) {
2656                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2657                 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
2658                 return JIM_ERR;
2659         }
2660         /* absurd transfer size? */
2661         if (len > 65536) {
2662                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2663                 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
2664                 return JIM_ERR;
2665         }
2666
2667         if ((width == 1) ||
2668                 ((width == 2) && ((addr & 1) == 0)) ||
2669                 ((width == 4) && ((addr & 3) == 0))) {
2670                 /* all is well */
2671         } else {
2672                 char buf[100];
2673                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2674                 sprintf(buf, "mem2array address: 0x%08x is not aligned for %d byte reads", addr, width);
2675                 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2676                 return JIM_ERR;
2677         }
2678
2679         context = Jim_GetAssocData(interp, "context");
2680         if (context == NULL)
2681         {
2682                 LOG_ERROR("mem2array: no command context");
2683                 return JIM_ERR;
2684         }
2685         target = get_current_target(context);
2686         if (target == NULL)
2687         {
2688                 LOG_ERROR("mem2array: no current target");
2689                 return JIM_ERR;
2690         }
2691
2692         /* Transfer loop */
2693
2694         /* index counter */
2695         n = 0;
2696         /* assume ok */
2697         e = JIM_OK;
2698         while (len) {
2699                 /* Slurp... in buffer size chunks */
2700
2701                 count = len; /* in objects.. */
2702                 if (count > (sizeof(buffer)/width)) {
2703                         count = (sizeof(buffer)/width);
2704                 }
2705
2706                 retval = target->type->read_memory( target, addr, width, count, buffer );
2707                 if (retval != ERROR_OK) {
2708                         /* BOO !*/
2709                         LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
2710                         Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2711                         Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
2712                         e = JIM_ERR;
2713                         len = 0;
2714                 } else {
2715                         v = 0; /* shut up gcc */
2716                         for (i = 0 ;i < count ;i++, n++) {
2717                                 switch (width) {
2718                                         case 4:
2719                                                 v = target_buffer_get_u32(target, &buffer[i*width]);
2720                                                 break;
2721                                         case 2:
2722                                                 v = target_buffer_get_u16(target, &buffer[i*width]);
2723                                                 break;
2724                                         case 1:
2725                                                 v = buffer[i] & 0x0ff;
2726                                                 break;
2727                                 }
2728                                 new_int_array_element(interp, varname, n, v);
2729                         }
2730                         len -= count;
2731                 }
2732         }
2733
2734         Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2735
2736         return JIM_OK;
2737 }
2738
2739 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 *val)
2740 {
2741         char *namebuf;
2742         Jim_Obj *nameObjPtr, *valObjPtr;
2743         int result;
2744         long l;
2745
2746         namebuf = alloc_printf("%s(%d)", varname, idx);
2747         if (!namebuf)
2748                 return JIM_ERR;
2749
2750         nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2751         if (!nameObjPtr)
2752         {
2753                 free(namebuf);
2754                 return JIM_ERR;
2755         }
2756
2757         Jim_IncrRefCount(nameObjPtr);
2758         valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
2759         Jim_DecrRefCount(interp, nameObjPtr);
2760         free(namebuf);
2761         if (valObjPtr == NULL)
2762                 return JIM_ERR;
2763
2764         result = Jim_GetLong(interp, valObjPtr, &l);
2765         /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
2766         *val = l;
2767         return result;
2768 }
2769
2770 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2771 {
2772         target_t *target;
2773         command_context_t *context;
2774         long l;
2775         u32 width;
2776         u32 len;
2777         u32 addr;
2778         u32 count;
2779         u32 v;
2780         const char *varname;
2781         u8 buffer[4096];
2782         int  i, n, e, retval;
2783
2784         /* argv[1] = name of array to get the data
2785          * argv[2] = desired width
2786          * argv[3] = memory address
2787          * argv[4] = count to write
2788          */
2789         if (argc != 5) {
2790                 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2791                 return JIM_ERR;
2792         }
2793         varname = Jim_GetString(argv[1], &len);
2794         /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2795
2796         e = Jim_GetLong(interp, argv[2], &l);
2797         width = l;
2798         if (e != JIM_OK) {
2799                 return e;
2800         }
2801
2802         e = Jim_GetLong(interp, argv[3], &l);
2803         addr = l;
2804         if (e != JIM_OK) {
2805                 return e;
2806         }
2807         e = Jim_GetLong(interp, argv[4], &l);
2808         len = l;
2809         if (e != JIM_OK) {
2810                 return e;
2811         }
2812         switch (width) {
2813                 case 8:
2814                         width = 1;
2815                         break;
2816                 case 16:
2817                         width = 2;
2818                         break;
2819                 case 32:
2820                         width = 4;
2821                         break;
2822                 default:
2823                         Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2824                         Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2825                         return JIM_ERR;
2826         }
2827         if (len == 0) {
2828                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2829                 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
2830                 return JIM_ERR;
2831         }
2832         if ((addr + (len * width)) < addr) {
2833                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2834                 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
2835                 return JIM_ERR;
2836         }
2837         /* absurd transfer size? */
2838         if (len > 65536) {
2839                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2840                 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
2841                 return JIM_ERR;
2842         }
2843
2844         if ((width == 1) ||
2845                 ((width == 2) && ((addr & 1) == 0)) ||
2846                 ((width == 4) && ((addr & 3) == 0))) {
2847                 /* all is well */
2848         } else {
2849                 char buf[100];
2850                 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2851                 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads", addr, width);
2852                 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2853                 return JIM_ERR;
2854         }
2855
2856         context = Jim_GetAssocData(interp, "context");
2857         if (context == NULL)
2858         {
2859                 LOG_ERROR("array2mem: no command context");
2860                 return JIM_ERR;
2861         }
2862         target = get_current_target(context);
2863         if (target == NULL)
2864         {
2865                 LOG_ERROR("array2mem: no current target");
2866                 return JIM_ERR;
2867         }
2868
2869         /* Transfer loop */
2870
2871         /* index counter */
2872         n = 0;
2873         /* assume ok */
2874         e = JIM_OK;
2875         while (len) {
2876                 /* Slurp... in buffer size chunks */
2877
2878                 count = len; /* in objects.. */
2879                 if (count > (sizeof(buffer)/width)) {
2880                         count = (sizeof(buffer)/width);
2881                 }
2882
2883                 v = 0; /* shut up gcc */
2884                 for (i = 0 ;i < count ;i++, n++) {
2885                         get_int_array_element(interp, varname, n, &v);
2886                         switch (width) {
2887                         case 4:
2888                                 target_buffer_set_u32(target, &buffer[i*width], v);
2889                                 break;
2890                         case 2:
2891                                 target_buffer_set_u16(target, &buffer[i*width], v);
2892                                 break;
2893                         case 1:
2894                                 buffer[i] = v & 0x0ff;
2895                                 break;
2896                         }
2897                 }
2898                 len -= count;
2899
2900                 retval = target->type->write_memory(target, addr, width, count, buffer);
2901                 if (retval != ERROR_OK) {
2902                         /* BOO !*/
2903                         LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
2904                         Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2905                         Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
2906                         e = JIM_ERR;
2907                         len = 0;
2908                 }
2909         }
2910
2911         Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2912
2913         return JIM_OK;
2914 }