OpenCores
URL https://opencores.org/ocsvn/amber/amber/trunk

Subversion Repositories amber

[/] [amber/] [trunk/] [sw/] [boot-loader-ethmac/] [telnet.c] - Blame information for rev 80

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 61 csantifort
/*----------------------------------------------------------------
2
//                                                              //
3
//  boot-loader-ethmac.c                                        //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  The main functions for the boot loader application. This    //
10 78 csantifort
//  application is embedded in the FPGA's SRAM and is used      //
11 61 csantifort
//  to load larger applications into the DDR3 memory on         //
12
//  the development board.                                      //
13
//                                                              //
14
//  Author(s):                                                  //
15
//      - Conor Santifort, csantifort.amber@gmail.com           //
16
//                                                              //
17
//////////////////////////////////////////////////////////////////
18
//                                                              //
19
// Copyright (C) 2011 Authors and OPENCORES.ORG                 //
20
//                                                              //
21
// This source file may be used and distributed without         //
22
// restriction provided that this copyright statement is not    //
23
// removed from the file and that any derivative work contains  //
24
// the original copyright notice and the associated disclaimer. //
25
//                                                              //
26
// This source file is free software; you can redistribute it   //
27
// and/or modify it under the terms of the GNU Lesser General   //
28
// Public License as published by the Free Software Foundation; //
29
// either version 2.1 of the License, or (at your option) any   //
30
// later version.                                               //
31
//                                                              //
32
// This source is distributed in the hope that it will be       //
33
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
34
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
35
// PURPOSE.  See the GNU Lesser General Public License for more //
36
// details.                                                     //
37
//                                                              //
38
// You should have received a copy of the GNU Lesser General    //
39
// Public License along with this source; if not, download it   //
40
// from http://www.opencores.org/lgpl.shtml                     //
41
//                                                              //
42
----------------------------------------------------------------*/
43
 
44 80 csantifort
#include "address_map.h"
45 61 csantifort
#include "timer.h"
46
#include "line-buffer.h"
47
#include "packet.h"
48
#include "tcp.h"
49
#include "telnet.h"
50
 
51
void parse_telnet_options(char* buf, socket_t* socket)
52
{
53
    int     i;
54
    int     stage = 0;
55
    char    stage1;
56 78 csantifort
 
57 61 csantifort
    for (i=0;i<socket->rx_packet->tcp_payload_len;i++) {
58 78 csantifort
 
59 61 csantifort
        if (stage == 0) {
60
            switch (buf[i]) {
61
                case 241: stage = 0; break;  // NOP
62 78 csantifort
                case 255: stage = 1;
63 61 csantifort
                                 if (socket->telnet_connection_state == TELNET_CLOSED) {
64
                                     socket->telnet_connection_state = TELNET_OPEN;
65
                                    }
66
                         break;  // IAC
67 78 csantifort
 
68
                default:  if (buf[i] < 128)
69 61 csantifort
                    goto telnet_payload;
70
            }
71 78 csantifort
 
72 61 csantifort
        } else if (stage == 1) {
73
            stage1 = buf[i];
74
            switch (buf[i]) {
75
                case 241        : stage = 0; break;  // NOP
76
                case 250        : stage = 2; break;  // SB
77
                case TELNET_WILL: stage = 2; break;  // 0xfb WILL
78
                case TELNET_WONT: stage = 2; break;  // 0xfc WONT
79
                case TELNET_DO  : stage = 2; break;  // 0xfd DO
80
                case TELNET_DONT: stage = 2; break;  // 0xfe DONT
81
                default         : stage = 2; break;
82
            }
83 78 csantifort
 
84 61 csantifort
        } else {  // stage = 2
85 78 csantifort
            stage = 0;
86 61 csantifort
            switch (buf[i]) {
87
                case 1:   // echo
88
                    /* Client request that server echos stuff back to client */
89
                    if (stage1 == TELNET_DO)
90
                        socket->telnet_echo_mode = 1;
91
                    /* Client request that server does not echo stuff back to client */
92
                    else if (stage1 == TELNET_DONT)
93
                        socket->telnet_echo_mode = 0;
94
                    break;
95 78 csantifort
 
96 61 csantifort
                case 3:   break;  // suppress go ahead
97
                case 5:   break;  // status
98
                case 6:   break;  // time mark
99
                case 24:  break;  // terminal type
100
                case 31:  break;  // window size
101
                case 32:  break;  // terminal speed
102
                case 33:  break;  // remote flow control
103
                case 34:  break;  // linemode
104
                case 35:  break;  // X display location
105
                case 39:  break;  // New environmental variable option
106
                default:  break;
107
                }
108
            }
109
        }
110
 
111 78 csantifort
    return;
112
 
113 61 csantifort
    telnet_payload:
114
        socket->rx_packet->telnet_payload_len = socket->rx_packet->tcp_payload_len - i;
115
        parse_telnet_payload(&buf[i], socket);
116
}
117
 
