- minimum autoconf 2.59 is now required and verified - due to issues with AS_HELP_STRING
[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 XML and allocate more space as needed */
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                         *xml = realloc(*xml, *size);
1244                         if (*xml == NULL)
1245                         {
1246                                 *retval = 1;
1247                                 return;
1248                         }
1249                 }
1250                 
1251             va_list ap;
1252             int ret;
1253             va_start(ap, fmt);
1254             ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1255             va_end(ap);
1256             if ((ret > 0) && ((ret + 1) < *size - *pos))
1257             {
1258                 *pos += ret;
1259                 return;
1260             }
1261             /* there was just enough or not enough space, allocate more. */
1262             first = 0;
1263         }
1264 }
1265
1266 static int decode_xfer_read (char *buf, char **annex, int *ofs, unsigned int *len)
1267 {
1268         char *separator;
1269         
1270         /* Extract and NUL-terminate the annex. */
1271         *annex = buf;
1272         while (*buf && *buf != ':')
1273                 buf++;
1274         if (*buf == '\0')
1275                 return -1;
1276         *buf++ = 0;
1277         
1278         /* After the read marker and annex, qXfer looks like a
1279          * traditional 'm' packet. */
1280         
1281         *ofs = strtoul(buf, &separator, 16);
1282
1283         if (*separator != ',')
1284                 return -1;
1285
1286         *len = strtoul(separator+1, NULL, 16);
1287         
1288         return 0;
1289 }
1290
1291 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1292 {
1293         char buffer[GDB_BUFFER_SIZE];
1294         command_context_t *cmd_ctx = connection->cmd_ctx;
1295         
1296         if (strstr(packet, "qRcmd,"))
1297         {
1298                 if (packet_size > 6)
1299                 {
1300                         char *cmd;
1301                         int i;
1302                         cmd = malloc((packet_size - 6)/2 + 1);
1303                         for (i=0; i < (packet_size - 6)/2; i++)
1304                         {
1305                                 u32 tmp;
1306                                 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1307                                 cmd[i] = tmp;
1308                         }
1309                         cmd[(packet_size - 6)/2] = 0x0;
1310                         command_run_line(cmd_ctx, cmd);
1311                         free(cmd);
1312                 }
1313                 gdb_put_packet(connection, "OK", 2);
1314                 return ERROR_OK;
1315         }
1316         else if (strstr(packet, "qCRC:"))
1317         {
1318                 if (packet_size > 5)
1319                 {
1320                         int retval;
1321                         u8 gdb_reply[10];
1322                         char *separator;
1323                         u32 checksum;
1324                         u32 addr = 0;
1325                         u32 len = 0;
1326                         
1327                         /* skip command character */
1328                         packet += 5;
1329                         
1330                         addr = strtoul(packet, &separator, 16);
1331                         
1332                         if (*separator != ',')
1333                         {
1334                                 ERROR("incomplete read memory packet received, dropping connection");
1335                                 return ERROR_SERVER_REMOTE_CLOSED;
1336                         }
1337                         
1338                         len = strtoul(separator + 1, NULL, 16);
1339                         
1340                         retval = target_checksum_memory(target, addr, len, &checksum);
1341                         
1342                         if (retval == ERROR_OK)
1343                         {
1344                                 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1345                                 gdb_put_packet(connection, gdb_reply, 9);
1346                         }
1347                         else
1348                         {
1349                                 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1350                                         return retval; 
1351                         }
1352                         
1353                         return ERROR_OK;
1354                 }
1355         }
1356         else if (strstr(packet, "qSupported"))
1357         {
1358                 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1359                  * disable qXfer:features:read for the moment */
1360                 
1361                 sprintf(buffer, "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1362                         (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1363                 
1364                 gdb_put_packet(connection, buffer, strlen(buffer));
1365                 return ERROR_OK;
1366         }
1367         else if (strstr(packet, "qXfer:memory-map:read::"))
1368         {
1369                 /* We get away with only specifying flash here. Regions that are not
1370                  * specified are treated as if we provided no memory map(if not we 
1371                  * could detect the holes and mark them as RAM).
1372                  * Normally we only execute this code once, but no big deal if we
1373                  * have to regenerate it a couple of times. */
1374                  
1375                 flash_bank_t *p;
1376                 char *xml = NULL;
1377                 int size = 0;
1378                 int pos = 0;
1379                 int retval = ERROR_OK;
1380                 
1381                 int offset;
1382                 int length;
1383                 char *separator;
1384                 
1385                 /* skip command character */
1386                 packet += 23;
1387                 
1388                 offset = strtoul(packet, &separator, 16);
1389                 length = strtoul(separator + 1, &separator, 16);
1390                 
1391                 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1392                 
1393                 int i = 0;
1394                 for (;;)
1395                 {
1396                         p = get_flash_bank_by_num(i);
1397                         if (p == NULL)
1398                                 break;
1399                         
1400                         xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1401                                 "<property name=\"blocksize\">0x%x</property>\n" \
1402                                 "</memory>\n", \
1403                                 p->base, p->size, p->size/p->num_sectors);
1404                         i++;
1405                 }
1406                 
1407                 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1408
1409                 if (retval != ERROR_OK)
1410                 {
1411                         gdb_send_error(connection, retval);
1412                         return retval;
1413                 }
1414                                 
1415                 if (offset + length > pos)
1416                 {
1417                         length = pos - offset;
1418                 }
1419
1420                 char *t = malloc(length + 1);
1421                 t[0] = 'l';
1422                 memcpy(t + 1, xml + offset, length);
1423                 gdb_put_packet(connection, t, length + 1);
1424                 
1425                 free(t);
1426                 free(xml);
1427                 return ERROR_OK;
1428         }
1429         else if (strstr(packet, "qXfer:features:read:"))
1430         {                
1431                 char *xml = NULL;
1432                 int size = 0;
1433                 int pos = 0;
1434                 int retval = ERROR_OK;
1435                 
1436                 int offset;
1437                 int length;
1438                 char *annex;
1439                 
1440                 /* skip command character */
1441                 packet += 20;
1442                 
1443                 if (decode_xfer_read( packet, &annex, &offset, &length ) < 0)
1444                 {
1445                         gdb_send_error(connection, 01);
1446                         return ERROR_OK;
1447                 }
1448                 
1449                 if (strcmp(annex, "target.xml") != 0)
1450                 {
1451                         gdb_send_error(connection, 01);
1452                         return ERROR_OK;
1453                 }
1454                                 
1455                 xml_printf(&retval, &xml, &pos, &size, \
1456                         "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1457                                         
1458                 if (retval != ERROR_OK)
1459                 {
1460                         gdb_send_error(connection, retval);
1461                         return retval;
1462                 }
1463                 
1464                 gdb_put_packet(connection, xml, strlen(xml) + 1);
1465                 
1466                 free(xml);
1467                 return ERROR_OK;
1468         }
1469         
1470         gdb_put_packet(connection, "", 0);
1471         return ERROR_OK;
1472 }
1473
1474 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1475 {
1476         gdb_connection_t *gdb_connection = connection->priv;
1477         gdb_service_t *gdb_service = connection->service->priv;
1478         int result;
1479
1480         /* if flash programming disabled - send a empty reply */
1481         
1482         if (gdb_flash_program == 0)
1483         {
1484                 gdb_put_packet(connection, "", 0);
1485                 return ERROR_OK;
1486         }
1487         
1488         if (strstr(packet, "vFlashErase:"))
1489         {
1490                 unsigned long addr;
1491                 unsigned long length;
1492         
1493                 char *parse = packet + 12;
1494                 if (*parse == '\0')
1495                 {
1496                         ERROR("incomplete vFlashErase packet received, dropping connection");
1497                         return ERROR_SERVER_REMOTE_CLOSED;
1498                 }
1499
1500                 addr = strtoul(parse, &parse, 16);
1501
1502                 if (*(parse++) != ',' || *parse == '\0')
1503                 {
1504                         ERROR("incomplete vFlashErase packet received, dropping connection");
1505                         return ERROR_SERVER_REMOTE_CLOSED;
1506                 }
1507
1508                 length = strtoul(parse, &parse, 16);
1509
1510                 if (*parse != '\0')
1511                 {
1512                         ERROR("incomplete vFlashErase packet received, dropping connection");
1513                         return ERROR_SERVER_REMOTE_CLOSED;
1514                 }
1515                 
1516                 /* disable gdb output while programming */
1517                 gdb_connection->output_disable = 1;
1518                 
1519                 /* assume all sectors need erasing - stops any problems
1520                  * when flash_write is called multiple times */
1521                 flash_set_dirty();
1522                 
1523                 /* perform any target specific operations before the erase */
1524                 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1525                 
1526                 /* perform erase */
1527                 if ((result = flash_erase(gdb_service->target, addr, length)) != ERROR_OK)
1528                 {
1529                         /* GDB doesn't evaluate the actual error number returned,
1530                          * treat a failed erase as an I/O error
1531                          */
1532                         gdb_send_error(connection, EIO);
1533                         ERROR("flash_erase returned %i", result);
1534                 }
1535                 else
1536                         gdb_put_packet(connection, "OK", 2);
1537                 
1538                 /* reenable gdb output */
1539                 gdb_connection->output_disable = 0;
1540                 
1541                 return ERROR_OK;
1542         }
1543
1544         if (strstr(packet, "vFlashWrite:"))
1545         {
1546                 unsigned long addr;
1547                 unsigned long length;
1548                 char *parse = packet + 12;
1549
1550                 if (*parse == '\0')
1551                 {
1552                         ERROR("incomplete vFlashErase packet received, dropping connection");
1553                         return ERROR_SERVER_REMOTE_CLOSED;
1554                 }
1555                 addr = strtoul(parse, &parse, 16);
1556                 if (*(parse++) != ':')
1557                 {
1558                         ERROR("incomplete vFlashErase packet received, dropping connection");
1559                         return ERROR_SERVER_REMOTE_CLOSED;
1560                 }
1561                 length = packet_size - (parse - packet);
1562                 
1563                 /* disable gdb output while programming */
1564                 gdb_connection->output_disable = 1;
1565                 
1566                 /* create a new image if there isn't already one */
1567                 if (gdb_connection->vflash_image == NULL)
1568                 {
1569                         gdb_connection->vflash_image = malloc(sizeof(image_t));
1570                         image_open(gdb_connection->vflash_image, "", "build");
1571                 }
1572
1573                 /* create new section with content from packet buffer */
1574                 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1575
1576                 gdb_put_packet(connection, "OK", 2);
1577
1578                 /* reenable gdb output */
1579                 gdb_connection->output_disable = 0;
1580                 
1581                 return ERROR_OK;
1582         }
1583
1584         if (!strcmp(packet, "vFlashDone"))
1585         {
1586                 u32 written;
1587                 char *error_str;
1588
1589                 /* disable gdb output while programming */
1590                 gdb_connection->output_disable = 1;
1591                 
1592                 /* process the flashing buffer */
1593                 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, &error_str, NULL, 0)) != ERROR_OK)
1594                 {
1595                         if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1596                                 gdb_put_packet(connection, "E.memtype", 9);
1597                         else
1598                                 gdb_send_error(connection, EIO);
1599                         
1600                         if (error_str)
1601                         {
1602                                 ERROR("flash writing failed: %s", error_str);
1603                                 free(error_str);
1604                         }
1605                 }
1606                 else
1607                 {
1608                         DEBUG("wrote %u bytes from vFlash image to flash", written);
1609                         gdb_put_packet(connection, "OK", 2);
1610                 }
1611                 
1612                 image_close(gdb_connection->vflash_image);
1613                 free(gdb_connection->vflash_image);
1614                 gdb_connection->vflash_image = NULL;
1615                 
1616                 /* reenable gdb output */
1617                 gdb_connection->output_disable = 0;
1618                 
1619                 return ERROR_OK;
1620         }
1621
1622         gdb_put_packet(connection, "", 0);
1623         return ERROR_OK;
1624 }
1625
1626 int gdb_detach(connection_t *connection, target_t *target)
1627 {
1628         switch( detach_mode )
1629         {
1630                 case GDB_DETACH_RESUME:
1631                         target->type->resume(target, 1, 0, 1, 0);
1632                         break;
1633                 
1634                 case GDB_DETACH_RESET:
1635                         target_process_reset(connection->cmd_ctx);
1636                         break;
1637                 
1638                 case GDB_DETACH_HALT:
1639                         target->type->halt(target);
1640                         break;
1641                 
1642                 case GDB_DETACH_NOTHING:
1643                         break;
1644         }
1645         
1646         gdb_put_packet(connection, "OK", 2);
1647         
1648         return ERROR_OK;
1649 }
1650
1651 int gdb_input(connection_t *connection)
1652 {
1653         gdb_service_t *gdb_service = connection->service->priv;
1654         target_t *target = gdb_service->target;
1655         char packet[GDB_BUFFER_SIZE];
1656         int packet_size;
1657         int retval;
1658         gdb_connection_t *gdb_con = connection->priv;
1659
1660         /* drain input buffer */
1661         do
1662         {
1663                 packet_size = GDB_BUFFER_SIZE-1;
1664                 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1665                 {
1666                         switch (retval)
1667                         {
1668                                 case ERROR_GDB_BUFFER_TOO_SMALL:
1669                                         ERROR("BUG: buffer supplied for gdb packet was too small");
1670                                         exit(-1);
1671                                 case ERROR_SERVER_REMOTE_CLOSED:
1672                                         return ERROR_SERVER_REMOTE_CLOSED;
1673                                 default:
1674                                         ERROR("BUG: unexpected error");
1675                                         exit(-1);
1676                         }
1677                 }
1678
1679                 /* terminate with zero */
1680                 packet[packet_size] = 0;
1681
1682                 DEBUG("received packet: '%s'", packet);
1683
1684                 if (packet_size > 0)
1685                 {
1686                         retval = ERROR_OK;
1687                         switch (packet[0])
1688                         {
1689                                 case 'H':
1690                                         /* Hct... -- set thread 
1691                                          * we don't have threads, send empty reply */
1692                                         gdb_put_packet(connection, NULL, 0);
1693                                         break;
1694                                 case 'q':
1695                                         retval = gdb_query_packet(connection, target, packet, packet_size);
1696                                         break;
1697                                 case 'g':
1698                                         retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1699                                         break;
1700                                 case 'G':
1701                                         retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1702                                         break;
1703                                 case 'p':
1704                                         retval = gdb_get_register_packet(connection, target, packet, packet_size);
1705                                         break;
1706                                 case 'P':
1707                                         retval = gdb_set_register_packet(connection, target, packet, packet_size);
1708                                         break;
1709                                 case 'm':
1710                                         retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1711                                         break;
1712                                 case 'M':
1713                                         retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1714                                         break;
1715                                 case 'z':
1716                                 case 'Z':
1717                                         retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1718                                         break;
1719                                 case '?':
1720                                         gdb_last_signal_packet(connection, target, packet, packet_size);
1721                                         break;
1722                                 case 'c':
1723                                 case 's':
1724                                         gdb_step_continue_packet(connection, target, packet, packet_size);
1725                                         break;
1726                                 case 'v':
1727                                         retval = gdb_v_packet(connection, target, packet, packet_size);
1728                                         break;
1729                                 case 'D':
1730                                         retval = gdb_detach(connection, target);
1731                                         break;
1732                                 case 'X':
1733                                         if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1734                                                 return retval;
1735                                         break;
1736                                 case 'k':
1737                                         gdb_put_packet(connection, "OK", 2);
1738                                         return ERROR_SERVER_REMOTE_CLOSED;
1739                                 default:
1740                                         /* ignore unkown packets */
1741                                         DEBUG("ignoring 0x%2.2x packet", packet[0]);
1742                                         gdb_put_packet(connection, NULL, 0);
1743                                         break;
1744                         }
1745
1746                         /* if a packet handler returned an error, exit input loop */
1747                         if (retval != ERROR_OK)
1748                                 return retval;
1749                 }
1750
1751                 if (gdb_con->ctrl_c)
1752                 {
1753                         if (target->state == TARGET_RUNNING)
1754                         {
1755                                 target->type->halt(target);
1756                                 gdb_con->ctrl_c = 0;
1757                         }
1758                 }
1759
1760         } while (gdb_con->buf_cnt > 0);
1761
1762         return ERROR_OK;
1763 }
1764
1765 int gdb_init()
1766 {
1767         gdb_service_t *gdb_service;
1768         target_t *target = targets;
1769         int i = 0;
1770
1771         if (!target)
1772         {
1773                 WARNING("no gdb ports allocated as no target has been specified");
1774                 return ERROR_OK;
1775         }
1776
1777         if (gdb_port == 0)
1778         {
1779                 WARNING("no gdb port specified, using default port 3333");
1780                 gdb_port = 3333;
1781         }
1782
1783         while (target)
1784         {
1785                 char service_name[8];
1786
1787                 snprintf(service_name, 8, "gdb-%2.2i", i);
1788
1789                 gdb_service = malloc(sizeof(gdb_service_t));
1790                 gdb_service->target = target;
1791
1792                 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1793
1794                 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1795
1796                 i++;
1797                 target = target->next;
1798         }
1799
1800         return ERROR_OK;
1801 }
1802
1803 /* daemon configuration command gdb_port */
1804 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1805 {
1806         if (argc == 0)
1807                 return ERROR_OK;
1808
1809         /* only if the port wasn't overwritten by cmdline */
1810         if (gdb_port == 0)
1811                 gdb_port = strtoul(args[0], NULL, 0);
1812
1813         return ERROR_OK;
1814 }
1815
1816 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1817 {
1818         if (argc == 1)
1819         {
1820                 if (strcmp(args[0], "resume") == 0)
1821                 {
1822                         detach_mode = GDB_DETACH_RESUME;
1823                         return ERROR_OK;
1824                 }
1825                 else if (strcmp(args[0], "reset") == 0)
1826                 {
1827                         detach_mode = GDB_DETACH_RESET;
1828                         return ERROR_OK;
1829                 }
1830                 else if (strcmp(args[0], "halt") == 0)
1831                 {
1832                         detach_mode = GDB_DETACH_HALT;
1833                         return ERROR_OK;
1834                 }
1835                 else if (strcmp(args[0], "nothing") == 0)
1836                 {
1837                         detach_mode = GDB_DETACH_NOTHING;
1838                         return ERROR_OK;
1839                 }
1840         }
1841         
1842         WARNING("invalid gdb_detach configuration directive: %s", args[0]);
1843         return ERROR_OK;
1844 }
1845
1846 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1847 {
1848         if (argc == 1)
1849         {
1850                 if (strcmp(args[0], "enable") == 0)
1851                 {
1852                         gdb_use_memory_map = 1;
1853                         return ERROR_OK;
1854                 }
1855                 else if (strcmp(args[0], "disable") == 0)
1856                 {
1857                         gdb_use_memory_map = 0;
1858                         return ERROR_OK;
1859                 }
1860         }
1861         
1862         WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
1863         return ERROR_OK;
1864 }
1865
1866 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1867 {
1868         if (argc == 1)
1869         {
1870                 if (strcmp(args[0], "enable") == 0)
1871                 {
1872                         gdb_flash_program = 1;
1873                         return ERROR_OK;
1874                 }
1875                 else if (strcmp(args[0], "disable") == 0)
1876                 {
1877                         gdb_flash_program = 0;
1878                         return ERROR_OK;
1879                 }
1880         }
1881         
1882         WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
1883         return ERROR_OK;
1884 }
1885
1886 int gdb_register_commands(command_context_t *command_context)
1887 {
1888         register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
1889                         COMMAND_CONFIG, "");
1890         register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
1891                         COMMAND_CONFIG, "");
1892         register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
1893                         COMMAND_CONFIG, "");
1894         register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
1895                         COMMAND_CONFIG, "");
1896         return ERROR_OK;
1897 }