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