118
 
119
void parse_telnet_payload(char * buf, socket_t* socket)
120
{
121
    int i;
122
    int cr = 0;
123
    int windows = 0;
124
    for (i=0;i<socket->rx_packet->telnet_payload_len;i++) {
125 78 csantifort
        if (buf[i] == '\n')
126 61 csantifort
            windows = 1;
127
        else if (buf[i] < 128 && buf[i] != 0) {
128
            /* end of a line */
129
            /* receive \r\n from Windows, \r from Linux */
130
            if (buf[i] == '\r') {
131
                cr=1;
132
                put_byte(socket->telnet_rxbuf, buf[i], 1); /* last byte of line */
133 78 csantifort
                }
134 61 csantifort
            else {
135
                put_byte(socket->telnet_rxbuf, buf[i], 0); /* not last byte of line */
136 78 csantifort
                }
137 61 csantifort
            }
138
        }
139 78 csantifort
 
140 61 csantifort
    if (socket->telnet_echo_mode) {
141
        if (cr && !windows) {
142
            buf[socket->rx_packet->telnet_payload_len] = '\n';
143
            socket->rx_packet->telnet_payload_len++;
144
            }
145
        tcp_reply(socket, buf, socket->rx_packet->telnet_payload_len);
146
        }
147
}
148
 
149
 
150
void telnet_options(socket_t* socket)
151
{
152
    char buf[3];
153
 
154
    // telnet options
155
    // Will echo - advertise that I have the ability to echo back commands to the client
156 78 csantifort
    buf[0] = 0xff; buf[1] = TELNET_WILL; buf[2] = 0x01;
157 61 csantifort
    tcp_reply(socket, buf, 3);
158
 
159
}
160
 
161
 
162
void telnet_tx(socket_t* socket, line_buf_t* txbuf)
163
{
164
    int line_len;
165
    int total_line_len;
166
    char* line;
167
    char* first_line;
168
 
169 78 csantifort
    /* Parse telnet tx buffer
170
       Grab as many lines as possible to stuff into a packet to transmit */
171
    line_len = get_line(txbuf, &first_line);
172
    if (line_len) {
173
        total_line_len = line_len;
174
        while (total_line_len < MAX_TELNET_TX && line_len) {
175
            line_len = get_line(txbuf, &line);
176
            total_line_len += line_len;
177
            }
178
        tcp_tx(socket, first_line, total_line_len);
179
        }
180 61 csantifort
}
181 78 csantifort
 
182 80 csantifort
 
