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