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