54c9694243b7a5779171f0681f573c288c31db9e
[fw/openocd] / src / target / openrisc / jsp_server.c
1 /***************************************************************************
2  *   Copyright (C) 2014 by Franck Jullien                                  *
3  *   franck.jullien@gmail.com                                              *
4  *                                                                         *
5  *   Based on ./src/server/telnet_server.c                                 *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *   This program is distributed in the hope that it will be useful,       *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15  *   GNU General Public License for more details.                          *
16  *                                                                         *
17  *   You should have received a copy of the GNU General Public License     *
18  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
19  ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <server/telnet_server.h>
26
27 #include "or1k_tap.h"
28 #include "or1k_du.h"
29 #include "jsp_server.h"
30
31 static char *jsp_port;
32
33 /**A skim of the relevant RFCs suggests that if my application simply sent the
34  * characters IAC DONT LINEMODE (\377\376\042) as soon as the client connects,
35  * the client should be forced into character mode. However it doesn't make any difference.
36  */
37
38 static const char * const negotiate =
39         "\xFF\xFB\x03"                  /* IAC WILL Suppress Go Ahead */
40         "\xFF\xFB\x01"                  /* IAC WILL Echo */
41         "\xFF\xFD\x03"                  /* IAC DO Suppress Go Ahead */
42         "\xFF\xFE\x01";                 /* IAC DON'T Echo */
43
44 /* The only way we can detect that the socket is closed is the first time
45  * we write to it, we will fail. Subsequent write operations will
46  * succeed. Shudder!
47  */
48 static int telnet_write(struct connection *connection, const void *data, int len)
49 {
50         struct telnet_connection *t_con = connection->priv;
51         if (t_con->closed)
52                 return ERROR_SERVER_REMOTE_CLOSED;
53
54         if (connection_write(connection, data, len) == len)
55                 return ERROR_OK;
56         t_con->closed = 1;
57         return ERROR_SERVER_REMOTE_CLOSED;
58 }
59
60 static int jsp_poll_read(void *priv)
61 {
62         struct jsp_service *jsp_service = (struct jsp_service *)priv;
63         unsigned char out_buffer[10];
64         unsigned char in_buffer[10];
65         int out_len = 0;
66         int in_len;
67
68         if (!jsp_service->connection)
69                 return ERROR_FAIL;
70
71         memset(out_buffer, 0, 10);
72
73         or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info, &out_len, out_buffer, &in_len, in_buffer);
74         if (in_len)
75                 telnet_write(jsp_service->connection, in_buffer, in_len);
76
77         return ERROR_OK;
78 }
79
80 static int jsp_new_connection(struct connection *connection)
81 {
82         struct telnet_connection *telnet_connection = malloc(sizeof(struct telnet_connection));
83         struct jsp_service *jsp_service = connection->service->priv;
84
85         connection->priv = telnet_connection;
86
87         /* initialize telnet connection information */
88         telnet_connection->closed = 0;
89         telnet_connection->line_size = 0;
90         telnet_connection->line_cursor = 0;
91         telnet_connection->state = TELNET_STATE_DATA;
92
93         /* negotiate telnet options */
94         telnet_write(connection, negotiate, strlen(negotiate));
95
96         /* print connection banner */
97         if (jsp_service->banner) {
98                 telnet_write(connection, jsp_service->banner, strlen(jsp_service->banner));
99                 telnet_write(connection, "\r\n", 2);
100         }
101
102         jsp_service->connection = connection;
103
104         int retval = target_register_timer_callback(&jsp_poll_read, 1,
105                 TARGET_TIMER_TYPE_PERIODIC, jsp_service);
106         if (retval != ERROR_OK)
107                 return retval;
108
109         return ERROR_OK;
110 }
111
112 static int jsp_input(struct connection *connection)
113 {
114         int bytes_read;
115         unsigned char buffer[TELNET_BUFFER_SIZE];
116         unsigned char *buf_p;
117         struct telnet_connection *t_con = connection->priv;
118         struct jsp_service *jsp_service = connection->service->priv;
119
120         bytes_read = connection_read(connection, buffer, TELNET_BUFFER_SIZE);
121
122         if (bytes_read == 0)
123                 return ERROR_SERVER_REMOTE_CLOSED;
124         else if (bytes_read == -1) {
125                 LOG_ERROR("error during read: %s", strerror(errno));
126                 return ERROR_SERVER_REMOTE_CLOSED;
127         }
128
129         buf_p = buffer;
130         while (bytes_read) {
131                 switch (t_con->state) {
132                         case TELNET_STATE_DATA:
133                                 if (*buf_p == 0xff)
134                                         t_con->state = TELNET_STATE_IAC;
135                                 else {
136                                         int out_len = 1;
137                                         int in_len;
138                                         unsigned char in_buffer[10];
139                                         or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info,
140                                                                &out_len, buf_p, &in_len,
141                                                                in_buffer);
142                                         if (in_len)
143                                                 telnet_write(connection,
144                                                              in_buffer, in_len);
145                                 }
146                                 break;
147                         case TELNET_STATE_IAC:
148                                 switch (*buf_p) {
149                                 case 0xfe:
150                                         t_con->state = TELNET_STATE_DONT;
151                                         break;
152                                 case 0xfd:
153                                         t_con->state = TELNET_STATE_DO;
154                                         break;
155                                 case 0xfc:
156                                         t_con->state = TELNET_STATE_WONT;
157                                         break;
158                                 case 0xfb:
159                                         t_con->state = TELNET_STATE_WILL;
160                                         break;
161                                 }
162                                 break;
163                         case TELNET_STATE_SB:
164                                 break;
165                         case TELNET_STATE_SE:
166                                 break;
167                         case TELNET_STATE_WILL:
168                         case TELNET_STATE_WONT:
169                         case TELNET_STATE_DO:
170                         case TELNET_STATE_DONT:
171                                 t_con->state = TELNET_STATE_DATA;
172                                 break;
173                         default:
174                                 LOG_ERROR("unknown telnet state");
175                                 exit(-1);
176                 }
177
178                 bytes_read--;
179                 buf_p++;
180         }
181
182         return ERROR_OK;
183 }
184
185 static int jsp_connection_closed(struct connection *connection)
186 {
187         struct jsp_service *jsp_service = connection->service->priv;
188
189         int retval = target_unregister_timer_callback(&jsp_poll_read, jsp_service);
190         if (retval != ERROR_OK)
191                 return retval;
192
193         free(connection->priv);
194         connection->priv = NULL;
195         return ERROR_OK;
196 }
197
198 static const struct service_driver jsp_service_driver = {
199         .name = "jsp",
200         .new_connection_during_keep_alive_handler = NULL,
201         .new_connection_handler = jsp_new_connection,
202         .input_handler = jsp_input,
203         .connection_closed_handler = jsp_connection_closed,
204         .keep_client_alive_handler = NULL,
205 };
206
207 int jsp_init(struct or1k_jtag *jtag_info, char *banner)
208 {
209         struct jsp_service *jsp_service = malloc(sizeof(struct jsp_service));
210         jsp_service->banner = banner;
211         jsp_service->jtag_info = jtag_info;
212
213         return add_service(&jsp_service_driver, jsp_port, 1, jsp_service);
214 }
215
216 COMMAND_HANDLER(handle_jsp_port_command)
217 {
218         return CALL_COMMAND_HANDLER(server_pipe_command, &jsp_port);
219 }
220
221 static const struct command_registration jsp_command_handlers[] = {
222         {
223                 .name = "jsp_port",
224                 .handler = handle_jsp_port_command,
225                 .mode = COMMAND_ANY,
226                 .help = "Specify port on which to listen "
227                         "for incoming JSP telnet connections.",
228                 .usage = "[port_num]",
229         },
230         COMMAND_REGISTRATION_DONE
231 };
232
233 int jsp_register_commands(struct command_context *cmd_ctx)
234 {
235         jsp_port = strdup("7777");
236         return register_commands(cmd_ctx, NULL, jsp_command_handlers);
237 }
238
239 void jsp_service_free(void)
240 {
241         free(jsp_port);
242 }