- added gdb flash fixes patch
[fw/openocd] / src / server / gdb_server.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "replacements.h"
25
26 #include "gdb_server.h"
27
28 #include "server.h"
29 #include "log.h"
30 #include "binarybuffer.h"
31 #include "jtag.h"
32 #include "breakpoints.h"
33 #include "flash.h"
34 #include "target_request.h"
35
36 #include <string.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <stdlib.h>
40
41 #if 0
42 #define _DEBUG_GDB_IO_
43 #endif
44
45 static unsigned short gdb_port;
46
47 enum gdb_detach_mode
48 {
49         GDB_DETACH_RESUME,
50         GDB_DETACH_RESET,
51         GDB_DETACH_HALT,
52         GDB_DETACH_NOTHING
53 };
54
55 /* target behaviour on gdb detach */
56 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
57
58 /* set if we are sending a memory map to gdb
59  * via qXfer:memory-map:read packet */
60 int gdb_use_memory_map = 0;
61 int gdb_flash_program = 0;
62
63 int gdb_last_signal(target_t *target)
64 {
65         switch (target->debug_reason)
66         {
67                 case DBG_REASON_DBGRQ:
68                         return 0x2; /* SIGINT */
69                 case DBG_REASON_BREAKPOINT:
70                 case DBG_REASON_WATCHPOINT:
71                 case DBG_REASON_WPTANDBKPT:
72                         return 0x05; /* SIGTRAP */
73                 case DBG_REASON_SINGLESTEP:
74                         return 0x05; /* SIGTRAP */
75                 case DBG_REASON_NOTHALTED:
76                         return 0x0; /* no signal... shouldn't happen */
77                 default:
78                         ERROR("BUG: undefined debug reason");
79                         exit(-1);
80         }
81 }
82
83 int gdb_get_char(connection_t *connection, int* next_char)
84 {
85         gdb_connection_t *gdb_con = connection->priv;
86
87 #ifdef _DEBUG_GDB_IO_
88         char *debug_buffer;
89 #endif
90
91         if (gdb_con->buf_cnt-- > 0)
92         {
93                 *next_char = *(gdb_con->buf_p++);
94                 if (gdb_con->buf_cnt > 0)
95                         connection->input_pending = 1;
96                 else
97                         connection->input_pending = 0;
98
99 #ifdef _DEBUG_GDB_IO_
100                 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
101 #endif
102
103                 return ERROR_OK;
104         }
105
106         while ((gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE)) <= 0)
107         {
108                 if (gdb_con->buf_cnt == 0)
109                         return ERROR_SERVER_REMOTE_CLOSED;
110
111 #ifdef _WIN32
112                 errno = WSAGetLastError();
113
114                 switch(errno)
115                 {
116                         case WSAEWOULDBLOCK:
117                                 usleep(1000);
118                                 break;
119                         case WSAECONNABORTED:
120                                 return ERROR_SERVER_REMOTE_CLOSED;
121                         case WSAECONNRESET:
122                                 return ERROR_SERVER_REMOTE_CLOSED;
123                         default:
124                                 ERROR("read: %d", errno);
125                                 exit(-1);
126                 }
127 #else
128                 switch(errno)
129                 {
130                         case EAGAIN:
131                                 usleep(1000);
132                                 break;
133                         case ECONNABORTED:
134                                 return ERROR_SERVER_REMOTE_CLOSED;
135                         case ECONNRESET:
136                                 return ERROR_SERVER_REMOTE_CLOSED;
137                         default:
138                                 ERROR("read: %s", strerror(errno));
139                                 exit(-1);
140                 }
141 #endif
142         }
143
144 #ifdef _DEBUG_GDB_IO_
145         debug_buffer = malloc(gdb_con->buf_cnt + 1);
146         memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
147         debug_buffer[gdb_con->buf_cnt] = 0;
148         DEBUG("received '%s'", debug_buffer);
149         free(debug_buffer);
150 #endif
151
152         gdb_con->buf_p = gdb_con->buffer;
153         gdb_con->buf_cnt--;
154         *next_char = *(gdb_con->buf_p++);
155         if (gdb_con->buf_cnt > 0)
156                 connection->input_pending = 1;
157         else
158                 connection->input_pending = 0;  
159 #ifdef _DEBUG_GDB_IO_
160         DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
161 #endif
162
163         return ERROR_OK;
164 }
165
166 int gdb_putback_char(connection_t *connection, int last_char)
167 {
168         gdb_connection_t *gdb_con = connection->priv;
169
170         if (gdb_con->buf_p > gdb_con->buffer)
171         {
172                 *(--gdb_con->buf_p) = last_char;
173                 gdb_con->buf_cnt++;
174         }
175         else
176         {
177                 ERROR("BUG: couldn't put character back");      
178         }
179
180         return ERROR_OK;
181 }
182
183 int gdb_put_packet(connection_t *connection, char *buffer, int len)
184 {
185         int i;
186         unsigned char my_checksum = 0;
187         char checksum[3];
188         char *debug_buffer;
189         int reply;
190         int retval;
191         gdb_connection_t *gdb_con = connection->priv;
192
193         for (i = 0; i < len; i++)
194                 my_checksum += buffer[i];
195
196         while (1)
197         {
198                 debug_buffer = malloc(len + 1);
199                 memcpy(debug_buffer, buffer, len);
200                 debug_buffer[len] = 0;
201                 DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
202                 free(debug_buffer);
203
204                 write_socket(connection->fd, "$", 1);
205                 if (len > 0)
206                         write_socket(connection->fd, buffer, len);
207                 write_socket(connection->fd, "#", 1);
208
209                 snprintf(checksum, 3, "%2.2x", my_checksum);
210
211                 write_socket(connection->fd, checksum, 2);
212
213                 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
214                         return retval;
215
216                 if (reply == '+')
217                         break;
218                 else if (reply == '-')
219                         WARNING("negative reply, retrying");
220                 else if (reply == 0x3)
221                 {
222                         gdb_con->ctrl_c = 1;
223                         if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
224                                 return retval;
225                         if (reply == '+')
226                                 break;
227                         else if (reply == '-')
228                                 WARNING("negative reply, retrying");
229                         else
230                         {
231                                 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
232                                 return ERROR_SERVER_REMOTE_CLOSED;
233                         }
234                 }
235                 else
236                 {
237                         ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
238                         return ERROR_SERVER_REMOTE_CLOSED;
239                 }
240         }
241
242         return ERROR_OK;
243 }
244
245 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
246 {
247         int character;
248         int count = 0;
249         int retval;
250         char checksum[3];
251         unsigned char my_checksum = 0;
252         gdb_connection_t *gdb_con = connection->priv;
253
254         while (1)
255         {
256                 do
257                 {
258                         if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
259                                 return retval;
260
261 #ifdef _DEBUG_GDB_IO_
262                         DEBUG("character: '%c'", character);
263 #endif
264
265                         switch (character)
266                         {
267                                 case '$':
268                                         break;
269                                 case '+':
270                                         WARNING("acknowledgment received, but no packet pending");
271                                         break;
272                                 case '-':
273                                         WARNING("negative acknowledgment, but no packet pending");
274                                         break;
275                                 case 0x3:
276                                         gdb_con->ctrl_c = 1;
277                                         *len = 0;
278                                         return ERROR_OK;
279                                 default:
280                                         WARNING("ignoring character 0x%x", character);
281                                         break;
282                         }
283                 } while (character != '$');
284
285                 my_checksum = 0;
286
287                 do
288                 {
289                         if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
290                                 return retval;
291
292                         if (character == '#') break;
293
294                         if (character == '}')
295                         {
296                                 /* data transmitted in binary mode (X packet)
297                                  * uses 0x7d as escape character */
298                                 my_checksum += character & 0xff;
299                                 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
300                                         return retval;
301                                 my_checksum += character & 0xff;
302                                 buffer[count++] = (character ^ 0x20) & 0xff;
303                         }
304                         else
305                         {
306                                 my_checksum += character & 0xff;
307                                 buffer[count++] = character & 0xff;
308                         }
309
310                         if (count > *len)
311                         {
312                                 ERROR("packet buffer too small");
313                                 return ERROR_GDB_BUFFER_TOO_SMALL;
314                         }
315                 } while (1);
316
317                 *len = count;
318
319                 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
320                         return retval;
321                 checksum[0] = character;
322                 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
323                         return retval;
324                 checksum[1] = character;
325                 checksum[2] = 0;
326
327                 if (my_checksum == strtoul(checksum, NULL, 16))
328                 {
329                         write_socket(connection->fd, "+", 1);
330                         break;
331                 }
332
333                 WARNING("checksum error, requesting retransmission");
334                 write_socket(connection->fd, "-", 1);
335         }
336
337         return ERROR_OK;
338 }
339
340 int gdb_output(struct command_context_s *context, char* line)
341 {
342         connection_t *connection = context->output_handler_priv;
343         gdb_connection_t *gdb_connection = connection->priv;
344         
345         char *hex_buffer;
346         int i, bin_size;
347
348         /* check if output is enabled */
349         if (gdb_connection->output_disable)
350         {
351                 return ERROR_OK;
352         }
353         
354         bin_size = strlen(line);
355
356         hex_buffer = malloc(bin_size*2 + 4);
357
358         hex_buffer[0] = 'O';
359         for (i=0; i<bin_size; i++)
360                 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
361         hex_buffer[bin_size*2+1] = '0';
362         hex_buffer[bin_size*2+2] = 'a';
363         hex_buffer[bin_size*2+3] = 0x0;
364
365         gdb_put_packet(connection, hex_buffer, bin_size*2 + 3);
366
367         free(hex_buffer);
368         return ERROR_OK;
369 }
370
371 int gdb_program_handler(struct target_s *target, enum target_event event, void *priv)
372 {
373         FILE *script;
374         struct command_context_s *cmd_ctx = priv;
375         
376         if (target->gdb_program_script)
377         {
378                 script = fopen(target->gdb_program_script, "r");
379                 if (!script)
380                 {
381                         ERROR("couldn't open script file %s", target->gdb_program_script);
382                                 return ERROR_OK;
383                 }
384
385                 INFO("executing gdb_program script '%s'", target->gdb_program_script);
386                 command_run_file(cmd_ctx, script, COMMAND_EXEC);
387                 fclose(script);
388                 
389                 jtag_execute_queue();
390         }
391         
392         return ERROR_OK;
393 }
394
395 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
396 {
397         connection_t *connection = priv;
398         gdb_connection_t *gdb_connection = connection->priv;
399         char sig_reply[4];
400         int signal;
401
402         switch (event)
403         {
404                 case TARGET_EVENT_HALTED:
405                         if (gdb_connection->frontend_state == TARGET_RUNNING)
406                         {
407                                 if (gdb_connection->ctrl_c)
408                                 {
409                                         signal = 0x2;
410                                         gdb_connection->ctrl_c = 0;
411                                 }
412                                 else
413                                 {
414                                         signal = gdb_last_signal(target);
415                                 }
416
417                                 snprintf(sig_reply, 4, "T%2.2x", signal);
418                                 gdb_put_packet(connection, sig_reply, 3);
419                                 gdb_connection->frontend_state = TARGET_HALTED;
420                         }
421                         break;
422                 case TARGET_EVENT_RESUMED:
423                         if (gdb_connection->frontend_state == TARGET_HALTED)
424                         {
425                                 gdb_connection->frontend_state = TARGET_RUNNING;
426                         }
427                         break;
428                 case TARGET_EVENT_GDB_PROGRAM:
429                         gdb_program_handler(target, event, connection->cmd_ctx);
430                         break;
431                 default:
432                         break;
433         }
434
435         return ERROR_OK;
436 }
437
438 int gdb_new_connection(connection_t *connection)
439 {
440         gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
441         gdb_service_t *gdb_service = connection->service->priv;
442         int retval;
443         int initial_ack;
444
445         connection->priv = gdb_connection;
446
447         /* initialize gdb connection information */
448         gdb_connection->buf_p = gdb_connection->buffer;
449         gdb_connection->buf_cnt = 0;
450         gdb_connection->ctrl_c = 0;
451         gdb_connection->frontend_state = TARGET_HALTED;
452         gdb_connection->vflash_image = NULL;
453         gdb_connection->output_disable = 0;
454         
455         /* output goes through gdb connection */
456         command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
457
458         /* register callback to be informed about target events */
459         target_register_event_callback(gdb_target_callback_event_handler, connection);  
460
461         /* a gdb session just attached, put the target in halt mode */
462         if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
463                         (retval != ERROR_TARGET_ALREADY_HALTED))
464         {
465                 ERROR("error when trying to halt target");
466                 exit(-1);
467         }
468
469         while (gdb_service->target->state != TARGET_HALTED)
470         {
471                 gdb_service->target->type->poll(gdb_service->target);
472         }
473
474         /* remove the initial ACK from the incoming buffer */
475         if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
476                 return retval;
477
478         if (initial_ack != '+')
479                 gdb_putback_char(connection, initial_ack);
480
481         return ERROR_OK;
482 }
483
484 int gdb_connection_closed(connection_t *connection)
485 {
486         gdb_service_t *gdb_service = connection->service->priv;
487         gdb_connection_t *gdb_connection = connection->priv;
488
489         /* see if an image built with vFlash commands is left */
490         if (gdb_connection->vflash_image)
491         {
492                 image_close(gdb_connection->vflash_image);
493                 free(gdb_connection->vflash_image);
494                 gdb_connection->vflash_image = NULL;
495         }
496
497         /* if this connection registered a debug-message receiver delete it */
498         delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
499         
500         if (connection->priv)
501         {
502                 free(connection->priv);
503                 connection->priv = NULL;
504         }
505         else
506         {
507                 ERROR("BUG: connection->priv == NULL");
508         }
509
510         target_unregister_event_callback(gdb_target_callback_event_handler, connection);
511
512         return ERROR_OK;
513 }
514
515 void gdb_send_error(connection_t *connection, u8 the_error)
516 {
517         char err[4];
518         snprintf(err, 4, "E%2.2X", the_error );
519         gdb_put_packet(connection, err, 3);
520 }
521
522 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
523 {
524         char sig_reply[4];
525         int signal;
526
527         signal = gdb_last_signal(target);
528
529         snprintf(sig_reply, 4, "S%2.2x", signal);
530         gdb_put_packet(connection, sig_reply, 3);
531
532         return ERROR_OK;
533 }
534
535 void gdb_str_to_target(target_t *target, char *str, char *tstr)
536 {
537         int str_len = strlen(str);
538         int i;
539
540         if (str_len % 2)
541         {
542                 ERROR("BUG: gdb value with uneven number of characters encountered: %s", str);
543                 exit(-1);
544         }
545
546         if (target->endianness == TARGET_LITTLE_ENDIAN)
547         {
548                 for (i = 0; i < str_len; i+=2)
549                 {
550                         tstr[str_len - i - 1] = str[i + 1];
551                         tstr[str_len - i - 2] = str[i];
552                 }
553         }
554         else
555         {
556                 for (i = 0; i < str_len; i++)
557                 {
558                         tstr[i] = str[i];
559                 }
560         }       
561 }
562
563 void gdb_target_to_str(target_t *target, char *tstr, char *str)
564 {
565         int str_len = strlen(tstr);
566         int i;
567
568         if (str_len % 2)
569         {
570                 ERROR("BUG: gdb value with uneven number of characters encountered");
571                 exit(-1);
572         }
573
574         if (target->endianness == TARGET_LITTLE_ENDIAN)
575         {
576                 for (i = 0; i < str_len; i+=2)
577                 {
578                         str[str_len - i - 1] = tstr[i + 1];
579                         str[str_len - i - 2] = tstr[i];
580                 }
581         }
582         else
583         {
584                 for (i = 0; i < str_len; i++)
585                 {
586                         str[i] = tstr[i];
587                 }
588         }       
589 }
590
591 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
592 {
593         reg_t **reg_list;
594         int reg_list_size;
595         int retval;
596         int reg_packet_size = 0;
597         char *reg_packet;
598         char *reg_packet_p;
599         int i;
600
601         DEBUG("-");
602
603         if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
604         {
605                 switch (retval)
606                 {
607                         case ERROR_TARGET_NOT_HALTED:
608                                 ERROR("gdb requested registers but we're not halted, dropping connection");
609                                 return ERROR_SERVER_REMOTE_CLOSED;
610                         default:
611                                 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
612                                 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
613                                 exit(-1);
614                 }
615         }
616
617         for (i = 0; i < reg_list_size; i++)
618         {
619                 reg_packet_size += reg_list[i]->size;
620         }
621
622         reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
623         reg_packet_p = reg_packet;
624
625         for (i = 0; i < reg_list_size; i++)
626         {
627                 char *hex_buf = buf_to_str(reg_list[i]->value, reg_list[i]->size, 16);
628                 DEBUG("hex_buf: %s", hex_buf);
629                 gdb_str_to_target(target, hex_buf, reg_packet_p);
630                 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
631                 free(hex_buf);
632         }
633
634         reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
635         DEBUG("reg_packet: %s", reg_packet_p);
636         free(reg_packet_p);
637
638         gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
639         free(reg_packet);
640
641         free(reg_list);
642
643         return ERROR_OK;
644 }
645
646 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
647 {
648         int i;
649         reg_t **reg_list;
650         int reg_list_size;
651         int retval;
652         char *packet_p;
653
654         DEBUG("-");
655
656         /* skip command character */
657         packet++;
658         packet_size--;
659
660         if (packet_size % 2)
661         {
662                 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
663                 return ERROR_SERVER_REMOTE_CLOSED;
664         }
665
666         if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
667         {
668                 switch (retval)
669                 {
670                         case ERROR_TARGET_NOT_HALTED:
671                                 ERROR("gdb tried to registers but we're not halted, dropping connection");
672                                 return ERROR_SERVER_REMOTE_CLOSED;
673                         default:
674                                 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
675                                 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
676                                 exit(-1);
677                 }
678         }
679
680         packet_p = packet;
681         for (i = 0; i < reg_list_size; i++)
682         {
683                 u8 *bin_buf;
684                 char *hex_buf;
685                 reg_arch_type_t *arch_type;
686
687                 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
688                 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
689                 gdb_target_to_str(target, packet_p, hex_buf);
690
691                 /* convert hex-string to binary buffer */
692                 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
693                 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
694
695                 /* get register arch_type, and call set method */       
696                 arch_type = register_get_arch_type(reg_list[i]->arch_type);
697                 if (arch_type == NULL)
698                 {
699                         ERROR("BUG: encountered unregistered arch type");
700                         exit(-1);
701                 }
702                 arch_type->set(reg_list[i], bin_buf);
703
704                 /* advance packet pointer */            
705                 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
706
707                 free(bin_buf);
708                 free(hex_buf);
709         }
710
711         /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */ 
712         free(reg_list);
713
714         gdb_put_packet(connection, "OK", 2);
715
716         return ERROR_OK;
717 }
718
719 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
720 {
721         char *reg_packet;
722         int reg_num = strtoul(packet + 1, NULL, 16);
723         reg_t **reg_list;
724         int reg_list_size;
725         int retval;
726         char *hex_buf;
727
728         DEBUG("-");
729
730         if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
731         {
732                 switch (retval)
733                 {
734                         case ERROR_TARGET_NOT_HALTED:
735                                 ERROR("gdb requested registers but we're not halted, dropping connection");
736                                 return ERROR_SERVER_REMOTE_CLOSED;
737                         default:
738                                 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
739                                 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
740                                 exit(-1);
741                 }
742         }
743
744         if (reg_list_size <= reg_num)
745         {
746                 ERROR("gdb requested a non-existing register");
747                 exit(-1);
748         }
749
750         reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
751
752         hex_buf = buf_to_str(reg_list[reg_num]->value, reg_list[reg_num]->size, 16);
753
754         gdb_str_to_target(target, hex_buf, reg_packet);
755
756         gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
757
758         free(reg_list);
759         free(reg_packet);
760         free(hex_buf);
761
762         return ERROR_OK;
763 }
764
765 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
766 {
767         char *separator;
768         char *hex_buf;
769         u8 *bin_buf;
770         int reg_num = strtoul(packet + 1, &separator, 16);
771         reg_t **reg_list;
772         int reg_list_size;
773         int retval;
774         reg_arch_type_t *arch_type;
775
776         DEBUG("-");
777
778         if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
779         {
780                 switch (retval)
781                 {
782                         case ERROR_TARGET_NOT_HALTED:
783                                 ERROR("gdb tried to set a register but we're not halted, dropping connection");
784                                 return ERROR_SERVER_REMOTE_CLOSED;
785                         default:
786                                 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
787                                 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
788                                 exit(-1);
789                 }
790         }
791
792         if (reg_list_size < reg_num)
793         {
794                 ERROR("gdb requested a non-existing register");
795                 return ERROR_SERVER_REMOTE_CLOSED;
796         }
797
798         if (*separator != '=')
799         {
800                 ERROR("GDB 'set register packet', but no '=' following the register number");
801                 return ERROR_SERVER_REMOTE_CLOSED;
802         }
803
804         /* convert from GDB-string (target-endian) to hex-string (big-endian) */
805         hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
806         gdb_target_to_str(target, separator + 1, hex_buf);
807
808         /* convert hex-string to binary buffer */
809         bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
810         str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
811
812         /* get register arch_type, and call set method */       
813         arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
814         if (arch_type == NULL)
815         {
816                 ERROR("BUG: encountered unregistered arch type");
817                 exit(-1);
818         }
819         arch_type->set(reg_list[reg_num], bin_buf);
820
821         gdb_put_packet(connection, "OK", 2);
822
823         free(bin_buf);
824         free(hex_buf);
825         free(reg_list);
826
827         return ERROR_OK;
828 }
829
830 int gdb_memory_packet_error(connection_t *connection, int retval)
831 {
832         switch (retval)
833         {
834                 case ERROR_TARGET_NOT_HALTED:
835                         ERROR("gdb tried to read memory but we're not halted, dropping connection");
836                         return ERROR_SERVER_REMOTE_CLOSED;
837                         break;
838                 case ERROR_TARGET_DATA_ABORT:
839                         gdb_send_error(connection, EIO);
840                         break;
841                 case ERROR_TARGET_TRANSLATION_FAULT:
842                         gdb_send_error(connection, EFAULT);
843                         break;
844                 case ERROR_TARGET_UNALIGNED_ACCESS:
845                         gdb_send_error(connection, EFAULT);
846                         break;
847                 default:
848                         ERROR("BUG: unexpected error %i", retval);
849                         exit(-1);
850         }
851
852         return ERROR_OK;
853 }
854
855 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
856 {
857         char *separator;
858         u32 addr = 0;
859         u32 len = 0;
860
861         u8 *buffer;
862         char *hex_buffer;
863
864         int i;
865         int retval;
866
867         /* skip command character */
868         packet++;
869
870         addr = strtoul(packet, &separator, 16);
871
872         if (*separator != ',')
873         {
874                 ERROR("incomplete read memory packet received, dropping connection");
875                 return ERROR_SERVER_REMOTE_CLOSED;
876         }
877
878         len = strtoul(separator+1, NULL, 16);
879
880         buffer = malloc(len);
881
882         DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
883
884         switch (len)
885         {
886                 case 4:
887                         if ((addr % 4) == 0)
888                                 retval = target->type->read_memory(target, addr, 4, 1, buffer);
889                         else
890                                 retval = target->type->read_memory(target, addr, 1, len, buffer);
891                         break;
892                 case 2:
893                         if ((addr % 2) == 0)
894                                 retval = target->type->read_memory(target, addr, 2, 1, buffer);
895                         else
896                                 retval = target->type->read_memory(target, addr, 1, len, buffer);
897                         break;
898                 default:
899                         if (((addr % 4) == 0) && ((len % 4) == 0))
900                                 retval = target->type->read_memory(target, addr, 4, len / 4, buffer);
901                         else
902                                 retval = target->type->read_memory(target, addr, 1, len, buffer);
903         }
904
905         if (retval == ERROR_OK)
906         {
907                 hex_buffer = malloc(len * 2 + 1);
908
909                 for (i=0; i<len; i++)
910                         snprintf(hex_buffer + 2*i, 3, "%2.2x", buffer[i]);
911
912                 gdb_put_packet(connection, hex_buffer, len * 2);
913
914                 free(hex_buffer);
915         }
916         else
917         {
918                 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
919                         return retval; 
920         }
921
922         free(buffer);
923
924         return ERROR_OK;
925 }
926
927 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
928 {
929         char *separator;
930         u32 addr = 0;
931         u32 len = 0;
932
933         u8 *buffer;
934
935         int i;
936         int retval;
937
938         /* skip command character */
939         packet++;
940
941         addr = strtoul(packet, &separator, 16);
942
943         if (*separator != ',')
944         {
945                 ERROR("incomplete write memory packet received, dropping connection");
946                 return ERROR_SERVER_REMOTE_CLOSED;
947         }
948
949         len = strtoul(separator+1, &separator, 16);
950
951         if (*(separator++) != ':')
952         {
953                 ERROR("incomplete write memory packet received, dropping connection");
954                 return ERROR_SERVER_REMOTE_CLOSED;
955         }
956
957         buffer = malloc(len);
958
959         DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
960
961         for (i=0; i<len; i++)
962         {
963                 u32 tmp;
964                 sscanf(separator + 2*i, "%2x", &tmp);
965                 buffer[i] = tmp;
966         }
967
968         retval = ERROR_OK;
969         switch (len)
970         {
971                 /* handle sized writes */
972                 case 4:
973                         if ((addr % 4) == 0)
974                                 retval = target->type->write_memory(target, addr, 4, 1, buffer);
975                         else
976                                 retval = target->type->write_memory(target, addr, 1, len, buffer);
977                         break;
978                 case 2:
979                         if ((addr % 2) == 0)
980                                 retval = target->type->write_memory(target, addr, 2, 1, buffer);
981                         else
982                                 retval = target->type->write_memory(target, addr, 1, len, buffer);
983                         break;
984                 case 3:
985                 case 1:
986                         retval = target->type->write_memory(target, addr, 1, len, buffer);
987                         break;
988                         /* handle bulk writes */
989                 default:
990                         retval = target_write_buffer(target, addr, len, buffer);
991                         break;
992         }
993
994         if (retval == ERROR_OK)
995         {
996                 gdb_put_packet(connection, "OK", 2);
997         }
998         else
999         {
1000                 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1001                         return retval; 
1002         }
1003
1004         free(buffer);
1005
1006         return ERROR_OK;
1007 }
1008
1009 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1010 {
1011         char *separator;
1012         u32 addr = 0;
1013         u32 len = 0;
1014
1015         u8 *buffer;
1016         int retval;
1017
1018         /* skip command character */
1019         packet++;
1020
1021         addr = strtoul(packet, &separator, 16);
1022
1023         if (*separator != ',')
1024         {
1025                 ERROR("incomplete write memory binary packet received, dropping connection");
1026                 return ERROR_SERVER_REMOTE_CLOSED;
1027         }
1028
1029         len = strtoul(separator+1, &separator, 16);
1030
1031         if (*(separator++) != ':')
1032         {
1033                 ERROR("incomplete write memory binary packet received, dropping connection");
1034                 return ERROR_SERVER_REMOTE_CLOSED;
1035         }
1036
1037         retval = ERROR_OK;
1038         if( len ) {
1039
1040                 buffer = malloc(len);
1041
1042                 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1043
1044                 memcpy( buffer, separator, len );
1045
1046                 switch (len)
1047                 {
1048                         case 4:
1049                                 if ((addr % 4) == 0)
1050                                         retval = target->type->write_memory(target, addr, 4, 1, buffer);
1051                                 else
1052                                         retval = target->type->write_memory(target, addr, 1, len, buffer);
1053                                 break;
1054                         case 2:
1055                                 if ((addr % 2) == 0)
1056                                         retval = target->type->write_memory(target, addr, 2, 1, buffer);
1057                                 else
1058                                         retval = target->type->write_memory(target, addr, 1, len, buffer);
1059                                 break;
1060                         case 3:
1061                         case 1:
1062                                 retval = target->type->write_memory(target, addr, 1, len, buffer);
1063                                 break;
1064                         default:
1065                                 retval = target_write_buffer(target, addr, len, buffer);
1066                                 break;
1067                 }
1068
1069                 free(buffer);
1070         }
1071
1072         if (retval == ERROR_OK)
1073         {
1074                 gdb_put_packet(connection, "OK", 2);
1075         }
1076         else
1077         {
1078                 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1079                         return retval; 
1080         }
1081
1082         return ERROR_OK;
1083 }
1084
1085 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1086 {
1087         int current = 0;
1088         u32 address = 0x0;
1089
1090         DEBUG("-");
1091
1092         if (packet_size > 1)
1093         {
1094                 packet[packet_size] = 0;
1095                 address = strtoul(packet + 1, NULL, 16);
1096         }
1097         else
1098         {
1099                 current = 1;
1100         }
1101
1102         if (packet[0] == 'c')
1103         {
1104                 DEBUG("continue");
1105                 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1106         }
1107         else if (packet[0] == 's')
1108         {
1109                 DEBUG("step");
1110                 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1111         }
1112 }
1113
1114 int gdb_bp_wp_packet_error(connection_t *connection, int retval)
1115 {
1116         switch (retval)
1117         {
1118                 case ERROR_TARGET_NOT_HALTED:
1119                         ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
1120                         return ERROR_SERVER_REMOTE_CLOSED;
1121                         break;
1122                 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1123                         gdb_send_error(connection, EBUSY);
1124                         break;
1125                 default:
1126                         ERROR("BUG: unexpected error %i", retval);
1127                         exit(-1);
1128         }
1129
1130         return ERROR_OK;
1131 }
1132
1133 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1134 {
1135         int type;
1136         enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1137         enum watchpoint_rw wp_type;
1138         u32 address;
1139         u32 size;
1140         char *separator;
1141         int retval;
1142
1143         DEBUG("-");
1144
1145         type = strtoul(packet + 1, &separator, 16);
1146
1147         if (type == 0)  /* memory breakpoint */
1148                 bp_type = BKPT_SOFT;
1149         else if (type == 1) /* hardware breakpoint */
1150                 bp_type = BKPT_HARD;
1151         else if (type == 2) /* write watchpoint */
1152                 wp_type = WPT_WRITE;
1153         else if (type == 3) /* read watchpoint */
1154                 wp_type = WPT_READ;
1155         else if (type == 4) /* access watchpoint */
1156                 wp_type = WPT_ACCESS;
1157
1158         if (*separator != ',')
1159         {
1160                 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1161                 return ERROR_SERVER_REMOTE_CLOSED;
1162         }
1163
1164         address = strtoul(separator+1, &separator, 16);
1165
1166         if (*separator != ',')
1167         {
1168                 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1169                 return ERROR_SERVER_REMOTE_CLOSED;
1170         }
1171
1172         size = strtoul(separator+1, &separator, 16);
1173
1174         switch (type)
1175         {
1176                 case 0:
1177                 case 1:
1178                         if (packet[0] == 'Z')
1179                         {
1180                                 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1181                                 {
1182                                         if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1183                                                 return retval;
1184                                 }
1185                                 else
1186                                 {
1187                                         gdb_put_packet(connection, "OK", 2);
1188                                 }
1189                         }
1190                         else
1191                         {
1192                                 breakpoint_remove(target, address);
1193                                 gdb_put_packet(connection, "OK", 2);
1194                         }
1195                         break;
1196                 case 2:
1197                 case 3:
1198                 case 4:
1199                 {
1200                         if (packet[0] == 'Z')
1201                         {
1202                                 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1203                                 {
1204                                         if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1205                                                 return retval;
1206                                 }
1207                                 else
1208                                 {
1209                                         gdb_put_packet(connection, "OK", 2);
1210                                 }
1211                         }
1212                         else
1213                         {
1214                                 watchpoint_remove(target, address);
1215                                 gdb_put_packet(connection, "OK", 2);
1216                         }
1217                         break;
1218                 }
1219                 default:
1220                         break;
1221         }
1222
1223         return ERROR_OK;
1224 }
1225
1226 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1227 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1228 {
1229         if (*retval != ERROR_OK)
1230         {
1231                 return;
1232         }
1233         int first = 1;
1234         
1235         for (;;)
1236         {
1237                 if ((*xml == NULL) || (!first))
1238                 {
1239                         /* start by 0 to exercise all the code paths.
1240                          * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1241                          
1242                         *size = *size * 2 + 2;
1243                         char *t=*xml;
1244                         *xml = realloc(*xml, *size);
1245                         if (*xml == NULL)
1246                         {
1247                                 if (t)
1248                                         free(t);
1249                                 *retval=ERROR_SERVER_REMOTE_CLOSED;
1250                                 return;
1251                         }
1252                 }
1253                 
1254             va_list ap;
1255             int ret;
1256             va_start(ap, fmt);
1257             ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1258             va_end(ap);
1259             if ((ret > 0) && ((ret + 1) < *size - *pos))
1260             {
1261                 *pos += ret;
1262                 return;
1263             }
1264             /* there was just enough or not enough space, allocate more. */
1265             first = 0;
1266         }
1267 }
1268
1269 static int decode_xfer_read (char *buf, char **annex, int *ofs, unsigned int *len)
1270 {
1271         char *separator;
1272         
1273         /* Extract and NUL-terminate the annex. */
1274         *annex = buf;
1275         while (*buf && *buf != ':')
1276                 buf++;
1277         if (*buf == '\0')
1278                 return -1;
1279         *buf++ = 0;
1280         
1281         /* After the read marker and annex, qXfer looks like a
1282          * traditional 'm' packet. */
1283         
1284         *ofs = strtoul(buf, &separator, 16);
1285
1286         if (*separator != ',')
1287                 return -1;
1288
1289         *len = strtoul(separator+1, NULL, 16);
1290         
1291         return 0;
1292 }
1293
1294 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1295 {
1296         command_context_t *cmd_ctx = connection->cmd_ctx;
1297         
1298         if (strstr(packet, "qRcmd,"))
1299         {
1300                 if (packet_size > 6)
1301                 {
1302                         char *cmd;
1303                         int i;
1304                         cmd = malloc((packet_size - 6)/2 + 1);
1305                         for (i=0; i < (packet_size - 6)/2; i++)
1306                         {
1307                                 u32 tmp;
1308                                 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1309                                 cmd[i] = tmp;
1310                         }
1311                         cmd[(packet_size - 6)/2] = 0x0;
1312                         command_run_line(cmd_ctx, cmd);
1313                         free(cmd);
1314                 }
1315                 gdb_put_packet(connection, "OK", 2);
1316                 return ERROR_OK;
1317         }
1318         else if (strstr(packet, "qCRC:"))
1319         {
1320                 if (packet_size > 5)
1321                 {
1322                         int retval;
1323                         u8 gdb_reply[10];
1324                         char *separator;
1325                         u32 checksum;
1326                         u32 addr = 0;
1327                         u32 len = 0;
1328                         
1329                         /* skip command character */
1330                         packet += 5;
1331                         
1332                         addr = strtoul(packet, &separator, 16);
1333                         
1334                         if (*separator != ',')
1335                         {
1336                                 ERROR("incomplete read memory packet received, dropping connection");
1337                                 return ERROR_SERVER_REMOTE_CLOSED;
1338                         }
1339                         
1340                         len = strtoul(separator + 1, NULL, 16);
1341                         
1342                         retval = target_checksum_memory(target, addr, len, &checksum);
1343                         
1344                         if (retval == ERROR_OK)
1345                         {
1346                                 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1347                                 gdb_put_packet(connection, gdb_reply, 9);
1348                         }
1349                         else
1350                         {
1351                                 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1352                                         return retval; 
1353                         }
1354                         
1355                         return ERROR_OK;
1356                 }
1357         }
1358         else if (strstr(packet, "qSupported"))
1359         {
1360                 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1361                  * disable qXfer:features:read for the moment */
1362                 int retval = ERROR_OK;
1363                 char *buffer = NULL;
1364                 int pos = 0;
1365                 int size = 0;
1366                 xml_printf(&retval, &buffer, &pos, &size, 
1367                                 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1368                                 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1369                 if (buffer!=NULL)
1370                 {
1371                         gdb_put_packet(connection, buffer, strlen(buffer));
1372                         free(buffer);
1373                 }
1374                 return retval;
1375         }
1376         else if (strstr(packet, "qXfer:memory-map:read::"))
1377         {
1378                 /* We get away with only specifying flash here. Regions that are not
1379                  * specified are treated as if we provided no memory map(if not we 
1380                  * could detect the holes and mark them as RAM).
1381                  * Normally we only execute this code once, but no big deal if we
1382                  * have to regenerate it a couple of times. */
1383                  
1384                 flash_bank_t *p;
1385                 char *xml = NULL;
1386                 int size = 0;
1387                 int pos = 0;
1388                 int retval = ERROR_OK;
1389                 
1390                 int offset;
1391                 int length;
1392                 char *separator;
1393                 
1394                 /* skip command character */
1395                 packet += 23;
1396                 
1397                 offset = strtoul(packet, &separator, 16);
1398                 length = strtoul(separator + 1, &separator, 16);
1399                 
1400                 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1401                 
1402                 int i = 0;
1403                 for (;;)
1404                 {
1405                         p = get_flash_bank_by_num(i);
1406                         if (p == NULL)
1407                                 break;
1408                         
1409                         xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1410                                 "<property name=\"blocksize\">0x%x</property>\n" \
1411                                 "</memory>\n", \
1412                                 p->base, p->size, p->size/p->num_sectors);
1413                         i++;
1414                 }
1415                 
1416                 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1417
1418                 if (retval != ERROR_OK)
1419                 {
1420                         gdb_send_error(connection, retval);
1421                         return retval;
1422                 }
1423                                 
1424                 if (offset + length > pos)
1425                 {
1426                         length = pos - offset;
1427                 }
1428
1429                 char *t = malloc(length + 1);
1430                 t[0] = 'l';
1431                 memcpy(t + 1, xml + offset, length);
1432                 gdb_put_packet(connection, t, length + 1);
1433                 
1434                 free(t);
1435                 free(xml);
1436                 return ERROR_OK;
1437         }
1438         else if (strstr(packet, "qXfer:features:read:"))
1439         {                
1440                 char *xml = NULL;
1441                 int size = 0;
1442                 int pos = 0;
1443                 int retval = ERROR_OK;
1444                 
1445                 int offset;
1446                 int length;
1447                 char *annex;
1448                 
1449                 /* skip command character */
1450                 packet += 20;
1451                 
1452                 if (decode_xfer_read( packet, &annex, &offset, &length ) < 0)
1453                 {
1454                         gdb_send_error(connection, 01);
1455                         return ERROR_OK;
1456                 }
1457                 
1458                 if (strcmp(annex, "target.xml") != 0)
1459                 {
1460                         gdb_send_error(connection, 01);
1461                         return ERROR_OK;
1462                 }
1463                                 
1464                 xml_printf(&retval, &xml, &pos, &size, \
1465                         "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1466                                         
1467                 if (retval != ERROR_OK)
1468                 {
1469                         gdb_send_error(connection, retval);
1470                         return retval;
1471                 }
1472                 
1473                 gdb_put_packet(connection, xml, strlen(xml) + 1);
1474                 
1475                 free(xml);
1476                 return ERROR_OK;
1477         }
1478         
1479         gdb_put_packet(connection, "", 0);
1480         return ERROR_OK;
1481 }
1482
1483 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1484 {
1485         gdb_connection_t *gdb_connection = connection->priv;
1486         gdb_service_t *gdb_service = connection->service->priv;
1487         int result;
1488
1489         /* if flash programming disabled - send a empty reply */
1490         
1491         if (gdb_flash_program == 0)
1492         {
1493                 gdb_put_packet(connection, "", 0);
1494                 return ERROR_OK;
1495         }
1496         
1497         if (strstr(packet, "vFlashErase:"))
1498         {
1499                 unsigned long addr;
1500                 unsigned long length;
1501         
1502                 char *parse = packet + 12;
1503                 if (*parse == '\0')
1504                 {
1505                         ERROR("incomplete vFlashErase packet received, dropping connection");
1506                         return ERROR_SERVER_REMOTE_CLOSED;
1507                 }
1508
1509                 addr = strtoul(parse, &parse, 16);
1510
1511                 if (*(parse++) != ',' || *parse == '\0')
1512                 {
1513                         ERROR("incomplete vFlashErase packet received, dropping connection");
1514                         return ERROR_SERVER_REMOTE_CLOSED;
1515                 }
1516
1517                 length = strtoul(parse, &parse, 16);
1518
1519                 if (*parse != '\0')
1520                 {
1521                         ERROR("incomplete vFlashErase packet received, dropping connection");
1522                         return ERROR_SERVER_REMOTE_CLOSED;
1523                 }
1524                 
1525                 /* disable gdb output while programming */
1526                 gdb_connection->output_disable = 1;
1527                 
1528                 /* assume all sectors need erasing - stops any problems
1529                  * when flash_write is called multiple times */
1530                 flash_set_dirty();
1531                 
1532                 /* perform any target specific operations before the erase */
1533                 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1534                 
1535                 /* perform erase */
1536                 if ((result = flash_erase(gdb_service->target, addr, length)) != ERROR_OK)
1537                 {
1538                         /* GDB doesn't evaluate the actual error number returned,
1539                          * treat a failed erase as an I/O error
1540                          */
1541                         gdb_send_error(connection, EIO);
1542                         ERROR("flash_erase returned %i", result);
1543                 }
1544                 else
1545                         gdb_put_packet(connection, "OK", 2);
1546                 
1547                 /* reenable gdb output */
1548                 gdb_connection->output_disable = 0;
1549                 
1550                 return ERROR_OK;
1551         }
1552
1553         if (strstr(packet, "vFlashWrite:"))
1554         {
1555                 unsigned long addr;
1556                 unsigned long length;
1557                 char *parse = packet + 12;
1558
1559                 if (*parse == '\0')
1560                 {
1561                         ERROR("incomplete vFlashErase packet received, dropping connection");
1562                         return ERROR_SERVER_REMOTE_CLOSED;
1563                 }
1564                 addr = strtoul(parse, &parse, 16);
1565                 if (*(parse++) != ':')
1566                 {
1567                         ERROR("incomplete vFlashErase packet received, dropping connection");
1568                         return ERROR_SERVER_REMOTE_CLOSED;
1569                 }
1570                 length = packet_size - (parse - packet);
1571                 
1572                 /* disable gdb output while programming */
1573                 gdb_connection->output_disable = 1;
1574                 
1575                 /* create a new image if there isn't already one */
1576                 if (gdb_connection->vflash_image == NULL)
1577                 {
1578                         gdb_connection->vflash_image = malloc(sizeof(image_t));
1579                         image_open(gdb_connection->vflash_image, "", "build");
1580                 }
1581
1582                 /* create new section with content from packet buffer */
1583                 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1584
1585                 gdb_put_packet(connection, "OK", 2);
1586
1587                 /* reenable gdb output */
1588                 gdb_connection->output_disable = 0;
1589                 
1590                 return ERROR_OK;
1591         }
1592
1593         if (!strcmp(packet, "vFlashDone"))
1594         {
1595                 u32 written;
1596                 char *error_str;
1597
1598                 /* disable gdb output while programming */
1599                 gdb_connection->output_disable = 1;
1600                 
1601                 /* process the flashing buffer */
1602                 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, &error_str, NULL, 0)) != ERROR_OK)
1603                 {
1604                         if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1605                                 gdb_put_packet(connection, "E.memtype", 9);
1606                         else
1607                                 gdb_send_error(connection, EIO);
1608                         
1609                         if (error_str)
1610                         {
1611                                 ERROR("flash writing failed: %s", error_str);
1612                                 free(error_str);
1613                         }
1614                 }
1615                 else
1616                 {
1617                         DEBUG("wrote %u bytes from vFlash image to flash", written);
1618                         gdb_put_packet(connection, "OK", 2);
1619                 }
1620                 
1621                 image_close(gdb_connection->vflash_image);
1622                 free(gdb_connection->vflash_image);
1623                 gdb_connection->vflash_image = NULL;
1624                 
1625                 /* reenable gdb output */
1626                 gdb_connection->output_disable = 0;
1627                 
1628                 return ERROR_OK;
1629         }
1630
1631         gdb_put_packet(connection, "", 0);
1632         return ERROR_OK;
1633 }
1634
1635 int gdb_detach(connection_t *connection, target_t *target)
1636 {
1637         switch( detach_mode )
1638         {
1639                 case GDB_DETACH_RESUME:
1640                         target->type->resume(target, 1, 0, 1, 0);
1641                         break;
1642                 
1643                 case GDB_DETACH_RESET:
1644                         target_process_reset(connection->cmd_ctx);
1645                         break;
1646                 
1647                 case GDB_DETACH_HALT:
1648                         target->type->halt(target);
1649                         break;
1650                 
1651                 case GDB_DETACH_NOTHING:
1652                         break;
1653         }
1654         
1655         gdb_put_packet(connection, "OK", 2);
1656         
1657         return ERROR_OK;
1658 }
1659
1660 int gdb_input(connection_t *connection)
1661 {
1662         gdb_service_t *gdb_service = connection->service->priv;
1663         target_t *target = gdb_service->target;
1664         char packet[GDB_BUFFER_SIZE];
1665         int packet_size;
1666         int retval;
1667         gdb_connection_t *gdb_con = connection->priv;
1668         static int extended_protocol = 0;
1669
1670         /* drain input buffer */
1671         do
1672         {
1673                 packet_size = GDB_BUFFER_SIZE-1;
1674                 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1675                 {
1676                         switch (retval)
1677                         {
1678                                 case ERROR_GDB_BUFFER_TOO_SMALL:
1679                                         ERROR("BUG: buffer supplied for gdb packet was too small");
1680                                         exit(-1);
1681                                 case ERROR_SERVER_REMOTE_CLOSED:
1682                                         return ERROR_SERVER_REMOTE_CLOSED;
1683                                 default:
1684                                         ERROR("BUG: unexpected error");
1685                                         exit(-1);
1686                         }
1687                 }
1688
1689                 /* terminate with zero */
1690                 packet[packet_size] = 0;
1691
1692                 DEBUG("received packet: '%s'", packet);
1693
1694                 if (packet_size > 0)
1695                 {
1696                         retval = ERROR_OK;
1697                         switch (packet[0])
1698                         {
1699                                 case 'H':
1700                                         /* Hct... -- set thread 
1701                                          * we don't have threads, send empty reply */
1702                                         gdb_put_packet(connection, NULL, 0);
1703                                         break;
1704                                 case 'q':
1705                                         retval = gdb_query_packet(connection, target, packet, packet_size);
1706                                         break;
1707                                 case 'g':
1708                                         retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1709                                         break;
1710                                 case 'G':
1711                                         retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1712                                         break;
1713                                 case 'p':
1714                                         retval = gdb_get_register_packet(connection, target, packet, packet_size);
1715                                         break;
1716                                 case 'P':
1717                                         retval = gdb_set_register_packet(connection, target, packet, packet_size);
1718                                         break;
1719                                 case 'm':
1720                                         retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1721                                         break;
1722                                 case 'M':
1723                                         retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1724                                         break;
1725                                 case 'z':
1726                                 case 'Z':
1727                                         retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1728                                         break;
1729                                 case '?':
1730                                         gdb_last_signal_packet(connection, target, packet, packet_size);
1731                                         break;
1732                                 case 'c':
1733                                 case 's':
1734                                         gdb_step_continue_packet(connection, target, packet, packet_size);
1735                                         break;
1736                                 case 'v':
1737                                         retval = gdb_v_packet(connection, target, packet, packet_size);
1738                                         break;
1739                                 case 'D':
1740                                         retval = gdb_detach(connection, target);
1741                                         extended_protocol = 0;
1742                                         break;
1743                                 case 'X':
1744                                         if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1745                                                 return retval;
1746                                         break;
1747                                 case 'k':
1748                                         if (extended_protocol != 0)
1749                                                 break;
1750                                         gdb_put_packet(connection, "OK", 2);
1751                                         return ERROR_SERVER_REMOTE_CLOSED;
1752                                 case '!':
1753                                         /* handle extended remote protocol */
1754                                         extended_protocol = 1;
1755                                         gdb_put_packet(connection, "OK", 2);
1756                                         break;
1757                                 case 'R':
1758                                         /* handle extended restart packet */
1759                                         target_process_reset(connection->cmd_ctx);
1760                                         break;
1761                                 default:
1762                                         /* ignore unkown packets */
1763                                         DEBUG("ignoring 0x%2.2x packet", packet[0]);
1764                                         gdb_put_packet(connection, NULL, 0);
1765                                         break;
1766                         }
1767
1768                         /* if a packet handler returned an error, exit input loop */
1769                         if (retval != ERROR_OK)
1770                                 return retval;
1771                 }
1772
1773                 if (gdb_con->ctrl_c)
1774                 {
1775                         if (target->state == TARGET_RUNNING)
1776                         {
1777                                 target->type->halt(target);
1778                                 gdb_con->ctrl_c = 0;
1779                         }
1780                 }
1781
1782         } while (gdb_con->buf_cnt > 0);
1783
1784         return ERROR_OK;
1785 }
1786
1787 int gdb_init()
1788 {
1789         gdb_service_t *gdb_service;
1790         target_t *target = targets;
1791         int i = 0;
1792
1793         if (!target)
1794         {
1795                 WARNING("no gdb ports allocated as no target has been specified");
1796                 return ERROR_OK;
1797         }
1798
1799         if (gdb_port == 0)
1800         {
1801                 WARNING("no gdb port specified, using default port 3333");
1802                 gdb_port = 3333;
1803         }
1804
1805         while (target)
1806         {
1807                 char service_name[8];
1808
1809                 snprintf(service_name, 8, "gdb-%2.2i", i);
1810
1811                 gdb_service = malloc(sizeof(gdb_service_t));
1812                 gdb_service->target = target;
1813
1814                 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1815
1816                 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1817
1818                 i++;
1819                 target = target->next;
1820         }
1821
1822         return ERROR_OK;
1823 }
1824
1825 /* daemon configuration command gdb_port */
1826 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1827 {
1828         if (argc == 0)
1829                 return ERROR_OK;
1830
1831         /* only if the port wasn't overwritten by cmdline */
1832         if (gdb_port == 0)
1833                 gdb_port = strtoul(args[0], NULL, 0);
1834
1835         return ERROR_OK;
1836 }
1837
1838 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1839 {
1840         if (argc == 1)
1841         {
1842                 if (strcmp(args[0], "resume") == 0)
1843                 {
1844                         detach_mode = GDB_DETACH_RESUME;
1845                         return ERROR_OK;
1846                 }
1847                 else if (strcmp(args[0], "reset") == 0)
1848                 {
1849                         detach_mode = GDB_DETACH_RESET;
1850                         return ERROR_OK;
1851                 }
1852                 else if (strcmp(args[0], "halt") == 0)
1853                 {
1854                         detach_mode = GDB_DETACH_HALT;
1855                         return ERROR_OK;
1856                 }
1857                 else if (strcmp(args[0], "nothing") == 0)
1858                 {
1859                         detach_mode = GDB_DETACH_NOTHING;
1860                         return ERROR_OK;
1861                 }
1862         }
1863         
1864         WARNING("invalid gdb_detach configuration directive: %s", args[0]);
1865         return ERROR_OK;
1866 }
1867
1868 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1869 {
1870         if (argc == 1)
1871         {
1872                 if (strcmp(args[0], "enable") == 0)
1873                 {
1874                         gdb_use_memory_map = 1;
1875                         return ERROR_OK;
1876                 }
1877                 else if (strcmp(args[0], "disable") == 0)
1878                 {
1879                         gdb_use_memory_map = 0;
1880                         return ERROR_OK;
1881                 }
1882         }
1883         
1884         WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
1885         return ERROR_OK;
1886 }
1887
1888 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1889 {
1890         if (argc == 1)
1891         {
1892                 if (strcmp(args[0], "enable") == 0)
1893                 {
1894                         gdb_flash_program = 1;
1895                         return ERROR_OK;
1896                 }
1897                 else if (strcmp(args[0], "disable") == 0)
1898                 {
1899                         gdb_flash_program = 0;
1900                         return ERROR_OK;
1901                 }
1902         }
1903         
1904         WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
1905         return ERROR_OK;
1906 }
1907
1908 int gdb_register_commands(command_context_t *command_context)
1909 {
1910         register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
1911                         COMMAND_CONFIG, "");
1912         register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
1913                         COMMAND_CONFIG, "");
1914         register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
1915                         COMMAND_CONFIG, "");
1916         register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
1917                         COMMAND_CONFIG, "");
1918         return ERROR_OK;
1919 }