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