smp: Fix byte order bug
[fw/openocd] / src / target / smp.c
1 /***************************************************************************
2  *                                                                         *
3  * Copyright (C) ST-Ericsson SA 2011                                       *
4  * Author: Michel Jaouen <michel.jaouen@stericsson.com> for ST-Ericsson.   *
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  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
19  ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "server/server.h"
26
27 #include "target/target.h"
28
29 #include "server/gdb_server.h"
30 #include "smp.h"
31 #include "helper/binarybuffer.h"
32
33 /*  implementation of new packet in gdb interface for smp feature          */
34 /*                                                                         */
35 /*   j : smp  status request                                               */
36 /*   J : smp  set request                                                  */
37 /*                                                                         */
38 /*   jc :read core id displayed by gdb connection                          */
39 /*   reply XXXXXXXX core id is int32_t , 8 hex digits                      */
40 /*                                                                         */
41 /*   Reply ENN error not supported (target not smp)                        */
42 /*                                                                         */
43 /*   JcXX  set core id displayed at next gdb continue                      */
44 /*   maximum 8 bytes described core id int32_t (8 hex digits)              */
45 /*  (core id -1 , reserved for returning to normal continue mode) */
46 /*  Reply ENN error not supported(target not smp,core id out of range)     */
47 /*  Reply OK : for success                                                 */
48 /*                                                                         */
49 /*  handling of this packet within gdb can be done by the creation         */
50 /*  internal variable by mean of function allocate_computed_value          */
51 /*  set $_core 1 => Jc01 packet is sent                                    */
52 /*  print $_core => jc packet is sent and result is affected in $          */
53 /*  Another way to test this packet is the usage of maintenance packet     */
54 /*  maint packet Jc01                                                      */
55 /*  maint packet jc                                                        */
56
57 /* packet j :smp status request */
58 int gdb_read_smp_packet(struct connection *connection,
59                 char *packet, int packet_size)
60 {
61         struct target *target = get_target_from_connection(connection);
62         int retval = ERROR_OK;
63         if (target->smp) {
64                 if (strncmp(packet, "jc", 2) == 0) {
65                         const uint32_t len = sizeof(target->gdb_service->core[0]);
66                         char hex_buffer[len * 2 + 1];
67                         char buffer[len];
68                         buf_set_u32(buffer, 0, len * 8, target->gdb_service->core[0]);
69                         int pkt_len = hexify(hex_buffer, buffer, sizeof(buffer), sizeof(hex_buffer));
70
71                         retval = gdb_put_packet(connection, hex_buffer, pkt_len);
72                 }
73         } else
74                 retval = gdb_put_packet(connection, "E01", 3);
75         return retval;
76 }
77
78 /* J :  smp set request */
79 int gdb_write_smp_packet(struct connection *connection,
80                 char *packet, int packet_size)
81 {
82         struct target *target = get_target_from_connection(connection);
83         char *separator;
84         int coreid = 0;
85         int retval = ERROR_OK;
86
87         /* skip command character */
88         if (target->smp) {
89                 if (strncmp(packet, "Jc", 2) == 0) {
90                         packet += 2;
91                         coreid = strtoul(packet, &separator, 16);
92                         target->gdb_service->core[1] = coreid;
93                         retval = gdb_put_packet(connection, "OK", 2);
94                 }
95         } else
96                 retval = gdb_put_packet(connection, "E01", 3);
97
98         return retval;
99 }