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

Subversion Repositories amber

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

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
#include "amber_registers.h"
45
#include "address_map.h"
46
#include "line-buffer.h"
47
#include "timer.h"
48
#include "utilities.h"
49
#include "ethmac.h"
50
#include "packet.h"
51
#include "tcp.h"
52
#include "udp.h"
53
#include "telnet.h"
54 78 csantifort
#include "serial.h"
55 61 csantifort
 
56
#include "elfsplitter.h"
57
#include "boot-loader-ethmac.h"
58
 
59
 
60 78 csantifort
 
61 61 csantifort
int main ( void ) {
62
    char* line;
63
    time_t* led_flash_timer;
64
    time_t* reboot_timer;
65
    socket_t* socket = socket0_g;
66
    int reboot_stage = 0;
67 78 csantifort
 
68
 
69
    /* Enable the serial debug port */
70
    init_serial();
71
    print_serial("Amber debug port\n\r");
72
 
73
 
74
    /* Turn off all LEDs */
75 61 csantifort
    led_clear();
76
 
77 78 csantifort
 
78
    /* initialize the memory allocation system */
79 61 csantifort
    init_malloc();
80 78 csantifort
 
81
 
82 61 csantifort
    /* Initialize current time and some timers */
83
    init_current_time();
84
    led_flash_timer = init_timer();
85 78 csantifort
    set_timer(led_flash_timer, 500);
86 61 csantifort
    reboot_timer = init_timer();
87 78 csantifort
 
88
 
89 61 csantifort
    /* receive packet buffer */
90
    rx_packet_g = malloc(sizeof(packet_t));
91 78 csantifort
 
92
 
93
    /* initialize two tcp sockets */
94 61 csantifort
    socket0_g = init_socket(0);
95
    socket1_g = init_socket(1);
96 78 csantifort
 
97
 
98
    /* open ethernet port and wait for connection requests
99 61 csantifort
       keep trying forever */
100
    while (!open_link());
101 78 csantifort
 
102
 
103 61 csantifort
    /* infinite loop. Everything is timer, interrupt and queue driven from here on down */
104
    while (1) {
105 78 csantifort
 
106 61 csantifort
        /* Flash a heartbeat LED */
107
        if (timer_expired(led_flash_timer)) {
108
            led_flip(0);
109
            set_timer(led_flash_timer, 500);
110
            }
111 78 csantifort
 
112
 
113 61 csantifort
        /* Check for newly downloaded tftp file. Add to all tx buffers */
114
        /* Has a file been uploaded via tftp ? */
115
        if (udp_file_g != NULL) {
116
            /* Notify telnet clients that file has been received */
117
            if (udp_file_g->ready) {
118
                udp_file_g->ready = 0;
119 78 csantifort
 
120
                print_serial("Received file %s, %d bytes",
121
                    udp_file_g->filename, udp_file_g->total_bytes);
122
                if (udp_file_g->linux_boot)
123
                    print_serial(", linux image detected\r\n");
124
                else
125
                    print_serial("\r\n");
126
 
127
                if (process_file(socket0_g) == 0) {
128 61 csantifort
                    /* Disconnect in 1 second */
129
                    set_timer(reboot_timer, 1000);
130 78 csantifort
                    }
131 61 csantifort
                else
132 78 csantifort
                    print_serial("Not an elf file\r\n");
133 61 csantifort
                }
134
            }
135 78 csantifort
 
136
 
137 61 csantifort
        /* reboot timer expired */
138
        if (timer_expired(reboot_timer)) {
139
            /* First stage of reboot sequence is to nicely disconnect */
140
            if (reboot_stage == 0) {
141
                set_timer(reboot_timer, 1000);
142
                reboot_stage = 1;
143
                socket0_g->tcp_disconnect = 1;
144
                socket1_g->tcp_disconnect = 1;
145 78 csantifort
                }
146 61 csantifort
            else {
147
            /* Second stage of reboot sequence is to turn off ethmac and then jump to restart vector */
148
                close_link();
149
                reboot();
150
                }
151
            }
152
 
153
 
154
        /* Poll both sockets in turn for activity */
155
        if (socket == socket0_g)
156
            socket = socket1_g;
157
        else
158
            socket = socket0_g;
159 78 csantifort
 
160
 
161 61 csantifort
        /* Check if any tcp packets need to be re-transmitted */
162
        tcp_retransmit(socket);
163
 
164
 
165
        /* Handle exit command */
166
        if (socket->tcp_disconnect && socket->tcp_connection_state == TCP_OPEN) {
167
            tcp_disconnect(socket);
168
            }
169
 
170 78 csantifort
 
171 61 csantifort
        /* Reset connection */
172
        if (socket->tcp_reset) {
173
            socket->tcp_connection_state = TCP_CLOSED;
174
            socket->telnet_connection_state = TELNET_CLOSED;
175
            socket->telnet_options_sent = 0;
176
            tcp_reply(socket, NULL, 0);
177
            socket->tcp_reset = 0;
178
            }
179 78 csantifort
 
180
 
181
        /* Send telnet options */
182 61 csantifort
        if (socket->tcp_connection_state == TCP_OPEN && !socket->telnet_options_sent){
183
            telnet_options(socket);
184
            socket->telnet_options_sent = 1;
185
            }
186 78 csantifort
 
187
        /* telnet connection open
188 61 csantifort
           Communicate with client */
189
        else if (socket->telnet_connection_state == TELNET_OPEN) {
190
            /* Send telnet greeting */
191
            if (!socket->telnet_sent_opening_message){
192
                put_line (socket->telnet_txbuf, "Amber Processor Boot Loader\r\n> ");
193
                socket->telnet_sent_opening_message = 1;
194
                }
195 78 csantifort
 
196 61 csantifort
            /* Parse telnet rx buffer */
197 78 csantifort
            if (get_line(socket->telnet_rxbuf, &line))
198 61 csantifort
                parse_command (socket, line);
199
 
200
            /* Transmit text from telnet tx buffer */
201
            telnet_tx(socket, socket->telnet_txbuf);
202
        }
203
    }
204
}
205
 
