Line 39... |
Line 39... |
// Public License along with this source; if not, download it //
|
// Public License along with this source; if not, download it //
|
// from http://www.opencores.org/lgpl.shtml //
|
// from http://www.opencores.org/lgpl.shtml //
|
// //
|
// //
|
----------------------------------------------------------------*/
|
----------------------------------------------------------------*/
|
|
|
#include "amber_registers.h"
|
|
#include "address_map.h"
|
|
#include "line-buffer.h"
|
#include "line-buffer.h"
|
#include "timer.h"
|
#include "timer.h"
|
#include "utilities.h"
|
#include "utilities.h"
|
#include "ethmac.h"
|
|
#include "packet.h"
|
#include "packet.h"
|
|
#include "ethmac.h"
|
#include "tcp.h"
|
#include "tcp.h"
|
#include "udp.h"
|
#include "udp.h"
|
#include "telnet.h"
|
#include "telnet.h"
|
#include "serial.h"
|
#include "serial.h"
|
|
|
#include "elfsplitter.h"
|
|
#include "boot-loader-ethmac.h"
|
|
|
|
|
|
|
|
int main ( void ) {
|
int main ( void ) {
|
char* line;
|
socket_t* socket;
|
time_t* led_flash_timer;
|
|
time_t* reboot_timer;
|
|
socket_t* socket = socket0_g;
|
|
int reboot_stage = 0;
|
|
|
|
|
|
/* Enable the serial debug port */
|
/* Enable the serial debug port */
|
init_serial();
|
init_serial();
|
print_serial("Amber debug port\n\r");
|
print_serial("Amber debug port\n\r");
|
|
|
|
|
/* Turn off all LEDs */
|
|
led_clear();
|
|
|
|
|
|
/* initialize the memory allocation system */
|
/* initialize the memory allocation system */
|
init_malloc();
|
init_malloc();
|
|
|
|
/* Enable the hardware timer to generate interrrupts 100 timer per second,
|
|
current time will start incrementing from this point onwards */
|
|
init_timer();
|
|
|
/* Initialize current time and some timers */
|
/* Create a timer to flash a led periodically */
|
init_current_time();
|
init_led();
|
led_flash_timer = init_timer();
|
|
set_timer(led_flash_timer, 500);
|
|
reboot_timer = init_timer();
|
|
|
|
|
|
/* receive packet buffer */
|
|
rx_packet_g = malloc(sizeof(packet_t));
|
|
|
|
|
/* initialize the tftp stuff */
|
|
init_tftp();
|
|
|
/* initialize two tcp sockets */
|
|
socket0_g = init_socket(0);
|
|
socket1_g = init_socket(1);
|
|
|
|
|
/* create a tcp socket for listening */
|
|
first_socket_g = new_socket(NULL);
|
|
socket = first_socket_g;
|
|
|
/* open ethernet port and wait for connection requests
|
/* initialize the PHY and MAC and listen for connections
|
keep trying forever */
|
This is the last init because packets will be received from this point
|
while (!open_link());
|
onwards. */
|
|
init_ethmac();
|
|
|
|
|
/* infinite loop. Everything is timer, interrupt and queue driven from here on down */
|
/* Process loop. Everything is timer, interrupt and queue driven from here on down */
|
while (1) {
|
while (1) {
|
|
|
/* Flash a heartbeat LED */
|
/* flash an led */
|
if (timer_expired(led_flash_timer)) {
|
process_led();
|
led_flip(0);
|
|
set_timer(led_flash_timer, 500);
|
|
}
|
|
|
|
|
|
/* Check for newly downloaded tftp file. Add to all tx buffers */
|
|
/* Has a file been uploaded via tftp ? */
|
|
if (udp_file_g != NULL) {
|
|
/* Notify telnet clients that file has been received */
|
|
if (udp_file_g->ready) {
|
|
udp_file_g->ready = 0;
|
|
|
|
print_serial("Received file %s, %d bytes",
|
|
udp_file_g->filename, udp_file_g->total_bytes);
|
|
if (udp_file_g->linux_boot)
|
|
print_serial(", linux image detected\r\n");
|
|
else
|
|
print_serial("\r\n");
|
|
|
|
if (process_file(socket0_g) == 0) {
|
|
/* Disconnect in 1 second */
|
|
set_timer(reboot_timer, 1000);
|
|
}
|
|
else
|
|
print_serial("Not an elf file\r\n");
|
|
}
|
|
}
|
|
|
|
|
|
/* reboot timer expired */
|
/* Check for received tftp files and reboot */
|
if (timer_expired(reboot_timer)) {
|
process_tftp();
|
/* First stage of reboot sequence is to nicely disconnect */
|
|
if (reboot_stage == 0) {
|
|
set_timer(reboot_timer, 1000);
|
|
reboot_stage = 1;
|
|
socket0_g->tcp_disconnect = 1;
|
|
socket1_g->tcp_disconnect = 1;
|
|
}
|
|
else {
|
|
/* Second stage of reboot sequence is to turn off ethmac and then jump to restart vector */
|
|
close_link();
|
|
reboot();
|
|
}
|
|
}
|
|
|
|
|
/* handle tcp connections and process buffers */
|
/* Poll both sockets in turn for activity */
|
/* Poll all sockets in turn for activity */
|
if (socket == socket0_g)
|
if (socket->next == NULL)
|
socket = socket1_g;
|
socket = first_socket_g;
|
else
|
else
|
socket = socket0_g;
|
socket = socket->next;
|
|
|
|
|
/* Check if any tcp packets need to be re-transmitted */
|
|
tcp_retransmit(socket);
|
|
|
|
|
|
/* Handle exit command */
|
process_tcp(socket);
|
if (socket->tcp_disconnect && socket->tcp_connection_state == TCP_OPEN) {
|
|
tcp_disconnect(socket);
|
|
}
|
}
|
|
|
|
|
/* Reset connection */
|
|
if (socket->tcp_reset) {
|
|
socket->tcp_connection_state = TCP_CLOSED;
|
|
socket->telnet_connection_state = TELNET_CLOSED;
|
|
socket->telnet_options_sent = 0;
|
|
tcp_reply(socket, NULL, 0);
|
|
socket->tcp_reset = 0;
|
|
}
|
}
|
|
|
|
|
/* Send telnet options */
|
|
if (socket->tcp_connection_state == TCP_OPEN && !socket->telnet_options_sent){
|
|
telnet_options(socket);
|
|
socket->telnet_options_sent = 1;
|
|
}
|
|
|
|
/* telnet connection open
|
|
Communicate with client */
|
|
else if (socket->telnet_connection_state == TELNET_OPEN) {
|
|
/* Send telnet greeting */
|
|
if (!socket->telnet_sent_opening_message){
|
|
put_line (socket->telnet_txbuf, "Amber Processor Boot Loader\r\n> ");
|
|
socket->telnet_sent_opening_message = 1;
|
|
}
|
|
|
|
/* Parse telnet rx buffer */
|
|
if (get_line(socket->telnet_rxbuf, &line))
|
|
parse_command (socket, line);
|
|
|
|
/* Transmit text from telnet tx buffer */
|
|
telnet_tx(socket, socket->telnet_txbuf);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* Parse a command line passed from main and execute the command */
|
|
/* returns the length of the reply string */
|
|
int parse_command (socket_t* socket, char* line)
|
|
{
|
|
unsigned int start_addr;
|
|
unsigned int address;
|
|
unsigned int range;
|
|
int len, error = 0;
|
|
|
|
/* All commands are just a single character.
|
|
Just ignore anything else */
|
|
switch (line[0]) {
|
|
/* Disconnect */
|
|
case 'e':
|
|
case 'x':
|
|
case 'q':
|
|
socket->tcp_disconnect = 1;
|
|
return 0;
|
|
|
|
case 'r': /* Read mem */
|
|
{
|
|
if (len = get_hex (&line[2], &start_addr)) {
|
|
if (len = get_hex (&line[3+len], &range)) {
|
|
for (address=start_addr; address<start_addr+range; address+=4) {
|
|
put_line (socket->telnet_txbuf, "0x%08x 0x%08x\r\n",
|
|
address, *(unsigned int *)address);
|
|
}
|
|
}
|
|
else {
|
|
put_line (socket->telnet_txbuf, "0x%08x 0x%08x\r\n",
|
|
start_addr, *(unsigned int *)start_addr);
|
|
}
|
|
}
|
|
else
|
|
error=1;
|
|
break;
|
|
}
|
|
|
|
|
|
case 'h': {/* Help */
|
|
put_line (socket->telnet_txbuf, "You need help alright\r\n");
|
|
break;
|
|
}
|
|
|
|
|
|
case 's': {/* Status */
|
|
put_line (socket->telnet_txbuf, "Socket ID %d\r\n", socket->id);
|
|
put_line (socket->telnet_txbuf, "Packets received %d\r\n", socket->packets_received);
|
|
put_line (socket->telnet_txbuf, "Packets transmitted %d\r\n", socket->packets_sent);
|
|
put_line (socket->telnet_txbuf, "Packets resent %d\r\n", socket->packets_resent);
|
|
put_line (socket->telnet_txbuf, "TCP checksum errors %d\r\n", tcp_checksum_errors_g);
|
|
|
|
put_line (socket->telnet_txbuf, "Counterparty IP %d.%d.%d.%d\r\n",
|
|
socket->rx_packet->src_ip[0],
|
|
socket->rx_packet->src_ip[1],
|
|
socket->rx_packet->src_ip[2],
|
|
socket->rx_packet->src_ip[3]);
|
|
|
|
put_line (socket->telnet_txbuf, "Counterparty Port %d\r\n",
|
|
socket->rx_packet->tcp_src_port);
|
|
|
|
put_line (socket->telnet_txbuf, "Malloc pointer 0x%08x\r\n",
|
|
*(unsigned int *)(ADR_MALLOC_POINTER));
|
|
put_line (socket->telnet_txbuf, "Malloc count %d\r\n",
|
|
*(unsigned int *)(ADR_MALLOC_COUNT));
|
|
put_line (socket->telnet_txbuf, "Uptime %d seconds\r\n", current_time_g->seconds);
|
|
break;
|
|
}
|
|
|
|
|
|
default: {
|
|
error=1; break;
|
|
}
|
|
}
|
|
|
|
|
|
if (error)
|
|
put_line (socket->telnet_txbuf, "You're not making any sense\r\n",
|
|
line[0], line[1], line[2]);
|
|
|
|
put_line (socket->telnet_txbuf, "> ");
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* copy tftp file into a single contiguous buffer so
|
|
if can be processed by elf splitter */
|
|
int process_file(socket_t* socket)
|
|
{
|
|
block_t* block;
|
|
char* buf512;
|
|
char* tftp_file;
|
|
char* line;
|
|
int line_len;
|
|
int ret;
|
|
|
|
tftp_file = malloc(udp_file_g->total_bytes);
|
|
|
|
block = udp_file_g;
|
|
buf512= tftp_file;
|
|
|
|
while (block->next) {
|
|
memcpy(buf512, block->buf512, block->bytes);
|
|
buf512=&buf512[512];
|
|
block=block->next;
|
|
}
|
|
memcpy(buf512, block->buf512, block->bytes);
|
|
buf512=&buf512[512];
|
|
|
|
return elfsplitter(tftp_file, socket);
|
|
}
|
|
|
|
|
|
/* Disable interrupts
|
|
Load new values into the interrupt vector memory space
|
|
Jump to address 0
|
|
*/
|
|
void reboot()
|
|
{
|
|
int i;
|
|
|
|
/* Disable all interrupts */
|
|
/* Disable ethmac_int interrupt */
|
|
/* Disable timer 0 interrupt in interrupt controller */
|
|
*(unsigned int *) ( ADR_AMBER_IC_IRQ0_ENABLECLR ) = 0x120;
|
|
|
|
for(i=0;i<MEM_BUF_ENTRIES;i++)
|
|
if (elf_mem0_g->entry[i].valid)
|
|
*(char *)(i) = elf_mem0_g->entry[i].data;
|
|
|
|
if (udp_file_g->linux_boot) {
|
|
print_serial("linux reboot\n\r");
|
|
_jump_to_program(LINUX_JUMP_ADR);
|
|
}
|
|
else {
|
|
print_serial("normal reboot\n\r");
|
|
_restart();
|
|
}
|
|
}
|
|
|
|
|
|
No newline at end of file
|
No newline at end of file
|