- Added support for ARM926EJ-S based cores
[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
27 #include "log.h"
28 #include "configuration.h"
29 #include "binarybuffer.h"
30 #include "jtag.h"
31
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <errno.h>
39
40 #include <sys/time.h>
41 #include <time.h>
42
43 #include <time_support.h>
44
45 int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv);
46
47 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
50
51 int handle_target_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52 int handle_run_and_halt_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
54
55 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
56 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
57 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
58 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
59 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
60 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
61 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
62 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
63 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
64 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
65 int handle_load_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
66 int handle_dump_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
67 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
70 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71
72 /* targets
73  */
74 extern target_type_t arm7tdmi_target;
75 extern target_type_t arm720t_target;
76 extern target_type_t arm9tdmi_target;
77 extern target_type_t arm920t_target;
78 extern target_type_t arm966e_target;
79 extern target_type_t arm926ejs_target;
80
81 target_type_t *target_types[] =
82 {
83         &arm7tdmi_target,
84         &arm9tdmi_target,
85         &arm920t_target,
86         &arm720t_target,
87         &arm966e_target,
88         &arm926ejs_target,
89         NULL,
90 };
91
92 target_t *targets = NULL;
93 target_event_callback_t *target_event_callbacks = NULL;
94 target_timer_callback_t *target_timer_callbacks = NULL;
95
96 char *target_state_strings[] =
97 {
98         "unknown",
99         "running",
100         "halted",
101         "reset",
102         "debug_running",
103 };
104
105 char *target_debug_reason_strings[] =
106 {
107         "debug request", "breakpoint", "watchpoint",
108         "watchpoint and breakpoint", "single step",
109         "target not halted"
110 };
111
112 char *target_endianess_strings[] =
113 {
114         "big endian",
115         "little endian",
116 };
117
118 enum daemon_startup_mode startup_mode = DAEMON_ATTACH;
119
120 static int target_continous_poll = 1;
121
122 /* read a u32 from a buffer in target memory endianness */
123 u32 target_buffer_get_u32(target_t *target, u8 *buffer)
124 {
125         if (target->endianness == TARGET_LITTLE_ENDIAN)
126                 return le_to_h_u32(buffer);
127         else
128                 return be_to_h_u32(buffer);
129 }
130
131 /* read a u16 from a buffer in target memory endianness */
132 u16 target_buffer_get_u16(target_t *target, u8 *buffer)
133 {
134         if (target->endianness == TARGET_LITTLE_ENDIAN)
135                 return le_to_h_u16(buffer);
136         else
137                 return be_to_h_u16(buffer);
138 }
139
140 /* write a u32 to a buffer in target memory endianness */
141 void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
142 {
143         if (target->endianness == TARGET_LITTLE_ENDIAN)
144                 h_u32_to_le(buffer, value);
145         else
146                 h_u32_to_be(buffer, value);
147 }
148
149 /* write a u16 to a buffer in target memory endianness */
150 void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
151 {
152         if (target->endianness == TARGET_LITTLE_ENDIAN)
153                 h_u16_to_le(buffer, value);
154         else
155                 h_u16_to_be(buffer, value);
156 }
157
158 /* returns a pointer to the n-th configured target */
159 target_t* get_target_by_num(int num)
160 {
161         target_t *target = targets;
162         int i = 0;
163
164         while (target)
165         {
166                 if (num == i)
167                         return target;
168                 target = target->next;
169                 i++;
170         }
171
172         return NULL;
173 }
174
175 int get_num_by_target(target_t *query_target)
176 {
177         target_t *target = targets;
178         int i = 0;      
179         
180         while (target)
181         {
182                 if (target == query_target)
183                         return i;
184                 target = target->next;
185                 i++;
186         }
187         
188         return -1;
189 }
190
191 target_t* get_current_target(command_context_t *cmd_ctx)
192 {
193         target_t *target = get_target_by_num(cmd_ctx->current_target);
194         
195         if (target == NULL)
196         {
197                 ERROR("BUG: current_target out of bounds");
198                 exit(-1);
199         }
200         
201         return target;
202 }
203
204 /* Process target initialization, when target entered debug out of reset
205  * the handler is unregistered at the end of this function, so it's only called once
206  */
207 int target_init_handler(struct target_s *target, enum target_event event, void *priv)
208 {
209         FILE *script;
210         struct command_context_s *cmd_ctx = priv;
211         
212         if ((event == TARGET_EVENT_HALTED) && (target->reset_script))
213         {
214                 target_unregister_event_callback(target_init_handler, priv);
215
216                 script = fopen(target->reset_script, "r");
217                 if (!script)
218                 {
219                         ERROR("couldn't open script file %s", target->reset_script);
220                                 return ERROR_OK;
221                 }
222
223                 INFO("executing reset script '%s'", target->reset_script);
224                 command_run_file(cmd_ctx, script, COMMAND_EXEC);
225                 fclose(script);
226
227                 jtag_execute_queue();
228         }
229         
230         return ERROR_OK;
231 }
232
233 int target_run_and_halt_handler(void *priv)
234 {
235         target_t *target = priv;
236         
237         target->type->halt(target);
238         
239         return ERROR_OK;
240 }
241
242 int target_process_reset(struct command_context_s *cmd_ctx)
243 {
244         int retval = ERROR_OK;
245         target_t *target;
246          
247         target = targets;
248         while (target)
249         {
250                 target->type->assert_reset(target);
251                 target = target->next;
252         }
253         jtag_execute_queue();
254         
255         /* request target halt if necessary, and schedule further action */
256         target = targets;
257         while (target)
258         {
259                 switch (target->reset_mode)
260                 {
261                         case RESET_RUN:
262                                 /* nothing to do if target just wants to be run */
263                                 break;
264                         case RESET_RUN_AND_HALT:
265                                 /* schedule halt */
266                                 target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
267                                 break;
268                         case RESET_RUN_AND_INIT:
269                                 /* schedule halt */
270                                 target_register_timer_callback(target_run_and_halt_handler, target->run_and_halt_time, 0, target);
271                                 target_register_event_callback(target_init_handler, cmd_ctx);
272                                 break;
273                         case RESET_HALT:
274                                 target->type->halt(target);
275                                 break;
276                         case RESET_INIT:
277                                 target->type->halt(target);
278                                 target_register_event_callback(target_init_handler, cmd_ctx);
279                                 break;
280                         default:
281                                 ERROR("BUG: unknown target->reset_mode");
282                 }
283                 target = target->next;
284         }
285         
286         target = targets;
287         while (target)
288         {
289                 target->type->deassert_reset(target);
290                 target = target->next;
291         }
292         jtag_execute_queue();
293         
294         return retval;
295 }       
296
297 int target_init(struct command_context_s *cmd_ctx)
298 {
299         target_t *target = targets;
300         
301         while (target)
302         {
303                 if (target->type->init_target(cmd_ctx, target) != ERROR_OK)
304                 {
305                         ERROR("target '%s' init failed", target->type->name);
306                         exit(-1);
307                 }
308                 target = target->next;
309         }
310         
311         if (targets)
312         {
313                 target_register_user_commands(cmd_ctx);
314                 target_register_timer_callback(handle_target, 100, 1, NULL);
315         }
316                 
317         if (startup_mode == DAEMON_RESET)
318                 target_process_reset(cmd_ctx);
319         
320         return ERROR_OK;
321 }
322
323 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
324 {
325         target_event_callback_t **callbacks_p = &target_event_callbacks;
326         
327         if (callback == NULL)
328         {
329                 return ERROR_INVALID_ARGUMENTS;
330         }
331         
332         if (*callbacks_p)
333         {
334                 while ((*callbacks_p)->next)
335                         callbacks_p = &((*callbacks_p)->next);
336                 callbacks_p = &((*callbacks_p)->next);
337         }
338         
339         (*callbacks_p) = malloc(sizeof(target_event_callback_t));
340         (*callbacks_p)->callback = callback;
341         (*callbacks_p)->priv = priv;
342         (*callbacks_p)->next = NULL;
343         
344         return ERROR_OK;
345 }
346
347 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
348 {
349         target_timer_callback_t **callbacks_p = &target_timer_callbacks;
350         struct timeval now;
351         
352         if (callback == NULL)
353         {
354                 return ERROR_INVALID_ARGUMENTS;
355         }
356         
357         if (*callbacks_p)
358         {
359                 while ((*callbacks_p)->next)
360                         callbacks_p = &((*callbacks_p)->next);
361                 callbacks_p = &((*callbacks_p)->next);
362         }
363         
364         (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
365         (*callbacks_p)->callback = callback;
366         (*callbacks_p)->periodic = periodic;
367         (*callbacks_p)->time_ms = time_ms;
368         
369         gettimeofday(&now, NULL);
370         (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
371         time_ms -= (time_ms % 1000);
372         (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
373         if ((*callbacks_p)->when.tv_usec > 1000000)
374         {
375                 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
376                 (*callbacks_p)->when.tv_sec += 1;
377         }
378         
379         (*callbacks_p)->priv = priv;
380         (*callbacks_p)->next = NULL;
381         
382         return ERROR_OK;
383 }
384
385 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
386 {
387         target_event_callback_t **p = &target_event_callbacks;
388         target_event_callback_t *c = target_event_callbacks;
389         
390         if (callback == NULL)
391         {
392                 return ERROR_INVALID_ARGUMENTS;
393         }
394                 
395         while (c)
396         {
397                 target_event_callback_t *next = c->next;
398                 if ((c->callback == callback) && (c->priv == priv))
399                 {
400                         *p = next;
401                         free(c);
402                         return ERROR_OK;
403                 }
404                 else
405                         p = &(c->next);
406                 c = next;
407         }
408         
409         return ERROR_OK;
410 }
411
412 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
413 {
414         target_timer_callback_t **p = &target_timer_callbacks;
415         target_timer_callback_t *c = target_timer_callbacks;
416         
417         if (callback == NULL)
418         {
419                 return ERROR_INVALID_ARGUMENTS;
420         }
421                 
422         while (c)
423         {
424                 target_timer_callback_t *next = c->next;
425                 if ((c->callback == callback) && (c->priv == priv))
426                 {
427                         *p = next;
428                         free(c);
429                         return ERROR_OK;
430                 }
431                 else
432                         p = &(c->next);
433                 c = next;
434         }
435         
436         return ERROR_OK;
437 }
438
439 int target_call_event_callbacks(target_t *target, enum target_event event)
440 {
441         target_event_callback_t *callback = target_event_callbacks;
442         target_event_callback_t *next_callback;
443         
444         DEBUG("target event %i", event);
445         
446         while (callback)
447         {
448                 next_callback = callback->next;
449                 callback->callback(target, event, callback->priv);
450                 callback = next_callback;
451         }
452         
453         return ERROR_OK;
454 }
455
456 int target_call_timer_callbacks()
457 {
458         target_timer_callback_t *callback = target_timer_callbacks;
459         target_timer_callback_t *next_callback;
460         struct timeval now;
461
462         gettimeofday(&now, NULL);
463         
464         while (callback)
465         {
466                 next_callback = callback->next;
467                 
468                 if (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
469                         || (now.tv_sec > callback->when.tv_sec))
470                 {
471                         callback->callback(callback->priv);
472                         if (callback->periodic)
473                         {
474                                 int time_ms = callback->time_ms;
475                                 callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
476                                 time_ms -= (time_ms % 1000);
477                                 callback->when.tv_sec = now.tv_sec + time_ms / 1000;
478                                 if (callback->when.tv_usec > 1000000)
479                                 {
480                                         callback->when.tv_usec = callback->when.tv_usec - 1000000;
481                                         callback->when.tv_sec += 1;
482                                 }
483                         }
484                         else
485                                 target_unregister_timer_callback(callback->callback, callback->priv);
486                 }
487                         
488                 callback = next_callback;
489         }
490         
491         return ERROR_OK;
492 }
493
494 int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
495 {
496         working_area_t *c = target->working_areas;
497         working_area_t *new_wa = NULL;
498         
499         /* only allocate multiples of 4 byte */
500         if (size % 4)
501         {
502                 ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
503                 size = CEIL(size, 4);
504         }
505         
506         /* see if there's already a matching working area */
507         while (c)
508         {
509                 if ((c->free) && (c->size == size))
510                 {
511                         new_wa = c;
512                         break;
513                 }
514                 c = c->next;
515         }
516         
517         /* if not, allocate a new one */
518         if (!new_wa)
519         {
520                 working_area_t **p = &target->working_areas;
521                 u32 first_free = target->working_area;
522                 u32 free_size = target->working_area_size;
523                 
524                 DEBUG("allocating new working area");
525                 
526                 c = target->working_areas;
527                 while (c)
528                 {
529                         first_free += c->size;
530                         free_size -= c->size;
531                         p = &c->next;
532                         c = c->next;
533                 }
534                 
535                 if (free_size < size)
536                 {
537                         WARNING("not enough working area available");
538                         return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
539                 }
540                 
541                 new_wa = malloc(sizeof(working_area_t));
542                 new_wa->next = NULL;
543                 new_wa->size = size;
544                 new_wa->address = first_free;
545                 
546                 if (target->backup_working_area)
547                 {
548                         new_wa->backup = malloc(new_wa->size);
549                         target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup);
550                 }
551                 else
552                 {
553                         new_wa->backup = NULL;
554                 }
555                 
556                 /* put new entry in list */
557                 *p = new_wa;
558         }
559         
560         /* mark as used, and return the new (reused) area */
561         new_wa->free = 0;
562         *area = new_wa;
563         
564         /* user pointer */
565         new_wa->user = area;
566         
567         return ERROR_OK;
568 }
569
570 int target_free_working_area(struct target_s *target, working_area_t *area)
571 {
572         if (area->free)
573                 return ERROR_OK;
574         
575         if (target->backup_working_area)
576                 target->type->write_memory(target, area->address, 4, area->size / 4, area->backup);
577         
578         area->free = 1;
579         
580         /* mark user pointer invalid */
581         *area->user = NULL;
582         area->user = NULL;
583         
584         return ERROR_OK;
585 }
586
587 int target_free_all_working_areas(struct target_s *target)
588 {
589         working_area_t *c = target->working_areas;
590
591         while (c)
592         {
593                 working_area_t *next = c->next;
594                 target_free_working_area(target, c);
595                 
596                 if (c->backup)
597                         free(c->backup);
598                 
599                 free(c);
600                 
601                 c = next;
602         }
603         
604         target->working_areas = NULL;
605         
606         return ERROR_OK;
607 }
608
609 int target_register_commands(struct command_context_s *cmd_ctx)
610 {
611         register_command(cmd_ctx, NULL, "target", handle_target_command, COMMAND_CONFIG, NULL);
612         register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, NULL);
613         register_command(cmd_ctx, NULL, "daemon_startup", handle_daemon_startup_command, COMMAND_CONFIG, NULL);
614         register_command(cmd_ctx, NULL, "target_script", handle_target_script_command, COMMAND_CONFIG, NULL);
615         register_command(cmd_ctx, NULL, "run_and_halt_time", handle_run_and_halt_time_command, COMMAND_CONFIG, NULL);
616         register_command(cmd_ctx, NULL, "working_area", handle_working_area_command, COMMAND_CONFIG, NULL);
617
618         return ERROR_OK;
619 }
620
621 int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
622 {
623         int retval;
624         
625         DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
626         
627         /* handle writes of less than 4 byte */
628         if (size < 4)
629         {
630                 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
631                         return retval;
632         }
633         
634         /* handle unaligned head bytes */
635         if (address % 4)
636         {
637                 int unaligned = 4 - (address % 4);
638                 
639                 if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
640                         return retval;
641                 
642                 buffer += unaligned;
643                 address += unaligned;
644                 size -= unaligned;
645         }
646                 
647         /* handle aligned words */
648         if (size >= 4)
649         {
650                 int aligned = size - (size % 4);
651         
652                 /* use bulk writes above a certain limit. This may have to be changed */
653                 if (aligned > 128)
654                 {
655                         if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
656                                 return retval;
657                 }
658                 else
659                 {
660                         if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
661                                 return retval;
662                 }
663                 
664                 buffer += aligned;
665                 address += aligned;
666                 size -= aligned;
667         }
668         
669         /* handle tail writes of less than 4 bytes */
670         if (size > 0)
671         {
672                 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
673                         return retval;
674         }
675         
676         return ERROR_OK;
677 }
678
679 int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
680 {
681         int retval;
682         
683         DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
684         
685         /* handle reads of less than 4 byte */
686         if (size < 4)
687         {
688                 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
689                         return retval;
690         }
691         
692         /* handle unaligned head bytes */
693         if (address % 4)
694         {
695                 int unaligned = 4 - (address % 4);
696                 
697                 if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
698                         return retval;
699                 
700                 buffer += unaligned;
701                 address += unaligned;
702                 size -= unaligned;
703         }
704                 
705         /* handle aligned words */
706         if (size >= 4)
707         {
708                 int aligned = size - (size % 4);
709         
710                 if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
711                         return retval;
712                 
713                 buffer += aligned;
714                 address += aligned;
715                 size -= aligned;
716         }
717         
718         /* handle tail writes of less than 4 bytes */
719         if (size > 0)
720         {
721                 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
722                         return retval;
723         }
724         
725         return ERROR_OK;
726 }
727
728 void target_read_u32(struct target_s *target, u32 address, u32 *value)
729 {
730         u8 value_buf[4];
731         
732         target->type->read_memory(target, address, 4, 1, value_buf);
733         
734         *value = target_buffer_get_u32(target, value_buf);
735 }
736
737 void target_read_u16(struct target_s *target, u32 address, u16 *value)
738 {
739         u8 value_buf[2];
740         
741         target->type->read_memory(target, address, 2, 1, value_buf);
742         
743         *value = target_buffer_get_u16(target, value_buf);
744 }
745
746 void target_read_u8(struct target_s *target, u32 address, u8 *value)
747 {
748         target->type->read_memory(target, address, 1, 1, value);
749 }
750
751 void target_write_u32(struct target_s *target, u32 address, u32 value)
752 {
753         u8 value_buf[4];
754
755         target_buffer_set_u32(target, value_buf, value);        
756         target->type->write_memory(target, address, 4, 1, value_buf);
757 }
758
759 void target_write_u16(struct target_s *target, u32 address, u16 value)
760 {
761         u8 value_buf[2];
762         
763         target_buffer_set_u16(target, value_buf, value);        
764         target->type->write_memory(target, address, 2, 1, value_buf);
765 }
766
767 void target_write_u8(struct target_s *target, u32 address, u8 value)
768 {
769         target->type->read_memory(target, address, 1, 1, &value);
770 }
771
772 int target_register_user_commands(struct command_context_s *cmd_ctx)
773 {
774         register_command(cmd_ctx,  NULL, "reg", handle_reg_command, COMMAND_EXEC, NULL);
775         register_command(cmd_ctx,  NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
776         register_command(cmd_ctx,  NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt");
777         register_command(cmd_ctx,  NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
778         register_command(cmd_ctx,  NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
779         register_command(cmd_ctx,  NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction");
780         register_command(cmd_ctx,  NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init|run_and_halt|run_and_init]");
781         register_command(cmd_ctx,  NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
782
783         register_command(cmd_ctx,  NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
784         register_command(cmd_ctx,  NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
785         register_command(cmd_ctx,  NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
786         
787         register_command(cmd_ctx,  NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value>");
788         register_command(cmd_ctx,  NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value>");
789         register_command(cmd_ctx,  NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value>");
790         
791         register_command(cmd_ctx,  NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");      
792         register_command(cmd_ctx,  NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
793         register_command(cmd_ctx,  NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");    
794         register_command(cmd_ctx,  NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
795         
796         register_command(cmd_ctx,  NULL, "load_binary", handle_load_binary_command, COMMAND_EXEC, "load binary <file> <address>");
797         register_command(cmd_ctx,  NULL, "dump_binary", handle_dump_binary_command, COMMAND_EXEC, "dump binary <file> <address> <size>");
798         
799         return ERROR_OK;
800 }
801
802 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
803 {
804         target_t *target = targets;
805         int count = 0;
806         
807         if (argc == 1)
808         {
809                 int num = strtoul(args[0], NULL, 0);
810                 
811                 while (target)
812                 {
813                         count++;
814                         target = target->next;
815                 }
816                 
817                 if (num < count)
818                         cmd_ctx->current_target = num;
819                 else
820                         command_print(cmd_ctx, "%i is out of bounds, only %i targets are configured", num, count);
821                         
822                 return ERROR_OK;
823         }
824                 
825         while (target)
826         {
827                 command_print(cmd_ctx, "%i: %s (%s), state: %s", count++, target->type->name, target_endianess_strings[target->endianness], target_state_strings[target->state]);
828                 target = target->next;
829         }
830         
831         return ERROR_OK;
832 }
833
834 int handle_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
835 {
836         int i;
837         int found = 0;
838         
839         if (argc < 3)
840         {
841                 ERROR("target command requires at least three arguments: <type> <endianess> <reset_mode>");
842                 exit(-1);
843         }
844         
845         /* search for the specified target */
846         if (args[0] && (args[0][0] != 0))
847         {
848                 for (i = 0; target_types[i]; i++)
849                 {
850                         if (strcmp(args[0], target_types[i]->name) == 0)
851                         {
852                                 target_t **last_target_p = &targets;
853                                 
854                                 /* register target specific commands */
855                                 if (target_types[i]->register_commands(cmd_ctx) != ERROR_OK)
856                                 {
857                                         ERROR("couldn't register '%s' commands", args[0]);
858                                         exit(-1);
859                                 }
860
861                                 if (*last_target_p)
862                                 {
863                                         while ((*last_target_p)->next)
864                                                 last_target_p = &((*last_target_p)->next);
865                                         last_target_p = &((*last_target_p)->next);
866                                 }
867
868                                 *last_target_p = malloc(sizeof(target_t));
869                                 
870                                 (*last_target_p)->type = target_types[i];
871                                 
872                                 if (strcmp(args[1], "big") == 0)
873                                         (*last_target_p)->endianness = TARGET_BIG_ENDIAN;
874                                 else if (strcmp(args[1], "little") == 0)
875                                         (*last_target_p)->endianness = TARGET_LITTLE_ENDIAN;
876                                 else
877                                 {
878                                         ERROR("endianness must be either 'little' or 'big', not '%s'", args[1]);
879                                         exit(-1);
880                                 }
881                                 
882                                 /* what to do on a target reset */
883                                 if (strcmp(args[2], "reset_halt") == 0)
884                                         (*last_target_p)->reset_mode = RESET_HALT;
885                                 else if (strcmp(args[2], "reset_run") == 0)
886                                         (*last_target_p)->reset_mode = RESET_RUN;
887                                 else if (strcmp(args[2], "reset_init") == 0)
888                                         (*last_target_p)->reset_mode = RESET_INIT;
889                                 else if (strcmp(args[2], "run_and_halt") == 0)
890                                         (*last_target_p)->reset_mode = RESET_RUN_AND_HALT;
891                                 else if (strcmp(args[2], "run_and_init") == 0)
892                                         (*last_target_p)->reset_mode = RESET_RUN_AND_INIT;
893                                 else
894                                 {
895                                         ERROR("unknown target startup mode %s", args[2]);
896                                         exit(-1);
897                                 }
898                                 (*last_target_p)->run_and_halt_time = 1000; /* default 1s */
899                                 
900                                 (*last_target_p)->reset_script = NULL;
901                                 (*last_target_p)->post_halt_script = NULL;
902                                 (*last_target_p)->pre_resume_script = NULL;
903                                 
904                                 (*last_target_p)->working_area = 0x0;
905                                 (*last_target_p)->working_area_size = 0x0;
906                                 (*last_target_p)->working_areas = NULL;
907                                 (*last_target_p)->backup_working_area = 0;
908                                 
909                                 (*last_target_p)->state = TARGET_UNKNOWN;
910                                 (*last_target_p)->reg_cache = NULL;
911                                 (*last_target_p)->breakpoints = NULL;
912                                 (*last_target_p)->watchpoints = NULL;
913                                 (*last_target_p)->next = NULL;
914                                 (*last_target_p)->arch_info = NULL;
915                                 
916                                 (*last_target_p)->type->target_command(cmd_ctx, cmd, args, argc, *last_target_p);
917                                 
918                                 found = 1;
919                                 break;
920                         }
921                 }
922         }
923         
924         /* no matching target found */
925         if (!found)
926         {
927                 ERROR("target '%s' not found", args[0]);
928                 exit(-1);
929         }
930
931         return ERROR_OK;
932 }
933
934 /* usage: target_script <target#> <event> <script_file> */
935 int handle_target_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
936 {
937         target_t *target = NULL;
938         
939         if (argc < 3)
940         {
941                 ERROR("incomplete target_script command");
942                 exit(-1);
943         }
944         
945         target = get_target_by_num(strtoul(args[0], NULL, 0));
946         
947         if (!target)
948         {
949                 ERROR("target number '%s' not defined", args[0]);
950                 exit(-1);
951         }
952         
953         if (strcmp(args[1], "reset") == 0)
954         {
955                 if (target->reset_script)
956                         free(target->reset_script);
957                 target->reset_script = strdup(args[2]);
958         }
959         else if (strcmp(args[1], "post_halt") == 0)
960         {
961                 if (target->post_halt_script)
962                         free(target->post_halt_script);
963                 target->post_halt_script = strdup(args[2]);
964         }
965         else if (strcmp(args[1], "pre_resume") == 0)
966         {
967                 if (target->pre_resume_script)
968                         free(target->pre_resume_script);
969                 target->pre_resume_script = strdup(args[2]);
970         }
971         else
972         {
973                 ERROR("unknown event type: '%s", args[1]);
974                 exit(-1);       
975         }
976         
977         return ERROR_OK;
978 }
979
980 int handle_run_and_halt_time_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
981 {
982         target_t *target = NULL;
983         
984         if (argc < 2)
985         {
986                 ERROR("incomplete run_and_halt_time command");
987                 exit(-1);
988         }
989         
990         target = get_target_by_num(strtoul(args[0], NULL, 0));
991         
992         if (!target)
993         {
994                 ERROR("target number '%s' not defined", args[0]);
995                 exit(-1);
996         }
997         
998         target->run_and_halt_time = strtoul(args[1], NULL, 0);
999         
1000         return ERROR_OK;
1001 }
1002
1003 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1004 {
1005         target_t *target = NULL;
1006         
1007         if (argc < 4)
1008         {
1009                 ERROR("incomplete working_area command. usage: working_area <target#> <address> <size> <'backup'|'nobackup'>");
1010                 exit(-1);
1011         }
1012         
1013         target = get_target_by_num(strtoul(args[0], NULL, 0));
1014         
1015         if (!target)
1016         {
1017                 ERROR("target number '%s' not defined", args[0]);
1018                 exit(-1);
1019         }
1020         
1021         target->working_area = strtoul(args[1], NULL, 0);
1022         target->working_area_size = strtoul(args[2], NULL, 0);
1023         
1024         if (strcmp(args[3], "backup") == 0)
1025         {
1026                 target->backup_working_area = 1;
1027         }
1028         else if (strcmp(args[3], "nobackup") == 0)
1029         {
1030                 target->backup_working_area = 0;
1031         }
1032         else
1033         {
1034                 ERROR("unrecognized <backup|nobackup> argument (%s)", args[3]);
1035                 exit(-1);
1036         }
1037         
1038         return ERROR_OK;
1039 }
1040
1041
1042 /* process target state changes */
1043 int handle_target(void *priv)
1044 {
1045         int retval;
1046         target_t *target = targets;
1047         
1048         while (target)
1049         {
1050                 /* only poll if target isn't already halted */
1051                 if (target->state != TARGET_HALTED)
1052                 {
1053                         if (target_continous_poll)
1054                                 if ((retval = target->type->poll(target)) < 0)
1055                                 {
1056                                         ERROR("couldn't poll target, exiting");
1057                                         exit(-1);
1058                                 }
1059                 }
1060         
1061                 target = target->next;
1062         }
1063         
1064         return ERROR_OK;
1065 }
1066
1067 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1068 {
1069         target_t *target;
1070         reg_t *reg = NULL;
1071         int count = 0;
1072         char *value;
1073         
1074         DEBUG("");
1075         
1076         target = get_current_target(cmd_ctx);
1077         
1078         /* list all available registers for the current target */
1079         if (argc == 0)
1080         {
1081                 reg_cache_t *cache = target->reg_cache;
1082                 
1083                 count = 0;
1084                 while(cache)
1085                 {
1086                         int i;
1087                         for (i = 0; i < cache->num_regs; i++)
1088                         {
1089                                 value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
1090                                 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);
1091                                 free(value);
1092                         }
1093                         cache = cache->next;
1094                 }
1095                 
1096                 return ERROR_OK;
1097         }
1098         
1099         /* access a single register by its ordinal number */
1100         if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1101         {
1102                 int num = strtoul(args[0], NULL, 0);
1103                 reg_cache_t *cache = target->reg_cache;
1104                 
1105                 count = 0;
1106                 while(cache)
1107                 {
1108                         int i;
1109                         for (i = 0; i < cache->num_regs; i++)
1110                         {
1111                                 if (count++ == num)
1112                                 {
1113                                         reg = &cache->reg_list[i];
1114                                         break;
1115                                 }
1116                         }
1117                         if (reg)
1118                                 break;
1119                         cache = cache->next;
1120                 }
1121                 
1122                 if (!reg)
1123                 {
1124                         command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1125                         return ERROR_OK;
1126                 }
1127         } else /* access a single register by its name */
1128         {
1129                 reg = register_get_by_name(target->reg_cache, args[0], 1);
1130                 
1131                 if (!reg)
1132                 {
1133                         command_print(cmd_ctx, "register %s not found in current target", args[0]);
1134                         return ERROR_OK;
1135                 }
1136         }
1137
1138         /* display a register */
1139         if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1140         {
1141                 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1142                         reg->valid = 0;
1143                 
1144                 if (reg->valid == 0)
1145                 {
1146                         reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1147                         if (arch_type == NULL)
1148                         {
1149                                 ERROR("BUG: encountered unregistered arch type");
1150                                 return ERROR_OK;
1151                         }
1152                         arch_type->get(reg);
1153                 }
1154                 value = buf_to_str(reg->value, reg->size, 16);
1155                 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1156                 free(value);
1157                 return ERROR_OK;
1158         }
1159         
1160         /* set register value */
1161         if (argc == 2)
1162         {
1163                 u8 *buf = malloc(CEIL(reg->size, 8));
1164                 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1165
1166                 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1167                 if (arch_type == NULL)
1168                 {
1169                         ERROR("BUG: encountered unregistered arch type");
1170                         return ERROR_OK;
1171                 }
1172                 
1173                 arch_type->set(reg, buf);
1174                 
1175                 value = buf_to_str(reg->value, reg->size, 16);
1176                 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1177                 free(value);
1178                 
1179                 free(buf);
1180                 
1181                 return ERROR_OK;
1182         }
1183         
1184         command_print(cmd_ctx, "usage: reg <#|name> [value]");
1185         
1186         return ERROR_OK;
1187 }
1188
1189 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1190 {
1191         target_t *target = get_current_target(cmd_ctx);
1192         char buffer[512];
1193
1194         if (argc == 0)
1195         {
1196                 command_print(cmd_ctx, "target state: %s", target_state_strings[target->type->poll(target)]);
1197                 if (target->state == TARGET_HALTED)
1198                 {
1199                         target->type->arch_state(target, buffer, 512);
1200                         buffer[511] = 0;
1201                         command_print(cmd_ctx, "%s", buffer);
1202                 }
1203         }
1204         else
1205         {
1206                 if (strcmp(args[0], "on") == 0)
1207                 {
1208                         target_continous_poll = 1;
1209                 }
1210                 else if (strcmp(args[0], "off") == 0)
1211                 {
1212                         target_continous_poll = 0;
1213                 }
1214         }
1215         
1216         
1217         return ERROR_OK;
1218 }
1219
1220 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1221 {
1222         target_t *target = get_current_target(cmd_ctx);
1223         struct timeval timeout, now;
1224         
1225         gettimeofday(&timeout, NULL);
1226         timeval_add_time(&timeout, 5, 0);
1227
1228         command_print(cmd_ctx, "waiting for target halted...");
1229
1230         while(target->type->poll(target))
1231         {
1232                 if (target->state == TARGET_HALTED)
1233                 {
1234                         command_print(cmd_ctx, "target halted");
1235                         break;
1236                 }
1237                 target_call_timer_callbacks();
1238                 
1239                 gettimeofday(&now, NULL);
1240                 if ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))
1241                 {
1242                         command_print(cmd_ctx, "timed out while waiting for target halt");
1243                         ERROR("timed out while waiting for target halt");
1244                         break;
1245                 }
1246         }
1247         
1248         return ERROR_OK;
1249 }
1250
1251 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1252 {
1253         int retval;
1254         target_t *target = get_current_target(cmd_ctx);
1255
1256         DEBUG("");
1257         
1258         command_print(cmd_ctx, "requesting target halt...");
1259
1260         if ((retval = target->type->halt(target)) != ERROR_OK)
1261         {       
1262                 switch (retval)
1263                 {
1264                         case ERROR_TARGET_ALREADY_HALTED:
1265                                 command_print(cmd_ctx, "target already halted");
1266                                 break;
1267                         case ERROR_TARGET_TIMEOUT:
1268                                 command_print(cmd_ctx, "target timed out... shutting down");
1269                                 exit(-1);
1270                         default:
1271                                 command_print(cmd_ctx, "unknown error... shutting down");
1272                                 exit(-1);
1273                 }
1274         }
1275         
1276         return ERROR_OK;
1277
1278 }
1279
1280 /* what to do on daemon startup */
1281 int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1282 {
1283         if (argc == 1)
1284         {
1285                 if (strcmp(args[0], "attach") == 0)
1286                 {
1287                         startup_mode = DAEMON_ATTACH;
1288                         return ERROR_OK;
1289                 }
1290                 else if (strcmp(args[0], "reset") == 0)
1291                 {
1292                         startup_mode = DAEMON_RESET;
1293                         return ERROR_OK;
1294                 }
1295         }
1296         
1297         WARNING("invalid daemon_startup configuration directive: %s", args[0]);
1298         return ERROR_OK;
1299
1300 }
1301                 
1302 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1303 {
1304         target_t *target = get_current_target(cmd_ctx);
1305         int retval;
1306         
1307         command_print(cmd_ctx, "requesting target halt and executing a soft reset");
1308         
1309         if ((retval = target->type->soft_reset_halt(target)) != ERROR_OK)
1310         {       
1311                 switch (retval)
1312                 {
1313                         case ERROR_TARGET_TIMEOUT:
1314                                 command_print(cmd_ctx, "target timed out... shutting down");
1315                                 exit(-1);
1316                         default:
1317                                 command_print(cmd_ctx, "unknown error... shutting down");
1318                                 exit(-1);
1319                 }
1320         }
1321         
1322         return ERROR_OK;
1323 }
1324
1325 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1326 {
1327         target_t *target = get_current_target(cmd_ctx);
1328         enum target_reset_mode reset_mode = RESET_RUN;
1329         
1330         DEBUG("");
1331         
1332         if (argc >= 1)
1333         {
1334                 if (strcmp("run", args[0]) == 0)
1335                         reset_mode = RESET_RUN;
1336                 else if (strcmp("halt", args[0]) == 0)
1337                         reset_mode = RESET_HALT;
1338                 else if (strcmp("init", args[0]) == 0)
1339                         reset_mode = RESET_INIT;
1340                 else if (strcmp("run_and_halt", args[0]) == 0)
1341                 {
1342                         reset_mode = RESET_RUN_AND_HALT;
1343                         if (argc >= 2)
1344                         {
1345                                 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1346                         }
1347                 }
1348                 else if (strcmp("run_and_init", args[0]) == 0)
1349                 {
1350                         reset_mode = RESET_RUN_AND_INIT;
1351                         if (argc >= 2)
1352                         {
1353                                 target->run_and_halt_time = strtoul(args[1], NULL, 0);
1354                         }
1355                 }
1356                 else
1357                 {
1358                         command_print(cmd_ctx, "usage: reset ['run', 'halt', 'init', 'run_and_halt', 'run_and_init]");
1359                         return ERROR_OK;
1360                 }
1361                 target->reset_mode = reset_mode;
1362         }
1363         
1364         target_process_reset(cmd_ctx);
1365         
1366         return ERROR_OK;
1367 }
1368
1369 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1370 {
1371         int retval;
1372         target_t *target = get_current_target(cmd_ctx);
1373         
1374         DEBUG("");
1375         
1376         if (argc == 0)
1377                 retval = target->type->resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1378         else if (argc == 1)
1379                 retval = target->type->resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1380         else
1381         {
1382                 command_print(cmd_ctx, "usage: resume [address]");
1383                 return ERROR_OK;
1384         }
1385         
1386         if (retval != ERROR_OK)
1387         {       
1388                 switch (retval)
1389                 {
1390                         case ERROR_TARGET_NOT_HALTED:
1391                                 command_print(cmd_ctx, "target not halted");
1392                                 break;
1393                         default:
1394                                 command_print(cmd_ctx, "unknown error... shutting down");
1395                                 exit(-1);
1396                 }
1397         }
1398
1399         return ERROR_OK;
1400 }
1401
1402 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1403 {
1404         target_t *target = get_current_target(cmd_ctx);
1405         
1406         DEBUG("");
1407         
1408         if (argc == 0)
1409                 target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1410
1411         if (argc == 1)
1412                 target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1413         
1414         return ERROR_OK;
1415 }
1416
1417 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1418 {
1419         int count = 1;
1420         int size = 4;
1421         u32 address = 0;
1422         int i;
1423
1424         char output[128];
1425         int output_len;
1426
1427         int retval;
1428
1429         u8 *buffer;
1430         target_t *target = get_current_target(cmd_ctx);
1431
1432         if (argc < 1)
1433                 return ERROR_OK;
1434
1435         if (argc == 2)
1436                 count = strtoul(args[1], NULL, 0);
1437
1438         address = strtoul(args[0], NULL, 0);
1439         
1440
1441         switch (cmd[2])
1442         {
1443                 case 'w':
1444                         size = 4;
1445                         break;
1446                 case 'h':
1447                         size = 2;
1448                         break;
1449                 case 'b':
1450                         size = 1;
1451                         break;
1452                 default:
1453                         return ERROR_OK;
1454         }
1455
1456         buffer = calloc(count, size);
1457         if ((retval  = target->type->read_memory(target, address, size, count, buffer)) != ERROR_OK)
1458         {
1459                 switch (retval)
1460                 {
1461                         case ERROR_TARGET_UNALIGNED_ACCESS:
1462                                 command_print(cmd_ctx, "error: address not aligned");
1463                                 break;
1464                         case ERROR_TARGET_NOT_HALTED:
1465                                 command_print(cmd_ctx, "error: target must be halted for memory accesses");
1466                                 break;                  
1467                         case ERROR_TARGET_DATA_ABORT:
1468                                 command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
1469                                 break;
1470                         default:
1471                                 command_print(cmd_ctx, "error: unknown error");
1472                                 break;
1473                 }
1474                 return ERROR_OK;
1475         }
1476
1477         output_len = 0;
1478
1479         for (i = 0; i < count; i++)
1480         {
1481                 if (i%8 == 0)
1482                         output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1483                 
1484                 switch (size)
1485                 {
1486                         case 4:
1487                                 output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
1488                                 break;
1489                         case 2:
1490                                 output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
1491                                 break;
1492                         case 1:
1493                                 output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
1494                                 break;
1495                 }
1496
1497                 if ((i%8 == 7) || (i == count - 1))
1498                 {
1499                         command_print(cmd_ctx, output);
1500                         output_len = 0;
1501                 }
1502         }
1503
1504         free(buffer);
1505         
1506         return ERROR_OK;
1507 }
1508
1509 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1510 {
1511         u32 address = 0;
1512         u32 value = 0;
1513         int retval;
1514         target_t *target = get_current_target(cmd_ctx);
1515         u8 value_buf[4];
1516
1517         if (argc < 2)
1518                 return ERROR_OK;
1519
1520         address = strtoul(args[0], NULL, 0);
1521         value = strtoul(args[1], NULL, 0);
1522
1523         switch (cmd[2])
1524         {
1525                 case 'w':
1526                         target_buffer_set_u32(target, value_buf, value);
1527                         retval = target->type->write_memory(target, address, 4, 1, value_buf);
1528                         break;
1529                 case 'h':
1530                         target_buffer_set_u16(target, value_buf, value);
1531                         retval = target->type->write_memory(target, address, 2, 1, value_buf);
1532                         break;
1533                 case 'b':
1534                         value_buf[0] = value;
1535                         retval = target->type->write_memory(target, address, 1, 1, value_buf);
1536                         break;
1537                 default:
1538                         return ERROR_OK;
1539         }
1540
1541         switch (retval)
1542         {
1543                 case ERROR_TARGET_UNALIGNED_ACCESS:
1544                         command_print(cmd_ctx, "error: address not aligned");
1545                         break;
1546                 case ERROR_TARGET_DATA_ABORT:
1547                         command_print(cmd_ctx, "error: access caused data abort, system possibly corrupted");
1548                         break;
1549                 case ERROR_TARGET_NOT_HALTED:
1550                         command_print(cmd_ctx, "error: target must be halted for memory accesses");
1551                         break;
1552                 case ERROR_OK:
1553                         break;
1554                 default:
1555                         command_print(cmd_ctx, "error: unknown error");
1556                         break;
1557         }
1558
1559         return ERROR_OK;
1560
1561 }
1562
1563 int handle_load_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1564 {
1565         FILE *binary;
1566         u32 address;
1567         struct stat binary_stat;
1568         u32 binary_size;
1569
1570         u8 *buffer;
1571         u32 buf_cnt;
1572         
1573         struct timeval start, end, duration;
1574                 
1575         target_t *target = get_current_target(cmd_ctx);
1576
1577         if (argc != 2)
1578         {
1579                 command_print(cmd_ctx, "usage: load_binary <filename> <address>");
1580                 return ERROR_OK;
1581         }
1582
1583         address = strtoul(args[1], NULL, 0);
1584
1585         if (stat(args[0], &binary_stat) == -1)
1586         {
1587                 ERROR("couldn't stat() %s: %s", args[0], strerror(errno));
1588                 command_print(cmd_ctx, "error accessing file %s", args[0]);
1589                 return ERROR_OK;
1590         }
1591
1592         if (!(binary = fopen(args[0], "rb")))
1593         {
1594                 ERROR("couldn't open %s: %s", args[0], strerror(errno));
1595                 command_print(cmd_ctx, "error accessing file %s", args[0]);
1596                 return ERROR_OK;
1597         }
1598         
1599         buffer = malloc(128 * 1024);
1600
1601         gettimeofday(&start, NULL);     
1602
1603         binary_size = binary_stat.st_size;
1604         while (binary_size > 0)
1605         {
1606                 buf_cnt = fread(buffer, 1, 128*1024, binary);
1607                 target_write_buffer(target, address, buf_cnt, buffer);
1608                 address += buf_cnt;
1609                 binary_size -= buf_cnt;
1610         }
1611
1612         gettimeofday(&end, NULL);       
1613
1614         free(buffer);
1615         
1616         timeval_subtract(&duration, &end, &start);
1617         command_print(cmd_ctx, "downloaded %lli byte in %is %ius", (long long) binary_stat.st_size, duration.tv_sec, duration.tv_usec);
1618         
1619         fclose(binary);
1620
1621         return ERROR_OK;
1622
1623 }
1624
1625 int handle_dump_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1626 {
1627         FILE *binary;
1628         u32 address;
1629         u32 size;
1630         u8 buffer[560];
1631         
1632         struct timeval start, end, duration;
1633         
1634         target_t *target = get_current_target(cmd_ctx);
1635
1636         if (argc != 3)
1637         {
1638                 command_print(cmd_ctx, "usage: dump_binary <filename> <address> <size>");
1639                 return ERROR_OK;
1640         }
1641
1642         address = strtoul(args[1], NULL, 0);
1643         size = strtoul(args[2], NULL, 0);
1644
1645         if (!(binary = fopen(args[0], "wb")))
1646         {
1647                 ERROR("couldn't open %s for writing: %s", args[0], strerror(errno));
1648                 command_print(cmd_ctx, "error accessing file %s", args[0]);
1649                 return ERROR_OK;
1650         }
1651
1652         if ((address & 3) || (size & 3))
1653         {
1654                 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
1655                 return ERROR_OK;
1656         }
1657
1658         gettimeofday(&start, NULL);     
1659         
1660         while (size > 0)
1661         {
1662                 u32 this_run_size = (size > 560) ? 560 : size;
1663                 target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
1664                 fwrite(buffer, 1, this_run_size, binary);
1665                 size -= this_run_size;
1666                 address += this_run_size;
1667         }
1668
1669         fclose(binary);
1670
1671         gettimeofday(&end, NULL);       
1672
1673         timeval_subtract(&duration, &end, &start);
1674         command_print(cmd_ctx, "dumped %i byte in %is %ius", strtoul(args[2], NULL, 0), duration.tv_sec, duration.tv_usec);
1675         
1676         return ERROR_OK;
1677
1678 }
1679
1680 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1681 {
1682         int retval;
1683         target_t *target = get_current_target(cmd_ctx);
1684
1685         if (argc == 0)
1686         {
1687                 breakpoint_t *breakpoint = target->breakpoints;
1688
1689                 while (breakpoint)
1690                 {
1691                         if (breakpoint->type == BKPT_SOFT)
1692                         {
1693                                 char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
1694                                 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
1695                                 free(buf);
1696                         }
1697                         else
1698                         {
1699                                 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
1700                         }
1701                         breakpoint = breakpoint->next;
1702                 }
1703         }
1704         else if (argc >= 2)
1705         {
1706                 int hw = BKPT_SOFT;
1707                 u32 length = 0;
1708
1709                 length = strtoul(args[1], NULL, 0);
1710                 
1711                 if (argc >= 3)
1712                         if (strcmp(args[2], "hw") == 0)
1713                                 hw = BKPT_HARD;
1714
1715                 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
1716                 {
1717                         switch (retval)
1718                         {
1719                                 case ERROR_TARGET_NOT_HALTED:
1720                                         command_print(cmd_ctx, "target must be halted to set breakpoints");
1721                                         break;
1722                                 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1723                                         command_print(cmd_ctx, "no more breakpoints available");
1724                                         break;
1725                                 default:
1726                                         command_print(cmd_ctx, "unknown error, breakpoint not set");
1727                                         break;
1728                         }
1729                 }
1730                 else
1731                 {
1732                         command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
1733                 }
1734         }
1735         else
1736         {
1737                 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
1738         }
1739
1740         return ERROR_OK;
1741 }
1742
1743 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1744 {
1745         target_t *target = get_current_target(cmd_ctx);
1746
1747         if (argc > 0)
1748                 breakpoint_remove(target, strtoul(args[0], NULL, 0));
1749
1750         return ERROR_OK;
1751 }
1752
1753 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1754 {
1755         target_t *target = get_current_target(cmd_ctx);
1756
1757         if (argc == 0)
1758         {
1759                 watchpoint_t *watchpoint = target->watchpoints;
1760
1761                 while (watchpoint)
1762                 {
1763                         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);
1764                         watchpoint = watchpoint->next;
1765                 }
1766         } 
1767         else if (argc >= 2)
1768         {
1769                 enum watchpoint_rw type = WPT_ACCESS;
1770                 u32 data_value = 0x0;
1771                 u32 data_mask = 0xffffffff;
1772                 
1773                 if (argc >= 3)
1774                 {
1775                         switch(args[2][0])
1776                         {
1777                                 case 'r':
1778                                         type = WPT_READ;
1779                                         break;
1780                                 case 'w':
1781                                         type = WPT_WRITE;
1782                                         break;
1783                                 case 'a':
1784                                         type = WPT_ACCESS;
1785                                         break;
1786                                 default:
1787                                         command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
1788                                         return ERROR_OK;
1789                         }
1790                 }
1791                 if (argc >= 4)
1792                 {
1793                         data_value = strtoul(args[3], NULL, 0);
1794                 }
1795                 if (argc >= 5)
1796                 {
1797                         data_mask = strtoul(args[4], NULL, 0);
1798                 }
1799                 watchpoint_add(target, strtoul(args[0], NULL, 0), strtoul(args[1], NULL, 0), type, data_value, data_mask);
1800         }
1801         else
1802         {
1803                 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
1804         }
1805                 
1806         return ERROR_OK;
1807 }
1808
1809 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1810 {
1811         target_t *target = get_current_target(cmd_ctx);
1812
1813         if (argc > 0)
1814                 watchpoint_remove(target, strtoul(args[0], NULL, 0));
1815         
1816         return ERROR_OK;
1817 }
1818