206
 
207
 
208
/* Parse a command line passed from main and execute the command */
209
/* returns the length of the reply string */
210 78 csantifort
int parse_command (socket_t* socket, char* line)
211
{
212 61 csantifort
    unsigned int start_addr;
213
    unsigned int address;
214
    unsigned int range;
215
    int len, error = 0;
216 78 csantifort
 
217 61 csantifort
    /* All commands are just a single character.
218
       Just ignore anything else  */
219
    switch (line[0]) {
220
        /* Disconnect */
221
        case 'e':
222
        case 'x':
223 78 csantifort
        case 'q':
224 61 csantifort
            socket->tcp_disconnect = 1;
225
            return 0;
226 78 csantifort
 
227
        case 'r': /* Read mem */
228 61 csantifort
            {
229 78 csantifort
            if (len = get_hex (&line[2], &start_addr)) {
230 61 csantifort
                if (len = get_hex (&line[3+len], &range)) {
231
                    for (address=start_addr; address<start_addr+range; address+=4) {
232 78 csantifort
                        put_line (socket->telnet_txbuf, "0x%08x 0x%08x\r\n",
233 61 csantifort
                                    address, *(unsigned int *)address);
234
                        }
235
                    }
236
                else {
237 78 csantifort
                    put_line (socket->telnet_txbuf, "0x%08x 0x%08x\r\n",
238 61 csantifort
                                    start_addr, *(unsigned int *)start_addr);
239
                    }
240
                }
241 78 csantifort
            else
242 61 csantifort
                error=1;
243 78 csantifort
            break;
244 61 csantifort
            }
245
 
246 78 csantifort
 
247
        case 'h': {/* Help */
248 61 csantifort
            put_line (socket->telnet_txbuf, "You need help alright\r\n");
249 78 csantifort
            break;
250 61 csantifort
            }
251
 
252 78 csantifort
 
253
        case 's': {/* Status */
254 61 csantifort
            put_line (socket->telnet_txbuf, "Socket ID           %d\r\n", socket->id);
255
            put_line (socket->telnet_txbuf, "Packets received    %d\r\n", socket->packets_received);
256
            put_line (socket->telnet_txbuf, "Packets transmitted %d\r\n", socket->packets_sent);
257
            put_line (socket->telnet_txbuf, "Packets resent      %d\r\n", socket->packets_resent);
258
            put_line (socket->telnet_txbuf, "TCP checksum errors %d\r\n", tcp_checksum_errors_g);
259 78 csantifort
 
260 61 csantifort
            put_line (socket->telnet_txbuf, "Counterparty IP %d.%d.%d.%d\r\n",
261
                socket->rx_packet->src_ip[0],
262
                socket->rx_packet->src_ip[1],
263
                socket->rx_packet->src_ip[2],
264
                socket->rx_packet->src_ip[3]);
265 78 csantifort
 
266 61 csantifort
            put_line (socket->telnet_txbuf, "Counterparty Port %d\r\n",
267
                socket->rx_packet->tcp_src_port);
268 78 csantifort
 
269 61 csantifort
            put_line (socket->telnet_txbuf, "Malloc pointer 0x%08x\r\n",
270
                *(unsigned int *)(ADR_MALLOC_POINTER));
271
            put_line (socket->telnet_txbuf, "Malloc count %d\r\n",
272
                *(unsigned int *)(ADR_MALLOC_COUNT));
273
            put_line (socket->telnet_txbuf, "Uptime %d seconds\r\n", current_time_g->seconds);
274
            break;
275
            }
276 78 csantifort
 
277
 
278 61 csantifort
        default: {
279
            error=1; break;
280 78 csantifort
            }
281 61 csantifort
        }
282 78 csantifort
 
283
 
284 61 csantifort
    if (error)
285 78 csantifort
            put_line (socket->telnet_txbuf, "You're not making any sense\r\n",
286 61 csantifort
                        line[0], line[1], line[2]);
287 78 csantifort
 
288 61 csantifort
    put_line (socket->telnet_txbuf, "> ");
289
    return 0;
290
}
291
 
