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