| 1 |
//========================================================================== |
|---|
| 2 |
// |
|---|
| 3 |
// net/tftp_server.c |
|---|
| 4 |
// |
|---|
| 5 |
// Stand-alone TFTP server support for RedBoot |
|---|
| 6 |
// |
|---|
| 7 |
|
|---|
| 8 |
#include <redboot.h> // have_net |
|---|
| 9 |
#include <net/net.h> |
|---|
| 10 |
#include <net/tftp.h> |
|---|
| 11 |
#include <net/tftp_support.h> |
|---|
| 12 |
#include <cyg/io/flash.h> |
|---|
| 13 |
#include "fwupgrade.h" |
|---|
| 14 |
|
|---|
| 15 |
#define IPPORT_TFTPD 69 |
|---|
| 16 |
#define FIRMWARE_PASSWORD "flash_update" |
|---|
| 17 |
|
|---|
| 18 |
#define TFTP_HEADER_SIZE 4 |
|---|
| 19 |
#define SEGSIZE 512 |
|---|
| 20 |
|
|---|
| 21 |
/* from main.c */ |
|---|
| 22 |
extern void do_reset(int argc, char *argv[]); |
|---|
| 23 |
|
|---|
| 24 |
/* global variables for tftpd */ |
|---|
| 25 |
static ip_route_t wrq_route = { ip_addr:{0, 0, 0, 0} }; |
|---|
| 26 |
|
|---|
| 27 |
static word wrq_port = 0; |
|---|
| 28 |
static char tftpd_buffer[SEGSIZE + TFTP_HEADER_SIZE]; |
|---|
| 29 |
|
|---|
| 30 |
unsigned long tftpd_base = 0; |
|---|
| 31 |
|
|---|
| 32 |
static char usage[] = ""; |
|---|
| 33 |
|
|---|
| 34 |
// Exported CLI function |
|---|
| 35 |
RedBoot_cmd("tftpd", |
|---|
| 36 |
"Start tftp server and wait for firmware update", usage, do_tftpd); |
|---|
| 37 |
|
|---|
| 38 |
static void |
|---|
| 39 |
tftpd_error(int error_code, char *error_msg, ip_route_t * src_route, |
|---|
| 40 |
word src_port) |
|---|
| 41 |
{ |
|---|
| 42 |
struct tftphdr *tp; |
|---|
| 43 |
int len; |
|---|
| 44 |
|
|---|
| 45 |
diag_printf("TFTPD: Error %d: %s\n", error_code, error_msg); |
|---|
| 46 |
|
|---|
| 47 |
tp = (struct tftphdr *)tftpd_buffer; |
|---|
| 48 |
tp->th_opcode = htons((unsigned short)ERROR); |
|---|
| 49 |
tp->th_code = htons((unsigned short)error_code); |
|---|
| 50 |
strcpy(tp->th_msg, error_msg); |
|---|
| 51 |
len = strlen(error_msg); |
|---|
| 52 |
tp->th_msg[len] = '\0'; |
|---|
| 53 |
len += (TFTP_HEADER_SIZE + 1); |
|---|
| 54 |
|
|---|
| 55 |
__udp_send((char *)tftpd_buffer, len, src_route, src_port, |
|---|
| 56 |
IPPORT_TFTPD); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
static void |
|---|
| 60 |
tftpd_send(int opcode, int block, ip_route_t * src_route, word src_port) |
|---|
| 61 |
{ |
|---|
| 62 |
struct tftphdr *tp; |
|---|
| 63 |
|
|---|
| 64 |
#if 0 |
|---|
| 65 |
diag_printf("tftpd_send\n"); |
|---|
| 66 |
#endif |
|---|
| 67 |
|
|---|
| 68 |
tp = (struct tftphdr *)tftpd_buffer; |
|---|
| 69 |
tp->th_opcode = htons((unsigned short)opcode); |
|---|
| 70 |
tp->th_block = htons((unsigned short)block); |
|---|
| 71 |
|
|---|
| 72 |
__udp_send((char *)tftpd_buffer, TFTP_HEADER_SIZE, src_route, src_port, |
|---|
| 73 |
IPPORT_TFTPD); |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
#if 0 |
|---|
| 77 |
/* jM -- set status led green or red */ |
|---|
| 78 |
#define STATUS_LED_GPIO 2 |
|---|
| 79 |
static void set_status_led(int status) |
|---|
| 80 |
{ |
|---|
| 81 |
HAL_GPIO_OUTPUT_ENABLE(STATUS_LED_GPIO); |
|---|
| 82 |
|
|---|
| 83 |
if (status == 0) |
|---|
| 84 |
HAL_GPIO_OUTPUT_CLEAR(STATUS_LED_GPIO); |
|---|
| 85 |
else |
|---|
| 86 |
HAL_GPIO_OUTPUT_SET(STATUS_LED_GPIO); |
|---|
| 87 |
} |
|---|
| 88 |
#endif |
|---|
| 89 |
|
|---|
| 90 |
#include "../ramconfig.h" |
|---|
| 91 |
|
|---|
| 92 |
extern int page_programming_supported; |
|---|
| 93 |
extern int page_gpio; |
|---|
| 94 |
|
|---|
| 95 |
struct firmware_formats { |
|---|
| 96 |
char *name; |
|---|
| 97 |
int (*fw_check_image) (unsigned char *, unsigned long, int); |
|---|
| 98 |
}; |
|---|
| 99 |
|
|---|
| 100 |
static const struct firmware_formats fw_formats[] = { |
|---|
| 101 |
{.name = "DD-WRT",.fw_check_image = fw_check_image_ddwrt}, |
|---|
| 102 |
{.name = "UBIQUITI",.fw_check_image = fw_check_image_ubnt}, |
|---|
| 103 |
{.name = "WILIGEAR",.fw_check_image = fw_check_image_wili}, |
|---|
| 104 |
{.name = "SENAO",.fw_check_image = fw_check_image_senao}, |
|---|
| 105 |
}; |
|---|
| 106 |
|
|---|
| 107 |
static void do_flash_update(unsigned long base_addr, unsigned long len, |
|---|
| 108 |
int index) |
|---|
| 109 |
{ |
|---|
| 110 |
int rc; |
|---|
| 111 |
#if defined(CYGPKG_HAL_MIPS_AR2316) |
|---|
| 112 |
#if FISTYPE == 1 |
|---|
| 113 |
*(volatile unsigned *)0xB1000090 |= 1; /*set GPIO0 to 1 to spi flash CS normal state */ |
|---|
| 114 |
*(volatile unsigned *)0xB1000098 |= 1; /*set GPIO0 to be output */ |
|---|
| 115 |
page_programming_supported = 1; |
|---|
| 116 |
page_gpio = 0; |
|---|
| 117 |
#endif |
|---|
| 118 |
|
|---|
| 119 |
#if FISTYPE == 2 |
|---|
| 120 |
*(volatile unsigned *)0xB1000090 |= 1 << 3; /*set GPIO0 to 1 to spi flash CS normal state */ |
|---|
| 121 |
*(volatile unsigned *)0xB1000098 |= 1 << 3; /*set GPIO0 to be output */ |
|---|
| 122 |
page_programming_supported = 1; |
|---|
| 123 |
page_gpio = 3; |
|---|
| 124 |
#endif |
|---|
| 125 |
|
|---|
| 126 |
#if FISTYPE == 0 |
|---|
| 127 |
page_programming_supported = 0; |
|---|
| 128 |
page_gpio = 0; |
|---|
| 129 |
#endif |
|---|
| 130 |
#endif |
|---|
| 131 |
/* do_flash = 1, write to flash */ |
|---|
| 132 |
rc = fw_formats[index].fw_check_image((char *)base_addr, len, 1); |
|---|
| 133 |
if (rc) |
|---|
| 134 |
diag_printf("Flash update failed!\n"); |
|---|
| 135 |
else |
|---|
| 136 |
diag_printf("Flash update complete.\n"); |
|---|
| 137 |
/* clean da memory */ |
|---|
| 138 |
memset((unsigned char *)base_addr, 0, len); |
|---|
| 139 |
do_reset(0, NULL); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
void |
|---|
| 143 |
tftpd_fsm(struct tftphdr *tp, int len, ip_route_t * src_route, word src_port) |
|---|
| 144 |
{ |
|---|
| 145 |
char *mode, *password; |
|---|
| 146 |
static unsigned int last_sync_block; |
|---|
| 147 |
unsigned int block; |
|---|
| 148 |
static int bytes_received = 0; |
|---|
| 149 |
static unsigned long ptr = 0L; |
|---|
| 150 |
|
|---|
| 151 |
#if 0 |
|---|
| 152 |
diag_printf("got packet from %d.%d.%d.%d port %d\n", |
|---|
| 153 |
src_route->ip_addr[0], |
|---|
| 154 |
src_route->ip_addr[1], |
|---|
| 155 |
src_route->ip_addr[2], src_route->ip_addr[3], src_port); |
|---|
| 156 |
|
|---|
| 157 |
diag_printf("packet len is %d, TFTP opcode is %d\n", |
|---|
| 158 |
len, tp->th_opcode); |
|---|
| 159 |
#endif |
|---|
| 160 |
|
|---|
| 161 |
switch (tp->th_opcode) { |
|---|
| 162 |
case WRQ: /* write request */ |
|---|
| 163 |
password = tp->th_stuff; |
|---|
| 164 |
mode = &password[strlen(password) + 1]; |
|---|
| 165 |
diag_printf("TFTPD: Connect from %d.%d.%d.%d port %d\n", |
|---|
| 166 |
src_route->ip_addr[0], |
|---|
| 167 |
src_route->ip_addr[1], |
|---|
| 168 |
src_route->ip_addr[2], |
|---|
| 169 |
src_route->ip_addr[3], src_port); |
|---|
| 170 |
//if (strcmp(password, FIRMWARE_PASSWORD)) { |
|---|
| 171 |
// tftpd_error(EACCESS, "Access violation (invalid password)", src_route, src_port); |
|---|
| 172 |
// break; |
|---|
| 173 |
//} |
|---|
| 174 |
if (strcmp(mode, "octet")) { |
|---|
| 175 |
tftpd_error(EACCESS, |
|---|
| 176 |
"Access violation (use binary mode)", |
|---|
| 177 |
src_route, src_port); |
|---|
| 178 |
break; |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
/* we are ready for data */ |
|---|
| 182 |
tftpd_send(ACK, 0, src_route, src_port); |
|---|
| 183 |
|
|---|
| 184 |
last_sync_block = 0; |
|---|
| 185 |
memmove(&wrq_route.ip_addr, &src_route->ip_addr, |
|---|
| 186 |
sizeof(ip_addr_t)); |
|---|
| 187 |
wrq_port = src_port; |
|---|
| 188 |
|
|---|
| 189 |
break; |
|---|
| 190 |
|
|---|
| 191 |
case DATA: /* received data */ |
|---|
| 192 |
if (*(int *)(wrq_route.ip_addr) != *(int *)(src_route->ip_addr)) |
|---|
| 193 |
tftpd_error(EACCESS, |
|---|
| 194 |
"Access violation (unknown address)", |
|---|
| 195 |
src_route, src_port); |
|---|
| 196 |
|
|---|
| 197 |
block = ntohs(tp->th_block); |
|---|
| 198 |
len -= TFTP_HEADER_SIZE; |
|---|
| 199 |
|
|---|
| 200 |
if (block == 1) { |
|---|
| 201 |
bytes_received = 0; |
|---|
| 202 |
ptr = BASE_ADDR; /* rewind pointer */ |
|---|
| 203 |
|
|---|
| 204 |
} |
|---|
| 205 |
bytes_received += len; |
|---|
| 206 |
#if 0 |
|---|
| 207 |
if (!(bytes_received % (1024 * 16))) |
|---|
| 208 |
diag_printf("ptr = %08X\n", ptr); |
|---|
| 209 |
#endif |
|---|
| 210 |
if (bytes_received >= MAX_IMAGE_SIZE) { |
|---|
| 211 |
tftpd_error(EACCESS, "Image size too big.", src_route, |
|---|
| 212 |
src_port); |
|---|
| 213 |
break; |
|---|
| 214 |
} |
|---|
| 215 |
if (block == last_sync_block + 1) { |
|---|
| 216 |
last_sync_block = block; |
|---|
| 217 |
|
|---|
| 218 |
memmove((char *)ptr, &tp->th_data, len); |
|---|
| 219 |
ptr += len; |
|---|
| 220 |
|
|---|
| 221 |
} else { |
|---|
| 222 |
/* out of sync */ |
|---|
| 223 |
diag_printf("Blocks out of sync (prev %d, curr %d)", |
|---|
| 224 |
last_sync_block, block); |
|---|
| 225 |
break; |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
if (len < SEGSIZE) { |
|---|
| 229 |
diag_printf("TFTPD: Upload completed (got %d bytes).\n", |
|---|
| 230 |
ptr - BASE_ADDR); |
|---|
| 231 |
|
|---|
| 232 |
// CRC CHECK |
|---|
| 233 |
diag_printf("Checking uploaded file...\n"); |
|---|
| 234 |
|
|---|
| 235 |
int detect = 0; |
|---|
| 236 |
int i; |
|---|
| 237 |
for (i = 0; |
|---|
| 238 |
i < |
|---|
| 239 |
sizeof(fw_formats) / |
|---|
| 240 |
sizeof(struct firmware_formats); i++) { |
|---|
| 241 |
int v = |
|---|
| 242 |
fw_formats[i]. |
|---|
| 243 |
fw_check_image((char *)BASE_ADDR, |
|---|
| 244 |
ptr - BASE_ADDR, 0) == 0; |
|---|
| 245 |
if (v) { |
|---|
| 246 |
detect = i; |
|---|
| 247 |
break; |
|---|
| 248 |
} |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
if (detect) /* third parameter 0 - do not write to flash */ |
|---|
| 252 |
tftpd_send(ACK, block, src_route, src_port); // crc ok |
|---|
| 253 |
else { |
|---|
| 254 |
tftpd_error(EACCESS, "CRC error", src_route, |
|---|
| 255 |
src_port); |
|---|
| 256 |
break; |
|---|
| 257 |
} |
|---|
| 258 |
|
|---|
| 259 |
// write to flash |
|---|
| 260 |
#if 1 |
|---|
| 261 |
diag_printf("%s: firmware format detected\n", |
|---|
| 262 |
fw_formats[detect].name); |
|---|
| 263 |
do_flash_update(BASE_ADDR, ptr - BASE_ADDR, detect); |
|---|
| 264 |
#else |
|---|
| 265 |
diag_printf("Not writing to flash.\n"); |
|---|
| 266 |
#endif |
|---|
| 267 |
|
|---|
| 268 |
} else { |
|---|
| 269 |
/* send ACK to last block in sequence */ |
|---|
| 270 |
tftpd_send(ACK, block, src_route, src_port); |
|---|
| 271 |
} |
|---|
| 272 |
break; |
|---|
| 273 |
default: |
|---|
| 274 |
tftpd_error(EBADOP, "Illegal TFTP operation", src_route, |
|---|
| 275 |
src_port); |
|---|
| 276 |
break; |
|---|
| 277 |
} |
|---|
| 278 |
} |
|---|
| 279 |
|
|---|
| 280 |
void |
|---|
| 281 |
tftpd_handler(udp_socket_t * skt, char *buf, int len, |
|---|
| 282 |
ip_route_t * src_route, word src_port) |
|---|
| 283 |
{ |
|---|
| 284 |
struct tftphdr *tp; |
|---|
| 285 |
|
|---|
| 286 |
#if 0 |
|---|
| 287 |
diag_printf("tftpd handler called!\n"); |
|---|
| 288 |
#endif |
|---|
| 289 |
|
|---|
| 290 |
tp = (struct tftphdr *)buf; |
|---|
| 291 |
tp->th_opcode = ntohs(tp->th_opcode); |
|---|
| 292 |
tftpd_fsm(tp, len, src_route, src_port); |
|---|
| 293 |
|
|---|
| 294 |
} |
|---|
| 295 |
|
|---|
| 296 |
void do_tftpd(int argc, char *argv[]) |
|---|
| 297 |
{ |
|---|
| 298 |
udp_socket_t udp_skt; |
|---|
| 299 |
|
|---|
| 300 |
/* First, fwupdate.bin will be loaded to BASE_ADDR. |
|---|
| 301 |
* Then before writing to flash each partition (kernel, ramdisk) |
|---|
| 302 |
* will be moved to FW_TEMP_BASE (thus this address will be in FIS mem entry |
|---|
| 303 |
*/ |
|---|
| 304 |
FW_TEMP_BASE = |
|---|
| 305 |
((CYG_ADDRWORD) mem_segments[0].start + 0x03ff) & ~0x03ff; |
|---|
| 306 |
/* note: memory addresses from 80700000 are reserved ? anyway, do not use them */ |
|---|
| 307 |
// BASE_ADDR = ((CYG_ADDRWORD)mem_segments[0].end & ~0x03ff) - MAX_IMAGE_SIZE; |
|---|
| 308 |
BASE_ADDR = |
|---|
| 309 |
(((CYG_ADDRWORD) mem_segments[0].start + 0x03ff) & ~0x03ff) + |
|---|
| 310 |
MAX_PART_SIZE; |
|---|
| 311 |
if ((BASE_ADDR + MAX_IMAGE_SIZE) >= 0x80730000) |
|---|
| 312 |
diag_printf |
|---|
| 313 |
("Warning: memory buffer for uploaded file may be on reserved RAM area.\n"); |
|---|
| 314 |
|
|---|
| 315 |
#if 1 |
|---|
| 316 |
/* hack: on ar531x, elf image should always be at 0x80002000 base, so adjust if possible */ |
|---|
| 317 |
if ((FW_TEMP_BASE < 0x80002000) |
|---|
| 318 |
&& (FW_TEMP_BASE + MAX_PART_SIZE <= BASE_ADDR)) |
|---|
| 319 |
FW_TEMP_BASE = 0x80002000; |
|---|
| 320 |
#endif |
|---|
| 321 |
|
|---|
| 322 |
diag_printf |
|---|
| 323 |
("TFTPD is running (using memory ranges: %p - %p, %p - %p).\n", |
|---|
| 324 |
FW_TEMP_BASE, BASE_ADDR, BASE_ADDR, BASE_ADDR + MAX_IMAGE_SIZE); |
|---|
| 325 |
|
|---|
| 326 |
__udp_install_listener(&udp_skt, IPPORT_TFTPD, tftpd_handler); |
|---|
| 327 |
|
|---|
| 328 |
while (!_rb_break(0)) { |
|---|
| 329 |
__enet_poll(); // wait for packet ? |
|---|
| 330 |
} |
|---|
| 331 |
|
|---|
| 332 |
__udp_remove_listener(IPPORT_TFTPD); |
|---|
| 333 |
diag_printf("TFTPD terminated.\n"); |
|---|
| 334 |
} |
|---|