292
 
293
/* copy tftp file into a single contiguous buffer so
294
   if can be processed by elf splitter */
295
int process_file(socket_t* socket)
296 78 csantifort
{
297 61 csantifort
    block_t* block;
298
    char*    buf512;
299
    char*    tftp_file;
300
    char*    line;
301
    int      line_len;
302
    int      ret;
303 78 csantifort
 
304 61 csantifort
    tftp_file = malloc(udp_file_g->total_bytes);
305 78 csantifort
 
306 61 csantifort
    block = udp_file_g;
307
    buf512= tftp_file;
308 78 csantifort
 
309 61 csantifort
    while (block->next) {
310
        memcpy(buf512, block->buf512, block->bytes);
311
        buf512=&buf512[512];
312
        block=block->next;
313
        }
314
    memcpy(buf512, block->buf512, block->bytes);
315
    buf512=&buf512[512];
316 78 csantifort
 
317 61 csantifort
    return elfsplitter(tftp_file, socket);
318
}
319
 
320
 
321
/* Disable interrupts
322
   Load new values into the interrupt vector memory space
323
   Jump to address 0
324
*/
325
void reboot()
326
{
327
   int i;
328 78 csantifort
 
329 61 csantifort
   /* Disable all interrupts */
330 78 csantifort
   /* Disable ethmac_int interrupt */
331 61 csantifort
   /* Disable timer 0 interrupt in interrupt controller */
332
   *(unsigned int *) ( ADR_AMBER_IC_IRQ0_ENABLECLR ) = 0x120;
333
 
334
   for(i=0;i<MEM_BUF_ENTRIES;i++)
335
       if (elf_mem0_g->entry[i].valid)
336
           *(char *)(i) = elf_mem0_g->entry[i].data;
337 78 csantifort
 
338
   if (udp_file_g->linux_boot) {
339
        print_serial("linux reboot\n\r");
340
        _jump_to_program(LINUX_JUMP_ADR);
341
        }
342
   else {
343
        print_serial("normal reboot\n\r");
344
        _restart();
345
        }
346 61 csantifort
}
347
 

powered by: WebSVN 2.1.0

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