- Fixed bug in pathmove for XScale
[fw/openocd] / src / target / target.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "replacements.h"
25 #include "target.h"
26 #include "target_request.h"
27
28 #include "log.h"
29 #include "configuration.h"
30 #include "binarybuffer.h"
31 #include "jtag.h"
32
33 #include <string.h>
34 #include <stdlib.h>
35 #include <inttypes.h>
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40 #include <errno.h>
41
42 #include <sys/time.h>
43 #include <time.h>
44
45 #include <time_support.h>
46
47 #include <fileio.h>
48 #include <image.h>
49
50 int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv);
51
52 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
54 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
55
56 int handle_target_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
57 int handle_run_and_halt_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
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
79 /* targets
80  */
81 extern target_type_t arm7tdmi_target;
82 extern target_type_t arm720t_target;
83 extern target_type_t arm9tdmi_target;
84 extern target_type_t arm920t_target;
85 extern target_type_t arm966e_target;
86 extern target_type_t arm926ejs_target;
87 extern target_type_t xscale_target;
88 extern target_type_t cortexm3_target;
89
90 target_type_t *target_types[] =
91 {
92         &arm7tdmi_target,
93         &arm9tdmi_target,
94         &arm920t_target,
95         &arm720t_target,
96         &arm966e_target,
97         &arm926ejs_target,
98         &xscale_target,
99         &cortexm3_target,
100         NULL,
101 };
102
103 target_t *targets = NULL;
104 target_event_callback_t *target_event_callbacks = NULL;
105 target_timer_callback_t *target_timer_callbacks = NULL;
106
107 char *target_state_strings[] =
108 {
109         "unknown",
110         "running",
111         "halted",
112         "reset",
113         "debug_running",
114 };
115
116 char *target_debug_reason_strings[] =
117 {
118         "debug request", "breakpoint", "watchpoint",
119         "watchpoint and breakpoint", "single step",
120         "target not halted"
121 };
122
123 char *target_endianess_strings[] =
124 {
125         "big endian",
126         "little endian",
127 };
128
129 enum daemon_startup_mode startup_mode = DAEMON_ATTACH;
130
131 static int target_continous_poll = 1;
132
133 /* read a u32 from a buffer in target memory endianness */
134 u32 target_buffer_get_u32(target_t *target, u8 *buffer)
135 {
136         if (target->endianness == TARGET_LITTLE_ENDIAN)
137                 return le_to_h_u32(buffer);
138         else
139                 return be_to_h_u32(buffer);
140 }
141
142 /* read a u16 from a buffer in target memory endianness */
143 u16 target_buffer_get_u16(target_t *target, u8 *buffer)
144 {
145         if (target->endianness == TARGET_LITTLE_ENDIAN)
146                 return le_to_h_u16(buffer);
147         else
148                 return be_to_h_u16(buffer);
149 }
150
151 /* write a u32 to a buffer in target memory endianness */
152 void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
153 {
154         if (target->endianness == TARGET_LITTLE_ENDIAN)
155                 h_u32_to_le(buffer, value);
156         else
157                 h_u32_to_be(buffer, value);
158 }
159
160 /* write a u16 to a buffer in target memory endianness */
161 void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
162 {
163         if (target->endianness == TARGET_LITTLE_ENDIAN)
164                 h_u16_to_le(buffer, value);
165         else
166                 h_u16_to_be(buffer, value);
167 }
168
169 /* returns a pointer to the n-th configured target */
170 target_t* get_target_by_num(int num)
171 {
172         target_t *target = targets;
173         int i = 0;
174
175         while (target)
176         {
177                 if (num == i)
178                         return target;
179                 target = target->next;
180                 i++;
181         }
182
183         return NULL;
184 }
185
186 int get_num_by_target(target_t *query_target)
187 {
188         target_t *target = targets;
189         int i = 0;      
190         
191         while (target)
192         {
193                 if (target == query_target)
194                         return i;
195                 target = target->next;
196                 i++;
197         }
198         
199         return -1;
200 }
201
202 target_t* get_current_target(command_context_t *cmd_ctx)
203 {
204         target_t *target = get_target_by_num(cmd_ctx->current_target);
205         
206         if (target == NULL)
207         {
208                 ERROR("BUG: current_target out of bounds");
209                 exit(-1);
210         }
211         
212         return target;
213 }
214
215 /* Process target initialization, when target entered debug out of reset
216  * the handler is unregistered at the end of this function, so it's only called once
217  */
218 int target_init_handler(struct target_s *target, enum target_event event, void *priv)
219 {
220         FILE *script;
221         struct command_context_s *cmd_ctx = priv;
222         
223         if ((event == TARGET_EVENT_HALTED) && (target->reset_script))
224         {
225                 target_unregister_event_callback(target_init_handler, priv);
226
227                 script = fopen(target->reset_script, "r");
228                 if (!script)
229                 {
230                         ERROR("couldn't open script file %s", target->reset_script);
231                                 return ERROR_OK;
232                 }
233
234                 INFO("executing reset script '%s'", target->reset_script);
235                 command_run_file(cmd_ctx, script, COMMAND_EXEC);
236                 fclose(script);
237
238                 jtag_execute_queue();
239         }
240         
241         return ERROR_OK;
242 }
243
244 int target_run_and_halt_handler(void *priv)
245 {
246         target_t *target = priv;
247         
248         target->type->halt(target);
249         
250         return ERROR_OK;
251 }
252
253 int target_process_reset(struct command_context_s *cmd_ctx)
254 {
255         int retval = ERROR_OK;
256         target_t *target;
257         struct timeval timeout, now;
258         
259         /* prepare reset_halt where necessary */
260         target = targets;
261         while (target)
262         {
263                 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
264                 {
265                         switch (target->reset_mode)
266                         {
267                                 case RESET_HALT:
268                                         command_print(cmd_ctx, "nSRST pulls nTRST, falling back to RESET_RUN_AND_HALT");
269                                         target->reset_mode = RESET_RUN_AND_HALT;
270                                         break;
271                                 case RESET_INIT:
272                                         command_print(cmd_ctx, "nSRST pulls nTRST, falling back to RESET_RUN_AND_INIT");
273                                         target->reset_mode = RESET_RUN_AND_INIT;
274                                         break;
275                                 default:
276                                         break;
277                         } 
278                 }
279                 switch (target->reset_mode)
280                 {
281                         case RESET_HALT:
282                         case RESET_INIT:
283                                 target->type->prepare_reset_halt(target);
284                                 break;
285                         default:
286                                 break;
287                 }
288                 target = target->next;
289         }
290         
291         target = targets;
292         while (target)
293         {
294                 target->type->assert_reset(target);
295                 target = target->next;
296         }
297         jtag_execute_queue();
298         
299         /* request target halt if necessary, and schedule further action */
300         target = targets;
301         while (target)
302         {
303                 switch (target->reset_mode)
304                 {
305                         case RESET_RUN:
306                                 /* nothing to do if target just wants to be run */
307                                 break;
308                         case RESET_RUN_AND_HALT:
309                                 /* schedule halt */
310                                 target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
311                                 break;
312                         case RESET_RUN_AND_INIT:
313                                 /* schedule halt */
314                                 target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
315                                 target_register_event_callback(target_init_handler, cmd_ctx);
316                                 break;
317                         case RESET_HALT:
318                                 target->type->halt(target);
319                                 break;
320                         case RESET_INIT:
321                                 target->type->halt(target);
322                                 target_register_event_callback(target_init_handler, cmd_ctx);
323                                 break;
324                         default:
325                                 ERROR("BUG: unknown target->reset_mode");
326                 }
327                 target = target->next;
328         }
329         
330         target = targets;
331         while (target)
332         {
333                 target->type->deassert_reset(target);
334                 target = target->next;
335         }
336         jtag_execute_queue();
337
338         /* Wait for reset to complete, maximum 5 seconds. */    
339         gettimeofday(&timeout, NULL);
340         timeval_add_time(&timeout, 5, 0);
341         for(;;)
342         {
343                 gettimeofday(&now, NULL);
344                 
345                 target_call_timer_callbacks();
346                 
347                 target = targets;
348                 while (target)
349                 {
350                         target->type->poll(target);
351                         if ((target->reset_mode == RESET_RUN_AND_INIT) || (target->reset_mode == RESET_RUN_AND_HALT))
352                         {
353                                 if (target->state != TARGET_HALTED)
354                                 {
355                                         if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
356                                         {
357                                                 command_print(cmd_ctx, "Timed out waiting for reset");
358                                                 goto done;
359                                         }
360                                         usleep(100*1000); /* Do not eat all cpu */
361                                         goto again;
362                                 }
363                         }
364                         target = target->next;
365                 }
366                 /* All targets we're waiting for are halted */
367                 break;
368                 
369                 again:;
370         }
371         done:
372         
373         
374         /* We want any events to be processed before the prompt */
375         target_call_timer_callbacks();
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         USER("No MMU present");
389         *enabled = 0;
390         return ERROR_OK;
391 }
392
393 int target_init(struct command_context_s *cmd_ctx)
394 {
395         target_t *target = targets;
396         
397         while (target)
398         {
399                 if (target->type->init_target(cmd_ctx, target) != ERROR_OK)
400                 {
401                         ERROR("target '%s' init failed", target->type->name);
402                         exit(-1);
403                 }
404                 
405                 /* Set up default functions if none are provided by target */
406                 if (target->type->virt2phys == NULL)
407                 {
408                         target->type->virt2phys = default_virt2phys;
409                 }
410                 if (target->type->mmu == NULL)
411                 {
412                         target->type->mmu = default_mmu;
413                 }
414                 target = target->next;
415         }
416         
417         if (targets)
418         {
419                 target_register_user_commands(cmd_ctx);
420                 target_register_timer_callback(handle_target, 100, 1, NULL);
421         }
422                 
423         return ERROR_OK;
424 }
425
426 int target_init_reset(struct command_context_s *cmd_ctx)
427 {
428         if (startup_mode == DAEMON_RESET)
429                 target_process_reset(cmd_ctx);
430         
431         return ERROR_OK;
432 }
433
434 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
435 {
436         target_event_callback_t **callbacks_p = &target_event_callbacks;
437         
438         if (callback == NULL)
439         {
440                 return ERROR_INVALID_ARGUMENTS;
441         }
442         
443         if (*callbacks_p)
444         {
445                 while ((*callbacks_p)->next)
446                         callbacks_p = &((*callbacks_p)->next);
447                 callbacks_p = &((*callbacks_p)->next);
448         }
449         
450         (*callbacks_p) = malloc(sizeof(target_event_callback_t));
451         (*callbacks_p)->callback = callback;
452         (*callbacks_p)->priv = priv;
453         (*callbacks_p)->next = NULL;
454         
455         return ERROR_OK;
456 }
457
458 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
459 {
460         target_timer_callback_t **callbacks_p = &target_timer_callbacks;
461         struct timeval now;
462         
463         if (callback == NULL)
464         {
465                 return ERROR_INVALID_ARGUMENTS;
466         }
467         
468         if (*callbacks_p)
469         {
470                 while ((*callbacks_p)->next)
471                         callbacks_p = &((*callbacks_p)->next);
472                 callbacks_p = &((*callbacks_p)->next);
473         }
474         
475         (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
476         (*callbacks_p)->callback = callback;
477         (*callbacks_p)->periodic = periodic;
478         (*callbacks_p)->time_ms = time_ms;
479         
480         gettimeofday(&now, NULL);
481         (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
482         time_ms -= (time_ms % 1000);
483         (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
484         if ((*callbacks_p)->when.tv_usec > 1000000)
485         {
486                 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
487                 (*callbacks_p)->when.tv_sec += 1;
488         }
489         
490         (*callbacks_p)->priv = priv;
491         (*callbacks_p)->next = NULL;
492         
493         return ERROR_OK;
494 }
495
496 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
497 {
498         target_event_callback_t **p = &target_event_callbacks;
499         target_event_callback_t *c = target_event_callbacks;
500         
501         if (callback == NULL)
502         {
503                 return ERROR_INVALID_ARGUMENTS;
504         }
505                 
506         while (c)
507         {
508                 target_event_callback_t *next = c->next;
509                 if ((c->callback == callback) && (c->priv == priv))
510                 {
511                         *p = next;
512                         free(c);
513                         return ERROR_OK;
514                 }
515                 else
516                         p = &(c->next);
517                 c = next;
518         }
519         
520         return ERROR_OK;
521 }
522
523 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
524 {
525         target_timer_callback_t **p = &target_timer_callbacks;
526         target_timer_callback_t *c = target_timer_callbacks;
527         
528         if (callback == NULL)
529         {
530                 return ERROR_INVALID_ARGUMENTS;
531         }
532                 
533         while (c)
534         {
535                 target_timer_callback_t *next = c->next;
536                 if ((c->callback == callback) && (c->priv == priv))
537                 {
538                         *p = next;
539                         free(c);
540                         return ERROR_OK;
541                 }
542                 else
543                         p = &(c->next);
544                 c = next;
545         }
546         
547         return ERROR_OK;
548 }
549
550 int target_call_event_callbacks(target_t *target, enum target_event event)
551 {
552         target_event_callback_t *callback = target_event_callbacks;
553         target_event_callback_t *next_callback;
554         
555         DEBUG("target event %i", event);
556         
557         while (callback)
558         {
559                 next_callback = callback->next;
560                 callback->callback(target, event, callback->priv);
561                 callback = next_callback;
562         }
563         
564         return ERROR_OK;
565 }
566
567 int target_call_timer_callbacks()
568 {
569         target_timer_callback_t *callback = target_timer_callbacks;
570         target_timer_callback_t *next_callback;
571         struct timeval now;
572
573         gettimeofday(&now, NULL);
574         
575         while (callback)
576         {
577                 next_callback = callback->next;
578                 
579                 if (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
580                         || (now.tv_sec > callback->when.tv_sec))
581                 {
582                         callback->callback(callback->priv);
583                         if (callback->periodic)
584                         {
585                                 int time_ms = callback->time_ms;
586                                 callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
587                                 time_ms -= (time_ms % 1000);
588                                 callback->when.tv_sec = now.tv_sec + time_ms / 1000;
589                                 if (callback->when.tv_usec > 1000000)
590                                 {
591                                         callback->when.tv_usec = callback->when.tv_usec - 1000000;
592                                         callback->when.tv_sec += 1;
593                                 }
594                         }
595                         else
596                                 target_unregister_timer_callback(callback->callback, callback->priv);
597                 }
598                         
599                 callback = next_callback;
600         }
601         
602         return ERROR_OK;
603 }
604
605 int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
606 {
607         working_area_t *c = target->working_areas;
608         working_area_t *new_wa = NULL;
609         
610         /* Reevaluate working area address based on MMU state*/
611         if (target->working_areas == NULL)
612         {
613                 int retval;
614                 int enabled;
615                 retval = target->type->mmu(target, &enabled);
616                 if (retval != ERROR_OK)
617                 {
618                         return retval;
619                 }
620                 if (enabled)
621                 {
622                         target->working_area = target->working_area_virt;
623                 }
624                 else
625                 {
626                         target->working_area = target->working_area_phys;
627                 }
628         }
629         
630         /* only allocate multiples of 4 byte */
631         if (size % 4)
632         {
633                 ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
634                 size = CEIL(size, 4);
635         }
636         
637         /* see if there's already a matching working area */
638         while (c)
639         {
640                 if ((c->free) && (c->size == size))
641                 {
642                         new_wa = c;
643                         break;
644                 }
645                 c = c->next;
646         }
647         
648         /* if not, allocate a new one */
649         if (!new_wa)
650         {
651                 working_area_t **p = &target->working_areas;
652                 u32 first_free = target->working_area;
653                 u32 free_size = target->working_area_size;
654                 
655                 DEBUG("allocating new working area");
656                 
657                 c = target->working_areas;
658                 while (c)
659                 {
660                         first_free += c->size;
661                         free_size -= c->size;
662                         p = &c->next;
663                         c = c->next;
664                 }
665                 
666                 if (free_size < size)
667                 {
668                         WARNING("not enough working area available(requested %d, free %d)", size, free_size);
669                         return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
670                 }
671                 
672                 new_wa = malloc(sizeof(working_area_t));
673                 new_wa->next = NULL;
674                 new_wa->size = size;
675                 new_wa->address = first_free;
676                 
677                 if (target->backup_working_area)
678                 {
679                         new_wa->backup = malloc(new_wa->size);
680                         target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup);
681                 }
682                 else
683                 {
684                         new_wa->backup = NULL;
685                 }
686                 
687                 /* put new entry in list */
688                 *p = new_wa;
689         }
690         
691         /* mark as used, and return the new (reused) area */
692         new_wa->free = 0;
693         *area = new_wa;
694         
695         /* user pointer */
696         new_wa->user = area;
697         
698         return ERROR_OK;
699 }
700
701 int target_free_working_area(struct target_s *target, working_area_t *area)
702 {
703         if (area->free)
704                 return ERROR_OK;
705         
706         if (target->backup_working_area)
707                 target->type->write_memory(target, area->address, 4, area->size / 4, area->backup);
708         
709         area->free = 1;
710         
711         /* mark user pointer invalid */
712         *area->user = NULL;
713         area->user = NULL;
714         
715         return ERROR_OK;
716 }
717
718 int target_free_all_working_areas(struct target_s *target)
719 {
720         working_area_t *c = target->working_areas;
721
722         while (c)
723         {
724                 working_area_t *next = c->next;
725                 target_free_working_area(target, c);
726                 
727                 if (c->backup)
728                         free(c->backup);
729                 
730                 free(c);
731                 
732                 c = next;
733         }
734         
735         target->working_areas = NULL;
736         
737         return ERROR_OK;
738 }
739
740 int target_register_commands(struct command_context_s *cmd_ctx)
741 {
742         register_command(cmd_ctx, NULL, "target", handle_target_command, COMMAND_CONFIG, NULL);
743         register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, NULL);
744         register_command(cmd_ctx, NULL, "daemon_startup", handle_daemon_startup_command, COMMAND_CONFIG, NULL);
745         register_command(cmd_ctx, NULL, "target_script", handle_target_script_command, COMMAND_CONFIG, NULL);
746         register_command(cmd_ctx, NULL, "run_and_halt_time", handle_run_and_halt_time_command, COMMAND_CONFIG, NULL);
747         register_command(cmd_ctx, NULL, "working_area", handle_working_area_command, COMMAND_ANY, "working_area <target#> <address> <size> <'backup'|'nobackup'> [virtual address]");
748         register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "virt2phys <virtual address>");
749
750         return ERROR_OK;
751 }
752
753 /* Single aligned words are guaranteed to use 16 or 32 bit access 
754  * mode respectively, otherwise data is handled as quickly as 
755  * possible
756  */
757 int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
758 {
759         int retval;
760         
761         DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
762         
763         if (((address % 2) == 0) && (size == 2))
764         {
765                 return target->type->write_memory(target, address, 2, 1, buffer);
766         }
767         
768         /* handle unaligned head bytes */
769         if (address % 4)
770         {
771                 int unaligned = 4 - (address % 4);
772                 
773                 if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
774                         return retval;
775                 
776                 buffer += unaligned;
777                 address += unaligned;
778                 size -= unaligned;
779         }
780                 
781         /* handle aligned words */
782         if (size >= 4)
783         {
784                 int aligned = size - (size % 4);
785         
786                 /* use bulk writes above a certain limit. This may have to be changed */
787                 if (aligned > 128)
788                 {
789                         if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
790                                 return retval;
791                 }
792                 else
793                 {
794                         if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
795                                 return retval;
796                 }
797                 
798                 buffer += aligned;
799                 address += aligned;
800                 size -= aligned;
801         }
802         
803         /* handle tail writes of less than 4 bytes */
804         if (size > 0)
805         {
806                 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
807                         return retval;
808         }
809         
810         return ERROR_OK;
811 }
812
813
814 /* Single aligned words are guaranteed to use 16 or 32 bit access 
815  * mode respectively, otherwise data is handled as quickly as 
816  * possible
817  */
818 int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
819 {
820         int retval;
821         
822         DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
823         
824         if (((address % 2) == 0) && (size == 2))
825         {
826                 return target->type->read_memory(target, address, 2, 1, buffer);
827         }
828         
829         /* handle unaligned head bytes */
830         if (address % 4)
831         {
832                 int unaligned = 4 - (address % 4);
833                 
834                 if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
835                         return retval;
836                 
837                 buffer += unaligned;
838                 address += unaligned;
839                 size -= unaligned;
840         }
841                 
842         /* handle aligned words */
843         if (size >= 4)
844         {
845                 int aligned = size - (size % 4);
846         
847                 if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
848                         return retval;
849                 
850                 buffer += aligned;
851                 address += aligned;
852                 size -= aligned;
853         }
854         
855         /* handle tail writes of less than 4 bytes */
856         if (size > 0)
857         {
858                 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
859                         return retval;
860         }
861         
862         return ERROR_OK;
863 }
864
865 int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* crc)
866 {
867         u8 *buffer;
868         int retval;
869         int i;
870         u32 checksum = 0;
871         
872         if ((retval = target->type->checksum_memory(target, address,
873                 size, &checksum)) == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
874         {
875                 buffer = malloc(size);
876                 if (buffer == NULL)
877                 {
878                         ERROR("error allocating buffer for section (%d bytes)", size);
879                         return ERROR_INVALID_ARGUMENTS;
880                 }
881                 retval = target_read_buffer(target, address, size, buffer);
882                 if (retval != ERROR_OK)
883                 {
884                         free(buffer);
885                         return retval;
886                 }
887
888                 /* convert to target endianess */
889                 for (i = 0; i < (size/sizeof(u32)); i++)
890                 {
891                         u32 target_data;
892                         target_data = target_buffer_get_u32(target, &buffer[i*sizeof(u32)]);
893                         target_buffer_set_u32(target, &buffer[i*sizeof(u32)], target_data);
894                 }
895
896                 retval = image_calculate_checksum( buffer, size, &checksum );
897                 free(buffer);
898         }
899         
900         *crc = checksum;
901         
902         return retval;
903 }
904
905 int target_read_u32(struct target_s *target, u32 address, u32 *value)
906 {
907         u8 value_buf[4];
908
909         int retval = target->type->read_memory(target, address, 4, 1, value_buf);
910         
911         if (retval == ERROR_OK)
912         {
913                 *value = target_buffer_get_u32(target, value_buf);
914                 DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
915         }
916         else
917         {
918                 *value = 0x0;
919                 DEBUG("address: 0x%8.8x failed", address);
920         }
921         
922         return retval;
923 }
924
925 int target_read_u16(struct target_s *target, u32 address, u16 *value)
926 {
927         u8 value_buf[2];
928         
929         int retval = target->type->read_memory(target, address, 2, 1, value_buf);
930         
931         if (retval == ERROR_OK)
932         {
933                 *value = target_buffer_get_u16(target, value_buf);
934                 DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
935         }
936         else
937         {
938                 *value = 0x0;
939                 DEBUG("address: 0x%8.8x failed", address);
940         }
941         
942         return retval;
943 }
944
945 int target_read_u8(struct target_s *target, u32 address, u8 *value)
946 {
947         int retval = target->type->read_memory(target, address, 1, 1, value);
948
949         if (retval == ERROR_OK)
950         {
951                 DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
952         }
953         else
954         {
955                 *value = 0x0;
956                 DEBUG("address: 0x%8.8x failed", address);
957         }
958         
959         return retval;
960 }
961
962 int target_write_u32(struct target_s *target, u32 address, u32 value)
963 {
964         int retval;
965         u8 value_buf[4];
966
967         DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
968
969         target_buffer_set_u32(target, value_buf, value);        
970         if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
971         {
972                 DEBUG("failed: %i", retval);
973         }
974         
975         return retval;
976 }
977
978 int target_write_u16(struct target_s *target, u32 address, u16 value)
979 {
980         int retval;
981         u8 value_buf[2];
982         
983         DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
984
985         target_buffer_set_u16(target, value_buf, value);        
986         if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
987         {
988                 DEBUG("failed: %i", retval);
989         }
990         
991         return retval;
992 }
993
994 int target_write_u8(struct target_s *target, u32 address, u8 value)
995 {
996         int retval;
997         
998         DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
999
1000         if ((retval = target->type->read_memory(target, address, 1, 1, &value)) != ERROR_OK)
1001         {
1002                 DEBUG("failed: %i", retval);
1003         }
1004         
1005         return retval;
1006 }
1007
1008 int target_register_user_commands(struct command_context_s *cmd_ctx)
1009 {
1010         register_command(cmd_ctx,  NULL, "reg", handle_reg_command, COMMAND_EXEC, NULL);
1011         register_command(cmd_ctx,  NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1012         register_command(cmd_ctx,  NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1013         register_command(cmd_ctx,  NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1014         register_command(cmd_ctx,  NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1015         register_command(cmd_ctx,  NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1016         register_command(cmd_ctx,  NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init|run_and_halt|run_and_init]");
1017         register_command(cmd_ctx,  NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1018
1019         register_command(cmd_ctx,  NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
1020         register_command(cmd_ctx,  NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
1021         register_command(cmd_ctx,  NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
1022         
1023         register_command(cmd_ctx,  NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value>");
1024         register_command(cmd_ctx,  NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value>");
1025         register_command(cmd_ctx,  NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value>");
1026         
1027         register_command(cmd_ctx,  NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");      
1028         register_command(cmd_ctx,  NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
1029         register_command(cmd_ctx,  NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");    
1030         register_command(cmd_ctx,  NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
1031         
1032         register_command(cmd_ctx,  NULL, "load_image", handle_load_image_command, COMMAND_EXEC, "load_image <file> <address> ['bin'|'ihex'|'elf'|'s19']");
1033         register_command(cmd_ctx,  NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1034         register_command(cmd_ctx,  NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1035         register_command(cmd_ctx,  NULL, "load_binary", handle_load_image_command, COMMAND_EXEC, "[DEPRECATED] load_binary <file> <address>");
1036         register_command(cmd_ctx,  NULL, "dump_binary", handle_dump_image_command, COMMAND_EXEC, "[DEPRECATED] dump_binary <file> <address> <size>");
1037         
1038         target_request_register_commands(cmd_ctx);
1039         trace_register_commands(cmd_ctx);
1040         
1041         return ERROR_OK;
1042 }
1043
1044 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1045 {
1046         target_t *target = targets;
1047         int count = 0;
1048         
1049         if (argc == 1)
1050         {
1051                 int num = strtoul(args[0], NULL, 0);
1052                 
1053                 while (target)
1054                 {
1055                         count++;
1056                         target = target->next;
1057                 }
1058                 
1059                 if (num < count)
1060                         cmd_ctx->current_target = num;
1061                 else
1062                         command_print(cmd_ctx, "%i is out of bounds, only %i targets are configured", num, count);
1063                         
1064                 return ERROR_OK;
1065         }
1066                 
1067         while (target)
1068         {
1069                 command_print(cmd_ctx, "%i: %s (%s), state: %s", count++, target->type->name, target_endianess_strings[target->endianness], target_state_strings[target->state]);
1070                 target = target->next;
1071         }
1072         
1073         return ERROR_OK;
1074 }
1075
1076 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1077 {
1078         int i;
1079         int found = 0;
1080         
1081         if (argc < 3)
1082         {
1083                 ERROR("target command requires at least three arguments: <type> <endianess> <reset_mode>");
1084                 exit(-1);
1085         }
1086         
1087         /* search for the specified target */
1088         if (args[0] && (args[0][0] != 0))
1089         {
1090                 for (i = 0; target_types[i]; i++)
1091                 {
1092                         if (strcmp(args[0], target_types[i]->name) == 0)
1093                         {
1094                                 target_t **last_target_p = &targets;
1095                                 
1096                                 /* register target specific commands */
1097                                 if (target_types[i]->register_commands(cmd_ctx) != ERROR_OK)
1098                                 {
1099                                         ERROR("couldn't register '%s' commands", args[0]);
1100                                         exit(-1);
1101                                 }
1102
1103                                 if (*last_target_p)
1104                                 {
1105                                         while ((*last_target_p)->next)
1106                                                 last_target_p = &((*last_target_p)->next);
1107                                         last_target_p = &((*last_target_p)->next);
1108                                 }
1109
1110                                 *last_target_p = malloc(sizeof(target_t));
1111                                 
1112                                 (*last_target_p)->type = target_types[i];
1113                                 
1114                                 if (strcmp(args[1], "big") == 0)
1115                                         (*last_target_p)->endianness = TARGET_BIG_ENDIAN;
1116                                 else if (strcmp(args[1], "little") == 0)
1117                                         (*last_target_p)->endianness = TARGET_LITTLE_ENDIAN;
1118                                 else
1119                                 {
1120                                         ERROR("endianness must be either 'little' or 'big', not '%s'", args[1]);
1121                                         exit(-1);
1122                                 }
1123                                 
1124                                 /* what to do on a target reset */
1125                                 if (strcmp(args[2], "reset_halt") == 0)
1126                                         (*last_target_p)->reset_mode = RESET_HALT;
1127                                 else if (strcmp(args[2], "reset_run") == 0)
1128                                         (*last_target_p)->reset_mode = RESET_RUN;
1129                                 else if (strcmp(args[2], "reset_init") == 0)
1130                                         (*last_target_p)->reset_mode = RESET_INIT;
1131                                 else if (strcmp(args[2], "run_and_halt") == 0)
1132                                         (*last_target_p)->reset_mode = RESET_RUN_AND_HALT;
1133                                 else if (strcmp(args[2], "run_and_init") == 0)
1134                                         (*last_target_p)->reset_mode = RESET_RUN_AND_INIT;
1135                                 else
1136                                 {
1137                                         ERROR("unknown target startup mode %s", args[2]);
1138                                         exit(-1);
1139                                 }
1140                                 (*last_target_p)->run_and_halt_time = 1000; /* default 1s */
1141                                 
1142                                 (*last_target_p)->reset_script = NULL;
1143                                 (*last_target_p)->post_halt_script = NULL;
1144                                 (*last_target_p)->pre_resume_script = NULL;
1145                                 (*last_target_p)->gdb_program_script = NULL;
1146                                 
1147                                 (*last_target_p)->working_area = 0x0;
1148                                 (*last_target_p)->working_area_size = 0x0;
1149                                 (*last_target_p)->working_areas = NULL;
1150                                 (*last_target_p)->backup_working_area = 0;
1151                                 
1152                                 (*last_target_p)->state = TARGET_UNKNOWN;
1153                                 (*last_target_p)->reg_cache = NULL;
1154                                 (*last_target_p)->breakpoints = NULL;
1155                                 (*last_target_p)->watchpoints = NULL;
1156                                 (*last_target_p)->next = NULL;
1157                                 (*last_target_p)->arch_info = NULL;
1158                                 
1159                                 /* initialize trace information */
1160                                 (*last_target_p)->trace_info = malloc(sizeof(trace_t));
1161                                 (*last_target_p)->trace_info->num_trace_points = 0;
1162                                 (*last_target_p)->trace_info->trace_points_size = 0;
1163                                 (*last_target_p)->trace_info->trace_points = NULL;
1164                                 (*last_target_p)->trace_info->trace_history_size = 0;
1165                                 (*last_target_p)->trace_info->trace_history = NULL;
1166                                 (*last_target_p)->trace_info->trace_history_pos = 0;
1167                                 (*last_target_p)->trace_info->trace_history_overflowed = 0;
1168                                 
1169                                 (*last_target_p)->dbgmsg = NULL;
1170                                 (*last_target_p)->dbg_msg_enabled = 0;
1171                                                                 
1172                                 (*last_target_p)->type->target_command(cmd_ctx, cmd, args, argc, *last_target_p);
1173                                 
1174                                 found = 1;
1175                                 break;
1176                         }
1177                 }
1178         }
1179         
1180         /* no matching target found */
1181         if (!found)
1182         {
1183                 ERROR("target '%s' not found", args[0]);
1184                 exit(-1);
1185         }
1186
1187         return ERROR_OK;
1188 }
1189
1190 /* usage: target_script <target#> <event> <script_file> */
1191 int handle_target_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1192 {
1193         target_t *target = NULL;
1194         
1195         if (argc < 3)
1196         {
1197                 ERROR("incomplete target_script command");
1198                 exit(-1);
1199         }
1200         
1201         target = get_target_by_num(strtoul(args[0], NULL, 0));
1202         
1203         if (!target)
1204         {
1205                 ERROR("target number '%s' not defined", args[0]);
1206                 exit(-1);
1207         }
1208         
1209         if (strcmp(args[1], "reset") == 0)
1210         {
1211                 if (target->reset_script)
1212                         free(target->reset_script);
1213                 target->reset_script = strdup(args[2]);
1214         }
1215         else if (strcmp(args[1], "post_halt") == 0)
1216         {
1217                 if (target->post_halt_script)
1218                         free(target->post_halt_script);
1219                 target->post_halt_script = strdup(args[2]);
1220         }
1221         else if (strcmp(args[1], "pre_resume") == 0)
1222         {
1223                 if (target->pre_resume_script)
1224                         free(target->pre_resume_script);
1225                 target->pre_resume_script = strdup(args[2]);
1226         }
1227         else if (strcmp(args[1], "gdb_program_config") == 0)
1228         {
1229                 if (target->gdb_program_script)
1230                         free(target->gdb_program_script);
1231                 target->gdb_program_script = strdup(args[2]);
1232         }
1233         else
1234         {
1235                 ERROR("unknown event type: '%s", args[1]);
1236                 exit(-1);       
1237         }
1238         
1239         return ERROR_OK;
1240 }
1241
1242 int handle_run_and_halt_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1243 {
1244         target_t *target = NULL;
1245         
1246         if (argc < 2)
1247         {
1248                 ERROR("incomplete run_and_halt_time command");
1249                 exit(-1);
1250         }
1251         
1252         target = get_target_by_num(strtoul(args[0], NULL, 0));
1253         
1254         if (!target)
1255         {
1256                 ERROR("target number '%s' not defined", args[0]);
1257                 exit(-1);
1258         }
1259         
1260         target->run_and_halt_time = strtoul(args[1], NULL, 0);
1261         
1262         return ERROR_OK;
1263 }
1264
1265 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1266 {
1267         target_t *target = NULL;
1268         
1269         if ((argc < 4) || (argc > 5))
1270         {
1271                 return ERROR_COMMAND_SYNTAX_ERROR;
1272         }
1273         
1274         target = get_target_by_num(strtoul(args[0], NULL, 0));
1275         
1276         if (!target)
1277         {
1278                 ERROR("target number '%s' not defined", args[0]);
1279                 exit(-1);
1280         }
1281         target_free_all_working_areas(target);
1282         
1283         target->working_area_phys = target->working_area_virt = strtoul(args[1], NULL, 0);
1284         if (argc == 5)
1285         {
1286                 target->working_area_virt = strtoul(args[4], NULL, 0);
1287         }
1288         target->working_area_size = strtoul(args[2], NULL, 0);
1289         
1290         if (strcmp(args[3], "backup") == 0)
1291         {
1292                 target->backup_working_area = 1;
1293         }
1294         else if (strcmp(args[3], "nobackup") == 0)
1295         {
1296                 target->backup_working_area = 0;
1297         }
1298         else
1299         {
1300                 ERROR("unrecognized <backup|nobackup> argument (%s)", args[3]);
1301                 return ERROR_COMMAND_SYNTAX_ERROR;
1302         }
1303         
1304         return ERROR_OK;
1305 }
1306
1307
1308 /* process target state changes */
1309 int handle_target(void *priv)
1310 {
1311         int retval;
1312         target_t *target = targets;
1313         
1314         while (target)
1315         {
1316                 /* only poll if target isn't already halted */
1317                 if (target->state != TARGET_HALTED)
1318                 {
1319                         if (target_continous_poll)
1320                                 if ((retval = target->type->poll(target)) < 0)
1321                                 {
1322                                         ERROR("couldn't poll target. It's due for a reset.");
1323                                 }
1324                 }
1325         
1326                 target = target->next;
1327         }
1328         
1329         return ERROR_OK;
1330 }
1331
1332 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1333 {
1334         target_t *target;
1335         reg_t *reg = NULL;
1336         int count = 0;
1337         char *value;
1338         
1339         DEBUG("-");
1340         
1341         target = get_current_target(cmd_ctx);
1342         
1343         /* list all available registers for the current target */
1344         if (argc == 0)
1345         {
1346                 reg_cache_t *cache = target->reg_cache;
1347                 
1348                 count = 0;
1349                 while(cache)
1350                 {
1351                         int i;
1352                         for (i = 0; i < cache->num_regs; i++)
1353                         {
1354                                 value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
1355                                 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);
1356                                 free(value);
1357                         }
1358                         cache = cache->next;
1359                 }
1360                 
1361                 return ERROR_OK;
1362         }
1363         
1364         /* access a single register by its ordinal number */
1365         if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1366         {
1367                 int num = strtoul(args[0], NULL, 0);
1368                 reg_cache_t *cache = target->reg_cache;
1369                 
1370                 count = 0;
1371                 while(cache)
1372                 {
1373                         int i;
1374                         for (i = 0; i < cache->num_regs; i++)
1375                         {
1376                                 if (count++ == num)
1377                                 {
1378                                         reg = &cache->reg_list[i];
1379                                         break;
1380                                 }
1381                         }
1382                         if (reg)
1383                                 break;
1384                         cache = cache->next;
1385                 }
1386                 
1387                 if (!reg)
1388                 {
1389                         command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1390                         return ERROR_OK;
1391                 }
1392         } else /* access a single register by its name */
1393         {
1394                 reg = register_get_by_name(target->reg_cache, args[0], 1);
1395                 
1396                 if (!reg)
1397                 {
1398                         command_print(cmd_ctx, "register %s not found in current target", args[0]);
1399                         return ERROR_OK;
1400                 }
1401         }
1402
1403         /* display a register */
1404         if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1405         {
1406                 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1407                         reg->valid = 0;
1408                 
1409                 if (reg->valid == 0)
1410                 {
1411                         reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1412                         if (arch_type == NULL)
1413                         {
1414                                 ERROR("BUG: encountered unregistered arch type");
1415                                 return ERROR_OK;
1416                         }
1417                         arch_type->get(reg);
1418                 }
1419                 value = buf_to_str(reg->value, reg->size, 16);
1420                 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1421                 free(value);
1422                 return ERROR_OK;
1423         }
1424         
1425         /* set register value */
1426         if (argc == 2)
1427         {
1428                 u8 *buf = malloc(CEIL(reg->size, 8));
1429                 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1430
1431                 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1432                 if (arch_type == NULL)
1433                 {
1434                         ERROR("BUG: encountered unregistered arch type");
1435                         return ERROR_OK;
1436                 }
1437                 
1438                 arch_type->set(reg, buf);
1439                 
1440                 value = buf_to_str(reg->value, reg->size, 16);
1441                 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1442                 free(value);
1443                 
1444                 free(buf);
1445                 
1446                 return ERROR_OK;
1447         }
1448         
1449         command_print(cmd_ctx, "usage: reg <#|name> [value]");
1450         
1451         return ERROR_OK;
1452 }
1453
1454 static int wait_state(struct command_context_s *cmd_ctx, char *cmd, enum target_state state, int ms);
1455
1456 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1457 {
1458         target_t *target = get_current_target(cmd_ctx);
1459         char buffer[512];
1460
1461         if (argc == 0)
1462         {
1463                 command_print(cmd_ctx, "target state: %s", target_state_strings[target->type->poll(target)]);
1464                 if (target->state == TARGET_HALTED)
1465                 {
1466                         target->type->arch_state(target, buffer, 512);
1467                         buffer[511] = 0;
1468                         command_print(cmd_ctx, "%s", buffer);
1469                 }
1470         }
1471         else
1472         {
1473                 if (strcmp(args[0], "on") == 0)
1474                 {
1475                         target_continous_poll = 1;
1476                 }
1477                 else if (strcmp(args[0], "off") == 0)
1478                 {
1479                         target_continous_poll = 0;
1480                 }
1481                 else
1482                 {
1483                         command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1484                 }
1485         }
1486         
1487         
1488         return ERROR_OK;
1489 }
1490
1491 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1492 {
1493         int ms = 5000;
1494         
1495         if (argc > 0)
1496         {
1497                 char *end;
1498
1499                 ms = strtoul(args[0], &end, 0) * 1000;
1500                 if (*end)
1501                 {
1502                         command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1503                         return ERROR_OK;
1504                 }
1505         }
1506
1507         return wait_state(cmd_ctx, cmd, TARGET_HALTED, ms); 
1508 }
1509
1510 static void target_process_events(struct command_context_s *cmd_ctx)
1511 {
1512         target_t *target = get_current_target(cmd_ctx);
1513         target->type->poll(target);
1514         target_call_timer_callbacks();
1515 }
1516
1517 static int wait_state(struct command_context_s *cmd_ctx, char *cmd, enum target_state state, int ms)
1518 {
1519         struct timeval timeout, now;
1520         
1521         gettimeofday(&timeout, NULL);
1522         timeval_add_time(&timeout, 0, ms * 1000);
1523         command_print(cmd_ctx, "waiting for target %s...", target_state_strings[state]);
1524         
1525         target_t *target = get_current_target(cmd_ctx);
1526         while (target->type->poll(target))
1527         {
1528                 target_call_timer_callbacks();
1529                 if (target->state == state)
1530                 {
1531                         command_print(cmd_ctx, "target %s", target_state_strings[state]);
1532                         break;
1533                 }
1534                 
1535                 gettimeofday(&now, NULL);
1536                 if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
1537                 {
1538                         command_print(cmd_ctx, "timed out while waiting for target %s", target_state_strings[state]);
1539                         ERROR("timed out while waiting for target %s", target_state_strings[state]);
1540                         break;
1541                 }
1542         }
1543         
1544         return ERROR_OK;
1545 }
1546
1547 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1548 {
1549         int retval;
1550         target_t *target = get_current_target(cmd_ctx);
1551
1552         DEBUG("-");
1553         
1554         command_print(cmd_ctx, "requesting target halt...");
1555
1556         if ((retval = target->type->halt(target)) != ERROR_OK)
1557         {       
1558                 switch (retval)
1559                 {
1560                         case ERROR_TARGET_ALREADY_HALTED:
1561                                 command_print(cmd_ctx, "target already halted");
1562                                 break;
1563                         case ERROR_TARGET_TIMEOUT:
1564                                 command_print(cmd_ctx, "target timed out... shutting down");
1565                                 return retval;
1566                         default:
1567                                 command_print(cmd_ctx, "unknown error... shutting down");
1568                                 return retval;
1569                 }
1570         }
1571         
1572         return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1573 }
1574
1575 /* what to do on daemon startup */
1576 int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1577 {
1578         if (argc == 1)
1579         {
1580                 if (strcmp(args[0], "attach") == 0)
1581                 {
1582                         startup_mode = DAEMON_ATTACH;
1583                         return ERROR_OK;
1584                 }
1585                 else if (strcmp(args[0], "reset") == 0)
1586                 {
1587                         startup_mode = DAEMON_RESET;
1588                         return ERROR_OK;
1589                 }
1590         }
1591         
1592         WARNING("invalid daemon_startup configuration directive: %s", args[0]);
1593         return ERROR_OK;
1594
1595 }
1596                 
1597 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1598 {
1599         target_t *target = get_current_target(cmd_ctx);
1600         int retval;
1601         
1602         command_print(cmd_ctx, "requesting target halt and executing a soft reset");
1603         
1604         if ((retval = target->type->soft_reset_halt(target)) != ERROR_OK)
1605         {       
1606                 switch (retval)
1607                 {
1608                         case ERROR_TARGET_TIMEOUT:
1609                                 command_print(cmd_ctx, "target timed out... shutting down");
1610                                 exit(-1);
1611                         default:
1612                                 command_print(cmd_ctx, "unknown error... shutting down");
1613                                 exit(-1);
1614                 }
1615         }
1616         
1617         return ERROR_OK;
1618 }
1619
1620 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1621 {
1622         target_t *target = get_current_target(cmd_ctx);
1623         enum target_reset_mode reset_mode = target->reset_mode;
1624         enum target_reset_mode save = target->reset_mode;
1625         
1626         DEBUG("-");
1627         
1628         if (argc >= 1)
1629         {
1630                 if (strcmp("run", args[0]) == 0)
1631                         reset_mode = RESET_RUN;
1632                 else if (strcmp("halt", args[0]) == 0)
1633                         reset_mode = RESET_HALT;
1634                 else if (strcmp("init", args[0]) == 0)
1635                         reset_mode = RESET_INIT;
1636                 else if (strcmp("run_and_halt", args[0]) == 0)
1637                 {
1638                         reset_mode = RESET_RUN_AND_HALT;
1639                         if (argc >= 2)
1640                         {
1641                                 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1642                         }
1643                 }
1644                 else if (strcmp("run_and_init", args[0]) == 0)
1645                 {
1646                         reset_mode = RESET_RUN_AND_INIT;
1647                         if (argc >= 2)
1648                         {
1649                                 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1650                         }
1651                 }
1652                 else
1653                 {
1654                         command_print(cmd_ctx, "usage: reset ['run', 'halt', 'init', 'run_and_halt', 'run_and_init]");
1655                         return ERROR_OK;
1656                 }
1657         }
1658         
1659         /* temporarily modify mode of current reset target */
1660         target->reset_mode = reset_mode;
1661
1662         /* reset *all* targets */
1663         target_process_reset(cmd_ctx);
1664         
1665         /* Restore default reset mode for this target */
1666     target->reset_mode = save;
1667         
1668         return ERROR_OK;
1669 }
1670
1671 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1672 {
1673         int retval;
1674         target_t *target = get_current_target(cmd_ctx);
1675         
1676         DEBUG("-");
1677         
1678         if (argc == 0)
1679                 retval = target->type->resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1680         else if (argc == 1)
1681                 retval = target->type->resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1682         else
1683         {
1684                 command_print(cmd_ctx, "usage: resume [address]");
1685                 return ERROR_OK;
1686         }
1687         
1688         if (retval != ERROR_OK)
1689         {       
1690                 switch (retval)
1691                 {
1692                         case ERROR_TARGET_NOT_HALTED:
1693                                 command_print(cmd_ctx, "target not halted");
1694                                 break;
1695                         default:
1696                                 command_print(cmd_ctx, "unknown error... shutting down");
1697                                 exit(-1);
1698                 }
1699         }
1700
1701         target_process_events(cmd_ctx);
1702         
1703         return ERROR_OK;
1704 }
1705
1706 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1707 {
1708         target_t *target = get_current_target(cmd_ctx);
1709         
1710         DEBUG("-");
1711         
1712         if (argc == 0)
1713                 target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1714
1715         if (argc == 1)
1716                 target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1717         
1718         return ERROR_OK;
1719 }
1720
1721 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1722 {
1723         const int line_bytecnt = 32;
1724         int count = 1;
1725         int size = 4;
1726         u32 address = 0;
1727         int line_modulo;
1728         int i;
1729
1730         char output[128];
1731         int output_len;
1732
1733         int retval;
1734
1735         u8 *buffer;
1736         target_t *target = get_current_target(cmd_ctx);
1737
1738         if (argc < 1)
1739                 return ERROR_OK;
1740
1741         if (argc == 2)
1742                 count = strtoul(args[1], NULL, 0);
1743
1744         address = strtoul(args[0], NULL, 0);
1745         
1746
1747         switch (cmd[2])
1748         {
1749                 case 'w':
1750                         size = 4; line_modulo = line_bytecnt / 4;
1751                         break;
1752                 case 'h':
1753                         size = 2; line_modulo = line_bytecnt / 2;
1754                         break;
1755                 case 'b':
1756                         size = 1; line_modulo = line_bytecnt / 1;
1757                         break;
1758                 default:
1759                         return ERROR_OK;
1760         }
1761
1762         buffer = calloc(count, size);
1763         retval  = target->type->read_memory(target, address, size, count, buffer);
1764         if (retval != ERROR_OK)
1765         {
1766                 switch (retval)
1767                 {
1768                         case ERROR_TARGET_UNALIGNED_ACCESS:
1769                                 command_print(cmd_ctx, "error: address not aligned");
1770                                 break;
1771                         case ERROR_TARGET_NOT_HALTED:
1772                                 command_print(cmd_ctx, "error: target must be halted for memory accesses");
1773                                 break;                  
1774                         case ERROR_TARGET_DATA_ABORT:
1775                                 command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
1776                                 break;
1777                         default:
1778                                 command_print(cmd_ctx, "error: unknown error");
1779                                 break;
1780                 }
1781                 return ERROR_OK;
1782         }
1783
1784         output_len = 0;
1785
1786         for (i = 0; i < count; i++)
1787         {
1788                 if (i%line_modulo == 0)
1789                         output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1790                 
1791                 switch (size)
1792                 {
1793                         case 4:
1794                                 output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
1795                                 break;
1796                         case 2:
1797                                 output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
1798                                 break;
1799                         case 1:
1800                                 output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
1801                                 break;
1802                 }
1803
1804                 if ((i%line_modulo == line_modulo-1) || (i == count - 1))
1805                 {
1806                         command_print(cmd_ctx, output);
1807                         output_len = 0;
1808                 }
1809         }
1810
1811         free(buffer);
1812         
1813         return ERROR_OK;
1814 }
1815
1816 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1817 {
1818         u32 address = 0;
1819         u32 value = 0;
1820         int retval;
1821         target_t *target = get_current_target(cmd_ctx);
1822         u8 value_buf[4];
1823
1824         if (argc < 2)
1825                 return ERROR_OK;
1826
1827         address = strtoul(args[0], NULL, 0);
1828         value = strtoul(args[1], NULL, 0);
1829
1830         switch (cmd[2])
1831         {
1832                 case 'w':
1833                         target_buffer_set_u32(target, value_buf, value);
1834                         retval = target->type->write_memory(target, address, 4, 1, value_buf);
1835                         break;
1836                 case 'h':
1837                         target_buffer_set_u16(target, value_buf, value);
1838                         retval = target->type->write_memory(target, address, 2, 1, value_buf);
1839                         break;
1840                 case 'b':
1841                         value_buf[0] = value;
1842                         retval = target->type->write_memory(target, address, 1, 1, value_buf);
1843                         break;
1844                 default:
1845                         return ERROR_OK;
1846         }
1847
1848         switch (retval)
1849         {
1850                 case ERROR_TARGET_UNALIGNED_ACCESS:
1851                         command_print(cmd_ctx, "error: address not aligned");
1852                         break;
1853                 case ERROR_TARGET_DATA_ABORT:
1854                         command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
1855                         break;
1856                 case ERROR_TARGET_NOT_HALTED:
1857                         command_print(cmd_ctx, "error: target must be halted for memory accesses");
1858                         break;
1859                 case ERROR_OK:
1860                         break;
1861                 default:
1862                         command_print(cmd_ctx, "error: unknown error");
1863                         break;
1864         }
1865
1866         return ERROR_OK;
1867
1868 }
1869
1870 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1871 {
1872         u8 *buffer;
1873         u32 buf_cnt;
1874         u32 image_size;
1875         int i;
1876         int retval;
1877
1878         image_t image;  
1879         
1880         duration_t duration;
1881         char *duration_text;
1882         
1883         target_t *target = get_current_target(cmd_ctx);
1884
1885         if (argc < 1)
1886         {
1887                 command_print(cmd_ctx, "usage: load_image <filename> [address] [type]");
1888                 return ERROR_OK;
1889         }
1890         
1891         /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
1892         if (argc >= 2)
1893         {
1894                 image.base_address_set = 1;
1895                 image.base_address = strtoul(args[1], NULL, 0);
1896         }
1897         else
1898         {
1899                 image.base_address_set = 0;
1900         }
1901         
1902         image.start_address_set = 0;
1903
1904         duration_start_measure(&duration);
1905         
1906         if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
1907         {
1908                 command_print(cmd_ctx, "load_image error: %s", image.error_str);
1909                 return ERROR_OK;
1910         }
1911         
1912         image_size = 0x0;
1913         for (i = 0; i < image.num_sections; i++)
1914         {
1915                 buffer = malloc(image.sections[i].size);
1916                 if (buffer == NULL)
1917                 {
1918                         command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
1919                         break;
1920                 }
1921                 
1922                 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
1923                 {
1924                         ERROR("image_read_section failed with error code: %i", retval);
1925                         command_print(cmd_ctx, "image reading failed, download aborted");
1926                         free(buffer);
1927                         image_close(&image);
1928                         return ERROR_OK;
1929                 }
1930                 target_write_buffer(target, image.sections[i].base_address, buf_cnt, buffer);
1931                 image_size += buf_cnt;
1932                 command_print(cmd_ctx, "%u byte written at address 0x%8.8x", buf_cnt, image.sections[i].base_address);
1933                 
1934                 free(buffer);
1935         }
1936
1937         duration_stop_measure(&duration, &duration_text);
1938         command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
1939         free(duration_text);
1940         
1941         image_close(&image);
1942
1943         return ERROR_OK;
1944
1945 }
1946
1947 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1948 {
1949         fileio_t fileio;
1950         
1951         u32 address;
1952         u32 size;
1953         u8 buffer[560];
1954         int retval;
1955         
1956         duration_t duration;
1957         char *duration_text;
1958         
1959         target_t *target = get_current_target(cmd_ctx);
1960
1961         if (argc != 3)
1962         {
1963                 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
1964                 return ERROR_OK;
1965         }
1966
1967         address = strtoul(args[1], NULL, 0);
1968         size = strtoul(args[2], NULL, 0);
1969
1970         if ((address & 3) || (size & 3))
1971         {
1972                 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
1973                 return ERROR_OK;
1974         }
1975         
1976         if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
1977         {
1978                 command_print(cmd_ctx, "dump_image error: %s", fileio.error_str);
1979                 return ERROR_OK;
1980         }
1981         
1982         duration_start_measure(&duration);
1983         
1984         while (size > 0)
1985         {
1986                 u32 size_written;
1987                 u32 this_run_size = (size > 560) ? 560 : size;
1988                 
1989                 retval = target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
1990                 if (retval != ERROR_OK)
1991                 {
1992                         command_print(cmd_ctx, "Reading memory failed %d", retval);
1993                         break;
1994                 }
1995                 
1996                 fileio_write(&fileio, this_run_size, buffer, &size_written);
1997                 
1998                 size -= this_run_size;
1999                 address += this_run_size;
2000         }
2001
2002         fileio_close(&fileio);
2003
2004         duration_stop_measure(&duration, &duration_text);
2005         command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
2006         free(duration_text);
2007         
2008         return ERROR_OK;
2009 }
2010
2011 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2012 {
2013         u8 *buffer;
2014         u32 buf_cnt;
2015         u32 image_size;
2016         int i;
2017         int retval;
2018         u32 checksum = 0;
2019         u32 mem_checksum = 0;
2020
2021         image_t image;  
2022         
2023         duration_t duration;
2024         char *duration_text;
2025         
2026         target_t *target = get_current_target(cmd_ctx);
2027         
2028         if (argc < 1)
2029         {
2030                 command_print(cmd_ctx, "usage: verify_image <file> [offset] [type]");
2031                 return ERROR_OK;
2032         }
2033         
2034         if (!target)
2035         {
2036                 ERROR("no target selected");
2037                 return ERROR_OK;
2038         }
2039         
2040         duration_start_measure(&duration);
2041         
2042         if (argc >= 2)
2043         {
2044                 image.base_address_set = 1;
2045                 image.base_address = strtoul(args[1], NULL, 0);
2046         }
2047         else
2048         {
2049                 image.base_address_set = 0;
2050                 image.base_address = 0x0;
2051         }
2052
2053         image.start_address_set = 0;
2054
2055         if (image_open(&image, args[0], (argc == 3) ? args[2] : NULL) != ERROR_OK)
2056         {
2057                 command_print(cmd_ctx, "verify_image error: %s", image.error_str);
2058                 return ERROR_OK;
2059         }
2060         
2061         image_size = 0x0;
2062         for (i = 0; i < image.num_sections; i++)
2063         {
2064                 buffer = malloc(image.sections[i].size);
2065                 if (buffer == NULL)
2066                 {
2067                         command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2068                         break;
2069                 }
2070                 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2071                 {
2072                         ERROR("image_read_section failed with error code: %i", retval);
2073                         command_print(cmd_ctx, "image reading failed, verify aborted");
2074                         free(buffer);
2075                         image_close(&image);
2076                         return ERROR_OK;
2077                 }
2078                 
2079                 /* calculate checksum of image */
2080                 image_calculate_checksum( buffer, buf_cnt, &checksum );
2081                 
2082                 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2083                 
2084                 if( retval != ERROR_OK )
2085                 {
2086                         command_print(cmd_ctx, "could not calculate checksum, verify aborted");
2087                         free(buffer);
2088                         image_close(&image);
2089                         return ERROR_OK;
2090                 }
2091                 
2092                 if( checksum != mem_checksum )
2093                 {
2094                         /* failed crc checksum, fall back to a binary compare */
2095                         u8 *data;
2096                         
2097                         command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2098                         
2099                         data = (u8*)malloc(buf_cnt);
2100                         
2101                         /* Can we use 32bit word accesses? */
2102                         int size = 1;
2103                         int count = buf_cnt;
2104                         if ((count % 4) == 0)
2105                         {
2106                                 size *= 4;
2107                                 count /= 4;
2108                         }
2109                         retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
2110         
2111                         if (retval == ERROR_OK)
2112                         {
2113                                 int t;
2114                                 for (t = 0; t < buf_cnt; t++)
2115                                 {
2116                                         if (data[t] != buffer[t])
2117                                         {
2118                                                 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]);
2119                                                 free(data);
2120                                                 free(buffer);
2121                                                 image_close(&image);
2122                                                 return ERROR_OK;
2123                                         }
2124                                 }
2125                         }
2126                         
2127                         free(data);
2128                 }
2129                 
2130                 free(buffer);
2131                 image_size += buf_cnt;
2132         }
2133         
2134         duration_stop_measure(&duration, &duration_text);
2135         command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
2136         free(duration_text);
2137         
2138         image_close(&image);
2139         
2140         return ERROR_OK;
2141 }
2142
2143 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2144 {
2145         int retval;
2146         target_t *target = get_current_target(cmd_ctx);
2147
2148         if (argc == 0)
2149         {
2150                 breakpoint_t *breakpoint = target->breakpoints;
2151
2152                 while (breakpoint)
2153                 {
2154                         if (breakpoint->type == BKPT_SOFT)
2155                         {
2156                                 char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
2157                                 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
2158                                 free(buf);
2159                         }
2160                         else
2161                         {
2162                                 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
2163                         }
2164                         breakpoint = breakpoint->next;
2165                 }
2166         }
2167         else if (argc >= 2)
2168         {
2169                 int hw = BKPT_SOFT;
2170                 u32 length = 0;
2171
2172                 length = strtoul(args[1], NULL, 0);
2173                 
2174                 if (argc >= 3)
2175                         if (strcmp(args[2], "hw") == 0)
2176                                 hw = BKPT_HARD;
2177
2178                 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
2179                 {
2180                         switch (retval)
2181                         {
2182                                 case ERROR_TARGET_NOT_HALTED:
2183                                         command_print(cmd_ctx, "target must be halted to set breakpoints");
2184                                         break;
2185                                 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
2186                                         command_print(cmd_ctx, "no more breakpoints available");
2187                                         break;
2188                                 default:
2189                                         command_print(cmd_ctx, "unknown error, breakpoint not set");
2190                                         break;
2191                         }
2192                 }
2193                 else
2194                 {
2195                         command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
2196                 }
2197         }
2198         else
2199         {
2200                 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2201         }
2202
2203         return ERROR_OK;
2204 }
2205
2206 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2207 {
2208         target_t *target = get_current_target(cmd_ctx);
2209
2210         if (argc > 0)
2211                 breakpoint_remove(target, strtoul(args[0], NULL, 0));
2212
2213         return ERROR_OK;
2214 }
2215
2216 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2217 {
2218         target_t *target = get_current_target(cmd_ctx);
2219         int retval;
2220
2221         if (argc == 0)
2222         {
2223                 watchpoint_t *watchpoint = target->watchpoints;
2224
2225                 while (watchpoint)
2226                 {
2227                         command_print(cmd_ctx, "address: 0x%8.8x, mask: 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);
2228                         watchpoint = watchpoint->next;
2229                 }
2230         } 
2231         else if (argc >= 2)
2232         {
2233                 enum watchpoint_rw type = WPT_ACCESS;
2234                 u32 data_value = 0x0;
2235                 u32 data_mask = 0xffffffff;
2236                 
2237                 if (argc >= 3)
2238                 {
2239                         switch(args[2][0])
2240                         {
2241                                 case 'r':
2242                                         type = WPT_READ;
2243                                         break;
2244                                 case 'w':
2245                                         type = WPT_WRITE;
2246                                         break;
2247                                 case 'a':
2248                                         type = WPT_ACCESS;
2249                                         break;
2250                                 default:
2251                                         command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2252                                         return ERROR_OK;
2253                         }
2254                 }
2255                 if (argc >= 4)
2256                 {
2257                         data_value = strtoul(args[3], NULL, 0);
2258                 }
2259                 if (argc >= 5)
2260                 {
2261                         data_mask = strtoul(args[4], NULL, 0);
2262                 }
2263                 
2264                 if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
2265                                 strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
2266                 {
2267                         switch (retval)
2268                         {
2269                                 case ERROR_TARGET_NOT_HALTED:
2270                                         command_print(cmd_ctx, "target must be halted to set watchpoints");
2271                                         break;
2272                                 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
2273                                         command_print(cmd_ctx, "no more watchpoints available");
2274                                         break;
2275                                 default:
2276                                         command_print(cmd_ctx, "unknown error, watchpoint not set");
2277                                         break;
2278                         }       
2279                 }
2280         }
2281         else
2282         {
2283                 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2284         }
2285                 
2286         return ERROR_OK;
2287 }
2288
2289 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2290 {
2291         target_t *target = get_current_target(cmd_ctx);
2292
2293         if (argc > 0)
2294                 watchpoint_remove(target, strtoul(args[0], NULL, 0));
2295         
2296         return ERROR_OK;
2297 }
2298
2299 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
2300 {
2301         int retval;
2302         target_t *target = get_current_target(cmd_ctx);
2303         u32 va;
2304         u32 pa;
2305
2306         if (argc != 1)
2307         {
2308                 return ERROR_COMMAND_SYNTAX_ERROR;
2309         }
2310         va = strtoul(args[0], NULL, 0);
2311
2312         retval = target->type->virt2phys(target, va, &pa);
2313         if (retval == ERROR_OK)
2314         {
2315                 command_print(cmd_ctx, "Physical address 0x%08x", pa);
2316         }
2317         else
2318         {
2319                 /* lower levels will have logged a detailed error which is 
2320                  * forwarded to telnet/GDB session.  
2321                  */
2322         }
2323         return retval;
2324 }