/*----------------------------------------------------------------
|
/*----------------------------------------------------------------
|
// //
|
// //
|
// boot-loader-ethmac.c //
|
// boot-loader-ethmac.c //
|
// //
|
// //
|
// This file is part of the Amber project //
|
// This file is part of the Amber project //
|
// http://www.opencores.org/project,amber //
|
// http://www.opencores.org/project,amber //
|
// //
|
// //
|
// Description //
|
// Description //
|
// The main functions for the boot loader application. This //
|
// The main functions for the boot loader application. This //
|
// application is embedded in the FPGA's SRAM and is used //
|
// application is embedded in the FPGA's SRAM and is used //
|
// to load larger applications into the DDR3 memory on //
|
// to load larger applications into the DDR3 memory on //
|
// the development board. //
|
// the development board. //
|
// //
|
// //
|
// Author(s): //
|
// Author(s): //
|
// - Conor Santifort, csantifort.amber@gmail.com //
|
// - Conor Santifort, csantifort.amber@gmail.com //
|
// //
|
// //
|
//////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////
|
// //
|
// //
|
// Copyright (C) 2011 Authors and OPENCORES.ORG //
|
// Copyright (C) 2011 Authors and OPENCORES.ORG //
|
// //
|
// //
|
// This source file may be used and distributed without //
|
// This source file may be used and distributed without //
|
// restriction provided that this copyright statement is not //
|
// restriction provided that this copyright statement is not //
|
// removed from the file and that any derivative work contains //
|
// removed from the file and that any derivative work contains //
|
// the original copyright notice and the associated disclaimer. //
|
// the original copyright notice and the associated disclaimer. //
|
// //
|
// //
|
// This source file is free software; you can redistribute it //
|
// This source file is free software; you can redistribute it //
|
// and/or modify it under the terms of the GNU Lesser General //
|
// and/or modify it under the terms of the GNU Lesser General //
|
// Public License as published by the Free Software Foundation; //
|
// Public License as published by the Free Software Foundation; //
|
// either version 2.1 of the License, or (at your option) any //
|
// either version 2.1 of the License, or (at your option) any //
|
// later version. //
|
// later version. //
|
// //
|
// //
|
// This source is distributed in the hope that it will be //
|
// This source is distributed in the hope that it will be //
|
// useful, but WITHOUT ANY WARRANTY; without even the implied //
|
// useful, but WITHOUT ANY WARRANTY; without even the implied //
|
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
|
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
|
// PURPOSE. See the GNU Lesser General Public License for more //
|
// PURPOSE. See the GNU Lesser General Public License for more //
|
// details. //
|
// details. //
|
// //
|
// //
|
// You should have received a copy of the GNU Lesser General //
|
// You should have received a copy of the GNU Lesser General //
|
// 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 //
|
// //
|
// //
|
----------------------------------------------------------------*/
|
----------------------------------------------------------------*/
|
|
|
// TODO list
|
|
// tcp.c clean up
|
|
// Cleanup self_g structure and usage
|
|
// tcp window - what is it, whats it set to? Add it to status stuff
|
|
// Get A25 version working
|
|
// test with booting linux
|
|
|
|
#include "amber_registers.h"
|
#include "amber_registers.h"
|
#include "address_map.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 "ethmac.h"
|
#include "packet.h"
|
#include "packet.h"
|
#include "tcp.h"
|
#include "tcp.h"
|
#include "udp.h"
|
#include "udp.h"
|
#include "telnet.h"
|
#include "telnet.h"
|
|
#include "serial.h"
|
|
|
#include "elfsplitter.h"
|
#include "elfsplitter.h"
|
#include "boot-loader-ethmac.h"
|
#include "boot-loader-ethmac.h"
|
|
|
|
|
|
|
int main ( void ) {
|
int main ( void ) {
|
char* line;
|
char* line;
|
time_t* led_flash_timer;
|
time_t* led_flash_timer;
|
time_t* reboot_timer;
|
time_t* reboot_timer;
|
socket_t* socket = socket0_g;
|
socket_t* socket = socket0_g;
|
int reboot_stage = 0;
|
int reboot_stage = 0;
|
|
|
/* Turn off all LEDs as a starting point */
|
|
|
/* Enable the serial debug port */
|
|
init_serial();
|
|
print_serial("Amber debug port\n\r");
|
|
|
|
|
|
/* Turn off all LEDs */
|
led_clear();
|
led_clear();
|
|
|
/* this must be first, because all the other init functions call malloc */
|
|
|
/* initialize the memory allocation system */
|
init_malloc();
|
init_malloc();
|
|
|
|
|
/* Initialize current time and some timers */
|
/* Initialize current time and some timers */
|
init_current_time();
|
init_current_time();
|
led_flash_timer = init_timer();
|
led_flash_timer = init_timer();
|
set_timer(led_flash_timer, 500);
|
set_timer(led_flash_timer, 500);
|
reboot_timer = init_timer();
|
reboot_timer = init_timer();
|
|
|
|
|
/* receive packet buffer */
|
/* receive packet buffer */
|
rx_packet_g = malloc(sizeof(packet_t));
|
rx_packet_g = malloc(sizeof(packet_t));
|
|
|
/* socket init */
|
|
|
/* initialize two tcp sockets */
|
socket0_g = init_socket(0);
|
socket0_g = init_socket(0);
|
socket1_g = init_socket(1);
|
socket1_g = init_socket(1);
|
|
|
|
|
/* open ethernet port and wait for connection requests
|
/* open ethernet port and wait for connection requests
|
keep trying forever */
|
keep trying forever */
|
while (!open_link());
|
while (!open_link());
|
|
|
|
|
/* infinite loop. Everything is timer, interrupt and queue driven from here on down */
|
/* infinite loop. Everything is timer, interrupt and queue driven from here on down */
|
while (1) {
|
while (1) {
|
|
|
/* Flash a heartbeat LED */
|
/* Flash a heartbeat LED */
|
if (timer_expired(led_flash_timer)) {
|
if (timer_expired(led_flash_timer)) {
|
led_flip(0);
|
led_flip(0);
|
set_timer(led_flash_timer, 500);
|
set_timer(led_flash_timer, 500);
|
}
|
}
|
|
|
|
|
/* Check for newly downloaded tftp file. Add to all tx buffers */
|
/* Check for newly downloaded tftp file. Add to all tx buffers */
|
/* Has a file been uploaded via tftp ? */
|
/* Has a file been uploaded via tftp ? */
|
if (udp_file_g != NULL) {
|
if (udp_file_g != NULL) {
|
/* Notify telnet clients that file has been received */
|
/* Notify telnet clients that file has been received */
|
if (udp_file_g->ready) {
|
if (udp_file_g->ready) {
|
udp_file_g->ready = 0;
|
udp_file_g->ready = 0;
|
telnet_broadcast("Received file %s, %d bytes, linux %d\r\n",
|
|
udp_file_g->filename, udp_file_g->total_bytes, udp_file_g->linux_boot);
|
print_serial("Received file %s, %d bytes",
|
if (process_file(socket0_g) == 0)
|
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 */
|
/* Disconnect in 1 second */
|
set_timer(reboot_timer, 1000);
|
set_timer(reboot_timer, 1000);
|
|
}
|
else
|
else
|
telnet_broadcast("Not an elf file\r\n");
|
print_serial("Not an elf file\r\n");
|
}
|
}
|
}
|
}
|
|
|
|
|
/* reboot timer expired */
|
/* reboot timer expired */
|
if (timer_expired(reboot_timer)) {
|
if (timer_expired(reboot_timer)) {
|
/* First stage of reboot sequence is to nicely disconnect */
|
/* First stage of reboot sequence is to nicely disconnect */
|
if (reboot_stage == 0) {
|
if (reboot_stage == 0) {
|
set_timer(reboot_timer, 1000);
|
set_timer(reboot_timer, 1000);
|
reboot_stage = 1;
|
reboot_stage = 1;
|
socket0_g->tcp_disconnect = 1;
|
socket0_g->tcp_disconnect = 1;
|
socket1_g->tcp_disconnect = 1;
|
socket1_g->tcp_disconnect = 1;
|
}
|
}
|
else {
|
else {
|
/* Second stage of reboot sequence is to turn off ethmac and then jump to restart vector */
|
/* Second stage of reboot sequence is to turn off ethmac and then jump to restart vector */
|
close_link();
|
close_link();
|
reboot();
|
reboot();
|
}
|
}
|
}
|
}
|
|
|
|
|
/* Poll both sockets in turn for activity */
|
/* Poll both sockets in turn for activity */
|
if (socket == socket0_g)
|
if (socket == socket0_g)
|
socket = socket1_g;
|
socket = socket1_g;
|
else
|
else
|
socket = socket0_g;
|
socket = socket0_g;
|
|
|
|
|
/* Check if any tcp packets need to be re-transmitted */
|
/* Check if any tcp packets need to be re-transmitted */
|
tcp_retransmit(socket);
|
tcp_retransmit(socket);
|
|
|
|
|
/* Handle exit command */
|
/* Handle exit command */
|
if (socket->tcp_disconnect && socket->tcp_connection_state == TCP_OPEN) {
|
if (socket->tcp_disconnect && socket->tcp_connection_state == TCP_OPEN) {
|
tcp_disconnect(socket);
|
tcp_disconnect(socket);
|
}
|
}
|
|
|
|
|
/* Reset connection */
|
/* Reset connection */
|
if (socket->tcp_reset) {
|
if (socket->tcp_reset) {
|
socket->tcp_connection_state = TCP_CLOSED;
|
socket->tcp_connection_state = TCP_CLOSED;
|
socket->telnet_connection_state = TELNET_CLOSED;
|
socket->telnet_connection_state = TELNET_CLOSED;
|
socket->telnet_options_sent = 0;
|
socket->telnet_options_sent = 0;
|
tcp_reply(socket, NULL, 0);
|
tcp_reply(socket, NULL, 0);
|
socket->tcp_reset = 0;
|
socket->tcp_reset = 0;
|
}
|
}
|
|
|
|
|
/* Send telnet options */
|
/* Send telnet options */
|
if (socket->tcp_connection_state == TCP_OPEN && !socket->telnet_options_sent){
|
if (socket->tcp_connection_state == TCP_OPEN && !socket->telnet_options_sent){
|
telnet_options(socket);
|
telnet_options(socket);
|
socket->telnet_options_sent = 1;
|
socket->telnet_options_sent = 1;
|
}
|
}
|
|
|
/* telnet connection open
|
/* telnet connection open
|
Communicate with client */
|
Communicate with client */
|
else if (socket->telnet_connection_state == TELNET_OPEN) {
|
else if (socket->telnet_connection_state == TELNET_OPEN) {
|
/* Send telnet greeting */
|
/* Send telnet greeting */
|
if (!socket->telnet_sent_opening_message){
|
if (!socket->telnet_sent_opening_message){
|
put_line (socket->telnet_txbuf, "Amber Processor Boot Loader\r\n> ");
|
put_line (socket->telnet_txbuf, "Amber Processor Boot Loader\r\n> ");
|
socket->telnet_sent_opening_message = 1;
|
socket->telnet_sent_opening_message = 1;
|
}
|
}
|
|
|
/* Parse telnet rx buffer */
|
/* Parse telnet rx buffer */
|
if (get_line(socket->telnet_rxbuf, &line))
|
if (get_line(socket->telnet_rxbuf, &line))
|
parse_command (socket, line);
|
parse_command (socket, line);
|
|
|
/* Transmit text from telnet tx buffer */
|
/* Transmit text from telnet tx buffer */
|
telnet_tx(socket, socket->telnet_txbuf);
|
telnet_tx(socket, socket->telnet_txbuf);
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
|
|
|
|
/* Parse a command line passed from main and execute the command */
|
/* Parse a command line passed from main and execute the command */
|
/* returns the length of the reply string */
|
/* returns the length of the reply string */
|
int parse_command (socket_t* socket, char* line)
|
int parse_command (socket_t* socket, char* line)
|
{
|
{
|
unsigned int start_addr;
|
unsigned int start_addr;
|
unsigned int address;
|
unsigned int address;
|
unsigned int range;
|
unsigned int range;
|
int len, error = 0;
|
int len, error = 0;
|
|
|
/* All commands are just a single character.
|
/* All commands are just a single character.
|
Just ignore anything else */
|
Just ignore anything else */
|
switch (line[0]) {
|
switch (line[0]) {
|
/* Disconnect */
|
/* Disconnect */
|
case 'e':
|
case 'e':
|
case 'x':
|
case 'x':
|
case 'q':
|
case 'q':
|
socket->tcp_disconnect = 1;
|
socket->tcp_disconnect = 1;
|
return 0;
|
return 0;
|
|
|
case 'r': /* Read mem */
|
case 'r': /* Read mem */
|
{
|
{
|
if (len = get_hex (&line[2], &start_addr)) {
|
if (len = get_hex (&line[2], &start_addr)) {
|
if (len = get_hex (&line[3+len], &range)) {
|
if (len = get_hex (&line[3+len], &range)) {
|
for (address=start_addr; address<start_addr+range; address+=4) {
|
for (address=start_addr; address<start_addr+range; address+=4) {
|
put_line (socket->telnet_txbuf, "0x%08x 0x%08x\r\n",
|
put_line (socket->telnet_txbuf, "0x%08x 0x%08x\r\n",
|
address, *(unsigned int *)address);
|
address, *(unsigned int *)address);
|
}
|
}
|
}
|
}
|
else {
|
else {
|
put_line (socket->telnet_txbuf, "0x%08x 0x%08x\r\n",
|
put_line (socket->telnet_txbuf, "0x%08x 0x%08x\r\n",
|
start_addr, *(unsigned int *)start_addr);
|
start_addr, *(unsigned int *)start_addr);
|
}
|
}
|
}
|
}
|
else
|
else
|
error=1;
|
error=1;
|
break;
|
break;
|
}
|
}
|
|
|
|
|
case 'h': {/* Help */
|
case 'h': {/* Help */
|
put_line (socket->telnet_txbuf, "You need help alright\r\n");
|
put_line (socket->telnet_txbuf, "You need help alright\r\n");
|
break;
|
break;
|
}
|
}
|
|
|
|
|
case 's': {/* Status */
|
case 's': {/* Status */
|
put_line (socket->telnet_txbuf, "Socket ID %d\r\n", socket->id);
|
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 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 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, "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, "TCP checksum errors %d\r\n", tcp_checksum_errors_g);
|
|
|
put_line (socket->telnet_txbuf, "Counterparty IP %d.%d.%d.%d\r\n",
|
put_line (socket->telnet_txbuf, "Counterparty IP %d.%d.%d.%d\r\n",
|
socket->rx_packet->src_ip[0],
|
socket->rx_packet->src_ip[0],
|
socket->rx_packet->src_ip[1],
|
socket->rx_packet->src_ip[1],
|
socket->rx_packet->src_ip[2],
|
socket->rx_packet->src_ip[2],
|
socket->rx_packet->src_ip[3]);
|
socket->rx_packet->src_ip[3]);
|
|
|
put_line (socket->telnet_txbuf, "Counterparty Port %d\r\n",
|
put_line (socket->telnet_txbuf, "Counterparty Port %d\r\n",
|
socket->rx_packet->tcp_src_port);
|
socket->rx_packet->tcp_src_port);
|
|
|
put_line (socket->telnet_txbuf, "Malloc pointer 0x%08x\r\n",
|
put_line (socket->telnet_txbuf, "Malloc pointer 0x%08x\r\n",
|
*(unsigned int *)(ADR_MALLOC_POINTER));
|
*(unsigned int *)(ADR_MALLOC_POINTER));
|
put_line (socket->telnet_txbuf, "Malloc count %d\r\n",
|
put_line (socket->telnet_txbuf, "Malloc count %d\r\n",
|
*(unsigned int *)(ADR_MALLOC_COUNT));
|
*(unsigned int *)(ADR_MALLOC_COUNT));
|
put_line (socket->telnet_txbuf, "Uptime %d seconds\r\n", current_time_g->seconds);
|
put_line (socket->telnet_txbuf, "Uptime %d seconds\r\n", current_time_g->seconds);
|
break;
|
break;
|
}
|
}
|
|
|
|
|
default: {
|
default: {
|
error=1; break;
|
error=1; break;
|
}
|
}
|
}
|
}
|
|
|
|
|
if (error)
|
if (error)
|
put_line (socket->telnet_txbuf, "You're not making any sense\r\n",
|
put_line (socket->telnet_txbuf, "You're not making any sense\r\n",
|
line[0], line[1], line[2]);
|
line[0], line[1], line[2]);
|
|
|
put_line (socket->telnet_txbuf, "> ");
|
put_line (socket->telnet_txbuf, "> ");
|
return 0;
|
return 0;
|
}
|
}
|
|
|
|
|
/* copy tftp file into a single contiguous buffer so
|
/* copy tftp file into a single contiguous buffer so
|
if can be processed by elf splitter */
|
if can be processed by elf splitter */
|
int process_file(socket_t* socket)
|
int process_file(socket_t* socket)
|
{
|
{
|
block_t* block;
|
block_t* block;
|
char* buf512;
|
char* buf512;
|
char* tftp_file;
|
char* tftp_file;
|
char* line;
|
char* line;
|
int line_len;
|
int line_len;
|
int ret;
|
int ret;
|
|
|
tftp_file = malloc(udp_file_g->total_bytes);
|
tftp_file = malloc(udp_file_g->total_bytes);
|
|
|
block = udp_file_g;
|
block = udp_file_g;
|
buf512= tftp_file;
|
buf512= tftp_file;
|
|
|
while (block->next) {
|
while (block->next) {
|
memcpy(buf512, block->buf512, block->bytes);
|
memcpy(buf512, block->buf512, block->bytes);
|
buf512=&buf512[512];
|
buf512=&buf512[512];
|
block=block->next;
|
block=block->next;
|
}
|
}
|
memcpy(buf512, block->buf512, block->bytes);
|
memcpy(buf512, block->buf512, block->bytes);
|
buf512=&buf512[512];
|
buf512=&buf512[512];
|
|
|
return elfsplitter(tftp_file, socket);
|
return elfsplitter(tftp_file, socket);
|
}
|
}
|
|
|
|
|
/* Disable interrupts
|
/* Disable interrupts
|
Load new values into the interrupt vector memory space
|
Load new values into the interrupt vector memory space
|
Jump to address 0
|
Jump to address 0
|
*/
|
*/
|
void reboot()
|
void reboot()
|
{
|
{
|
int i;
|
int i;
|
|
|
/* Disable all interrupts */
|
/* Disable all interrupts */
|
/* Disable ethmac_int interrupt */
|
/* Disable ethmac_int interrupt */
|
/* Disable timer 0 interrupt in interrupt controller */
|
/* Disable timer 0 interrupt in interrupt controller */
|
*(unsigned int *) ( ADR_AMBER_IC_IRQ0_ENABLECLR ) = 0x120;
|
*(unsigned int *) ( ADR_AMBER_IC_IRQ0_ENABLECLR ) = 0x120;
|
|
|
for(i=0;i<MEM_BUF_ENTRIES;i++)
|
for(i=0;i<MEM_BUF_ENTRIES;i++)
|
if (elf_mem0_g->entry[i].valid)
|
if (elf_mem0_g->entry[i].valid)
|
*(char *)(i) = elf_mem0_g->entry[i].data;
|
*(char *)(i) = elf_mem0_g->entry[i].data;
|
|
|
if (udp_file_g->linux_boot)
|
if (udp_file_g->linux_boot) {
|
|
print_serial("linux reboot\n\r");
|
_jump_to_program(LINUX_JUMP_ADR);
|
_jump_to_program(LINUX_JUMP_ADR);
|
else
|
}
|
|
else {
|
|
print_serial("normal reboot\n\r");
|
_restart();
|
_restart();
|
}
|
}
|
|
}
|
|
|
|
|