183
void process_telnet(socket_t* socket)
184
{
185
    char* line;
186
 
187
    if (!socket->telnet_options_sent){
188
        telnet_options(socket);
189
        socket->telnet_options_sent = 1;
190
        }
191
 
192
    else {
193
        /* Send telnet greeting */
194
        if (!socket->telnet_sent_opening_message){
195
            put_line (socket->telnet_txbuf, "Amber Processor Boot Loader\r\n> ");
196
            socket->telnet_sent_opening_message = 1;
197
            }
198
 
199
        /* Parse telnet rx buffer */
200
        if (get_line(socket->telnet_rxbuf, &line))
201
            parse_command (socket, line);
202
 
203
        /* Transmit text from telnet tx buffer */
204
        telnet_tx(socket, socket->telnet_txbuf);
205
        }
206
}
207
 
208
 
209
 
210
/* Parse a command line passed from main and execute the command */
211
/* returns the length of the reply string */
212
int parse_command (socket_t* socket, char* line)
213
{
214
    unsigned int start_addr;
215
    unsigned int address;
216
    unsigned int range;
217
    int len, error = 0;
218
 
219
    /* All commands are just a single character.
220
       Just ignore anything else  */
221
    switch (line[0]) {
222
        /* Disconnect */
223
        case 'e':
224
        case 'x':
225
        case 'q':
226
            socket->tcp_disconnect = 1;
227
            return 0;
228
 
229
        case 'r': /* Read mem */
230
            {
231
            if (len = get_hex (&line[2], &start_addr)) {
232
                if (len = get_hex (&line[3+len], &range)) {
233
                    for (address=start_addr; address<start_addr+range; address+=4) {
234
                        put_line (socket->telnet_txbuf, "0x%08x 0x%08x\r\n",
235
                                    address, *(unsigned int *)address);
236
                        }
237
                    }
238
                else {
239
                    put_line (socket->telnet_txbuf, "0x%08x 0x%08x\r\n",
240
                                    start_addr, *(unsigned int *)start_addr);
241
                    }
242
                }
243
            else
244
                error=1;
245
            break;
246
            }
247
 
248
 
249
        case 'h': {/* Help */
250
            put_line (socket->telnet_txbuf, "You need help alright\r\n");
251
            break;
252
            }
253
 
254
 
255
        case 's': {/* Status */
256
            put_line (socket->telnet_txbuf, "Socket ID           %d\r\n", socket->id);
257
            put_line (socket->telnet_txbuf, "Packets received    %d\r\n", socket->packets_received);
258
            put_line (socket->telnet_txbuf, "Packets transmitted %d\r\n", socket->packets_sent);
259
            put_line (socket->telnet_txbuf, "Packets resent      %d\r\n", socket->packets_resent);
260
            put_line (socket->telnet_txbuf, "TCP checksum errors %d\r\n", tcp_checksum_errors_g);
261
 
262
            put_line (socket->telnet_txbuf, "Counterparty IP %d.%d.%d.%d\r\n",
263
                socket->rx_packet->src_ip[0],
264
                socket->rx_packet->src_ip[1],
265
                socket->rx_packet->src_ip[2],
266
                socket->rx_packet->src_ip[3]);
267
 
268
            put_line (socket->telnet_txbuf, "Counterparty Port %d\r\n",
269
                socket->rx_packet->tcp_src_port);
270
 
271
            put_line (socket->telnet_txbuf, "Malloc pointer 0x%08x\r\n",
272
                *(unsigned int *)(ADR_MALLOC_POINTER));
273
            put_line (socket->telnet_txbuf, "Malloc count %d\r\n",
274
                *(unsigned int *)(ADR_MALLOC_COUNT));
275
            put_line (socket->telnet_txbuf, "Uptime %d seconds\r\n", current_time_g->seconds);
276
            break;
277
            }
278
 
279
 
280
        default: {
281
            error=1; break;
282
            }
283
        }
284
 
285
 
286
    if (error)
287
            put_line (socket->telnet_txbuf, "You're not making any sense\r\n",
288
                        line[0], line[1], line[2]);
289
 
290
    put_line (socket->telnet_txbuf, "> ");
291
    return 0;
292
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.