From: Piotr Dymacz Date: Sun, 5 Feb 2017 18:50:05 +0000 (+0100) Subject: Clean and fix up messy TFTP/RARP related code X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=c3e10c03a03b17f00c1c67edb61bd368e0e4386b;p=oweals%2Fu-boot_mod.git Clean and fix up messy TFTP/RARP related code --- diff --git a/u-boot/common/cmd_net.c b/u-boot/common/cmd_net.c index 939258f..a7d002c 100644 --- a/u-boot/common/cmd_net.c +++ b/u-boot/common/cmd_net.c @@ -30,255 +30,284 @@ #if defined(CONFIG_CMD_NET) -u32 save_addr; /* Default Save Address */ -u32 save_size; /* Default Save Size (in bytes) */ +u32 save_addr; /* Default save address for TFTPPUT */ +u32 save_size; /* Default save size (in bytes) for TFTPPUT */ extern int do_bootm(cmd_tbl_t *, int, int, char *[]); static int netboot_common(proto_t, cmd_tbl_t *, int, char *[]); #if defined(CONFIG_CMD_HTTPD) -int do_httpd(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){ +int do_httpd(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) +{ return NetLoopHttpd(); } -U_BOOT_CMD(httpd, 1, 1, do_httpd, "start www server for firmware recovery\n", NULL); + +U_BOOT_CMD(httpd, 1, 1, do_httpd, + "start web server for firmware recovery\n", + NULL); #endif /* CONFIG_CMD_HTTPD */ -int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){ - return netboot_common(TFTPGET, cmdtp, argc, argv); +#if defined(CONFIG_CMD_DHCP) +int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) +{ + return netboot_common(DHCP, cmdtp, argc, argv); } -U_BOOT_CMD(tftpboot, 3, 1, do_tftpb, "boot image via network using TFTP protocol\n", "[loadAddress] [bootfilename]\n"); -int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) +U_BOOT_CMD(dhcp, 3, 1, do_dhcp, + "invoke DHCP client to obtain IP/boot params\n", + NULL); +#endif /* CONFIG_CMD_DHCP */ + +#if defined(CONFIG_CMD_NFS) +int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) { - if (argc < 4) { + return netboot_common(NFS, cmdtp, argc, argv); +} +U_BOOT_CMD(nfs, 3, 1, do_nfs, + "boot image via network using NFS protocol\n", + "[address] [host ip addr:filename]\n" + "\t- loads 'filename' at 'address' using NFS protocol"); +#endif /* CONFIG_CMD_NFS */ + +#if defined(CONFIG_CMD_PING) +int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) +{ + if (argc < 2) { print_cmd_help(cmdtp); return -1; } - return netboot_common(TFTPPUT, cmdtp, argc, argv); + NetPingIP = string_to_ip(argv[1]); + + if (NetPingIP == 0) { + print_cmd_help(cmdtp); + return -1; + } + + if (NetLoop(PING) < 0) { + puts("\n"); + printf_err("ping failed, host %s is not alive!\n\n", argv[1]); + return 1; + } + + printf("\nPing OK, host %s is alive!\n\n", argv[1]); + + return 0; } -U_BOOT_CMD(tftpput, 4, 1, do_tftpput, "send file to TFTP server\n", "address size filename\n" - "\t- sends 'size' of data from 'address' as 'filename' to TFTP server"); +U_BOOT_CMD(ping, 2, 1, do_ping, + "send ICMP ECHO_REQUEST to network host\n", + "host IP\n" + "\t- sends ping to IP 'host IP'\n"); +#endif /* CONFIG_CMD_PING */ -#if defined(CONFIG_CMD_DHCP) -int do_dhcp(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){ - return netboot_common(DHCP, cmdtp, argc, argv); +#if defined(CONFIG_CMD_SNTP) +int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) +{ + char *toff; + + if (argc < 2) { + print_cmd_help(cmdtp); + return -1; + } else { + NetNtpServerIP = string_to_ip(argv[1]); + + if (NetNtpServerIP == 0) { + printf_err("bad SNTP server IP address\n"); + return 1; + } + } + + toff = getenv("timeoffset"); + + if (toff == NULL) + NetTimeOffset = 0; + else + NetTimeOffset = simple_strtol(toff, NULL, 10); + + if (NetLoop(SNTP) < 0) { + printf_err("SNTP host %s not responding\n", argv[1]); + return 1; + } + + return 0 ; } -U_BOOT_CMD(dhcp, 3, 1, do_dhcp, "invoke DHCP client to obtain IP/boot params\n", NULL); -#endif /* CONFIG_CMD_DHCP */ -#if defined(CONFIG_CMD_NFS) -int do_nfs(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){ - return netboot_common(NFS, cmdtp, argc, argv); +U_BOOT_CMD(sntp, 2, 1, do_sntp, + "send NTP request to NTP server\n", + "ntpserverip\n" + "\t- sends NTP request to NTP server 'ntpserverip'\n"); +#endif /* CONFIG_CMD_SNTP */ + +int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) +{ + return netboot_common(TFTPGET, cmdtp, argc, argv); } -U_BOOT_CMD(nfs, 3, 1, do_nfs, "boot image via network using NFS protocol\n", "[loadAddress] [host ip addr:bootfilename]\n"); -#endif /* CONFIG_CMD_NFS */ -static void netboot_update_env(void){ +U_BOOT_CMD(tftpboot, 3, 1, do_tftpb, + "boot image via network using TFTP protocol\n", + "[address] [filename]\n" + "\t- loads 'filename' at 'address' from TFTP server"); + +int do_tftpput(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) +{ + if (argc < 4) { + print_cmd_help(cmdtp); + return -1; + } + + return netboot_common(TFTPPUT, cmdtp, argc, argv); +} + +U_BOOT_CMD(tftpput, 4, 1, do_tftpput, + "send file to TFTP server\n", + "address size filename\n" + "\t- sends 'size' of data from 'address' as 'filename' to TFTP server"); + +static void netboot_update_env(void) +{ char tmp[22]; - if(NetOurGatewayIP){ + if (NetOurGatewayIP) { ip_to_string(NetOurGatewayIP, tmp); setenv("gatewayip", tmp); } - if(NetOurSubnetMask){ + if (NetOurSubnetMask) { ip_to_string(NetOurSubnetMask, tmp); setenv("netmask", tmp); } - if(NetOurHostName[0]){ + if (NetOurHostName[0]) setenv("hostname", NetOurHostName); - } - if(NetOurRootPath[0]){ + if (NetOurRootPath[0]) setenv("rootpath", NetOurRootPath); - } - if(NetOurIP){ + if (NetOurIP) { ip_to_string(NetOurIP, tmp); setenv("ipaddr", tmp); } - if(NetServerIP){ + if (NetServerIP) { ip_to_string(NetServerIP, tmp); setenv("serverip", tmp); } - if(NetOurDNSIP){ + if (NetOurDNSIP) { ip_to_string(NetOurDNSIP, tmp); setenv("dnsip", tmp); } #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2) - if(NetOurDNS2IP){ - ip_to_string (NetOurDNS2IP, tmp); + if (NetOurDNS2IP) { + ip_to_string(NetOurDNS2IP, tmp); setenv("dnsip2", tmp); } #endif - if(NetOurNISDomain[0]){ + if (NetOurNISDomain[0]) setenv("domain", NetOurNISDomain); - } -#if defined(CONFIG_CMD_SNTP) && (CONFIG_BOOTP_MASK & CONFIG_BOOTP_TIMEOFFSET) - if(NetTimeOffset){ +#if defined(CONFIG_CMD_SNTP) + #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_TIMEOFFSET) + if (NetTimeOffset) { sprintf(tmp, "%d", NetTimeOffset); setenv("timeoffset", tmp); } -#endif + #endif -#if defined(CONFIG_CMD_SNTP) && (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NTPSERVER) - if (NetNtpServerIP){ + #if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_NTPSERVER) + if (NetNtpServerIP) { ip_to_string(NetNtpServerIP, tmp); setenv("ntpserverip", tmp); } -#endif + #endif +#endif /* CONFIG_CMD_SNTP */ } -static int netboot_common(proto_t proto, cmd_tbl_t *cmdtp, int argc, char *argv[]){ +static int netboot_common(proto_t proto, + cmd_tbl_t *cmdtp, + int argc, + char *argv[]) +{ char *s; - int rcode = 0; int size; + ulong tmp; + int rcode = 0; - /* pre-set load_addr */ - if((s = getenv("loadaddr")) != NULL){ + /* Pre-set load_addr */ + if ((s = getenv("loadaddr")) != NULL) load_addr = simple_strtoul(s, NULL, 16); - } - switch(argc){ - case 1: - break; - - case 2: - /* only one arg - accept two forms: - * just load address, or just boot file name. - * The latter form must be written "filename" here. + switch (argc) { + case 1: + break; + case 2: + /* + * Don't allow to use loading address + * lower than RAM/FLASH base */ - if(argv[1][0] == '"'){ /* just boot filename */ - copy_filename(BootFile, argv[1], sizeof(BootFile)); - } else { /* load address */ - load_addr = simple_strtoul(argv[1], NULL, 16); - } - - break; - - case 3: - load_addr = simple_strtoul(argv[1], NULL, 16); - copy_filename(BootFile, argv[2], sizeof(BootFile)); - - break; + tmp = simple_strtoul(argv[1], NULL, 16); - case 4: - save_addr = simple_strtoul(argv[1], NULL, 16); - save_size = simple_strtoul(argv[2], NULL, 16); - copy_filename(BootFile, argv[3], sizeof(BootFile)); - break; - - default: - - print_cmd_help(cmdtp); - return 1; +#if defined(CFG_DIRECT_FLASH_TFTP) + if ((tmp < CFG_SDRAM_BASE && tmp < CFG_FLASH_BASE) || tmp == 0) +#else + if (tmp < CFG_SDRAM_BASE || tmp == 0) +#endif + copy_filename(BootFile, argv[1], sizeof(BootFile)); + else + load_addr = tmp; + + break; + case 3: + load_addr = simple_strtoul(argv[1], NULL, 16); + copy_filename(BootFile, argv[2], sizeof(BootFile)); + break; + case 4: + save_addr = simple_strtoul(argv[1], NULL, 16); + save_size = simple_strtoul(argv[2], NULL, 16); + copy_filename(BootFile, argv[3], sizeof(BootFile)); + break; + default: + print_cmd_help(cmdtp); + return 1; } - if((size = NetLoop(proto)) < 0){ - return(1); - } + if ((size = NetLoop(proto)) < 0) + return 1; /* NetLoop ok, update environment */ netboot_update_env(); - /* done if no file was loaded (no errors though) */ - if(size == 0){ - return(0); - } + /* Done if no file was loaded (no errors though) */ + if (size == 0) + return 0; - /* flush cache */ + /* Flush cache */ flush_cache(load_addr, size); /* Loading ok, check if we should attempt an auto-start */ - if(((s = getenv("autostart")) != NULL) && (strcmp(s, "yes") == 0)){ + if (((s = getenv("autostart")) != NULL) && (strcmp(s, "yes") == 0)) { char *local_args[2]; local_args[0] = argv[0]; local_args[1] = NULL; - printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr); + printf("Automatic boot of image at addr 0x%08lX ...\n", + load_addr); + rcode = do_bootm(cmdtp, 0, 1, local_args); } -#ifdef CONFIG_AUTOSCRIPT - if(((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)){ - printf("Running autoscript at addr 0x%08lX ...\n", load_addr); +#if defined(CONFIG_AUTOSCRIPT) + if (((s = getenv("autoscript")) != NULL) && (strcmp(s,"yes") == 0)) { + printf("Running autoscript at addr 0x%08lX ...\n", + load_addr); + rcode = autoscript(load_addr); } #endif - return rcode; -} - -#if defined(CONFIG_CMD_PING) -int do_ping(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){ - - if(argc < 2){ - print_cmd_help(cmdtp); - return(-1); - } - - NetPingIP = string_to_ip(argv[1]); - if (NetPingIP == 0){ - print_cmd_help(cmdtp); - return(-1); - } - - if(NetLoop(PING) < 0){ - puts("\n"); - printf_err("ping failed, host %s is not alive!\n\n", argv[1]); - return(1); - } - - printf("\nPing OK, host %s is alive!\n\n", argv[1]); - - return(0); -} - -U_BOOT_CMD(ping, 2, 1, do_ping, "send ICMP ECHO_REQUEST to network host\n", "host IP\n" - "\t- sends ping to IP 'host IP'\n"); -#endif /* CONFIG_CMD_PING */ - -#if defined(CONFIG_CMD_SNTP) -int do_sntp(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){ - char *toff; - - if(argc < 2){ - print_cmd_help(cmdtp); - return(-1); - } else { - NetNtpServerIP = string_to_ip(argv[1]); - if(NetNtpServerIP == 0){ - printf_err("bad SNTP server IP address\n"); - return(1); - } - } - - toff = getenv("timeoffset"); - - if(toff == NULL){ - NetTimeOffset = 0; - } else{ - NetTimeOffset = simple_strtol(toff, NULL, 10); - } - - if(NetLoop(SNTP) < 0){ - printf_err("SNTP host %s not responding\n", argv[1]); - return(1); - } - - return(0); + return rcode; } - -U_BOOT_CMD(sntp, 2, 1, do_sntp, "send NTP request to NTP server\n", "ntpserverip\n" - "\t- sends NTP request to NTP server 'ntpserverip'\n"); -#endif /* CONFIG_CMD_SNTP */ - #endif /* CONFIG_CMD_NET */ diff --git a/u-boot/net/rarp.c b/u-boot/net/rarp.c index 6c5dfd5..6e9c729 100644 --- a/u-boot/net/rarp.c +++ b/u-boot/net/rarp.c @@ -33,26 +33,29 @@ DECLARE_GLOBAL_DATA_PTR; -#define TIMEOUT 5 /* Seconds before trying BOOTP again */ -#ifndef CONFIG_NET_RETRY_COUNT -# define TIMEOUT_COUNT 5 /* # of timeouts before giving up */ +#define TIMEOUT 5 /* Seconds before trying BOOTP again */ + +#if !defined(CONFIG_NET_RETRY_COUNT) + /* # of timeouts before giving up */ + #define TIMEOUT_COUNT 5 #else -# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) + #define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT) #endif +int RarpTry; -int RarpTry; - -/* - * Handle a RARP received packet. - */ -static void -RarpHandler(uchar * dummi0, unsigned dummi1, unsigned dummi2, unsigned dummi3) +/* Handle a RARP received packet */ +static void RarpHandler(uchar *dummi0, + unsigned dummi1, + unsigned dummi2, + unsigned dummi3) { char *s; -#ifdef DEBUG + +#if defined(DEBUG) puts ("Got good RARP\n"); #endif + if ((s = getenv("autoload")) != NULL) { if (*s == 'n') { /* @@ -68,31 +71,30 @@ RarpHandler(uchar * dummi0, unsigned dummi1, unsigned dummi2, unsigned dummi3) #endif } } - TftpStart (TFTPGET); -} + TftpStart(TFTPGET); +} -/* - * Timeout on BOOTP request. - */ -static void RarpTimeout(void){ +/* Timeout on BOOTP request */ +static void RarpTimeout(void) +{ bd_t *bd = gd->bd; if (RarpTry >= TIMEOUT_COUNT) { - puts ("\nRetry count exceeded; starting again\n"); - NetStartAgain (); + puts("\nRetry count exceeded; starting again\n"); + NetStartAgain(); } else { - NetSetTimeout (TIMEOUT * CFG_HZ, RarpTimeout); - RarpRequest (); + NetSetTimeout(TIMEOUT * CFG_HZ, RarpTimeout); + RarpRequest(); } } - -void RarpRequest (void){ - bd_t *bd = gd->bd; +void RarpRequest(void) +{ int i; + ARP_t *rarp; + bd_t *bd = gd->bd; volatile uchar *pkt; - ARP_t * rarp; printf("RARP broadcast %d\n", ++RarpTry); pkt = NetTxPacket; @@ -101,23 +103,23 @@ void RarpRequest (void){ rarp = (ARP_t *)pkt; - rarp->ar_hrd = htons (ARP_ETHER); - rarp->ar_pro = htons (PROT_IP); + rarp->ar_hrd = htons(ARP_ETHER); + rarp->ar_pro = htons(PROT_IP); rarp->ar_hln = 6; rarp->ar_pln = 4; - rarp->ar_op = htons (RARPOP_REQUEST); - memcpy (&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ - memcpy (&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ - memcpy (&rarp->ar_data[10], NetOurEther, 6); /* dest ET addr = source ET addr ??*/ - /* dest. IP addr set to broadcast */ - for (i = 0; i <= 3; i++) { + rarp->ar_op = htons(RARPOP_REQUEST); + + memcpy(&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */ + memcpy(&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */ + memcpy(&rarp->ar_data[10], NetOurEther, 6); /* dest ET addr = source ET addr ??*/ + + /* Dest. IP addr set to broadcast */ + for (i = 0; i <= 3; i++) rarp->ar_data[16 + i] = 0xff; - } NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE); NetSetTimeout(TIMEOUT * CFG_HZ, RarpTimeout); NetSetHandler(RarpHandler); } - #endif /* CONFIG_CMD_NET */ diff --git a/u-boot/net/tftp.c b/u-boot/net/tftp.c index 53c6229..9a0d4fc 100644 --- a/u-boot/net/tftp.c +++ b/u-boot/net/tftp.c @@ -16,20 +16,19 @@ DECLARE_GLOBAL_DATA_PTR; -#define WELL_KNOWN_PORT 69 /* Well known TFTP port # */ -#define TIMEOUT 5 /* Seconds to timeout for a lost pkt */ +#define WELL_KNOWN_PORT 69 /* Well known TFTP port # */ +#define TIMEOUT 5 /* Seconds to timeout for a lost pkt */ -#ifndef CONFIG_NET_RETRY_COUNT - #define TIMEOUT_COUNT 10 /* # of timeouts before giving up */ +#if !defined(CONFIG_NET_RETRY_COUNT) + #define TIMEOUT_COUNT 10 /* # of timeouts before giving up */ #else - #define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2) + #define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2) #endif -/* (for checking the image size) */ -#define HASHES_PER_LINE 40 /* Number of "loading" hashes per line */ +#define HASHES_PER_LINE 40 /* Number of "loading"/"saving" hashes per line */ /* - * TFTP operations. + * TFTP operations */ #define TFTP_RRQ 1 #define TFTP_WRQ 2 @@ -38,57 +37,59 @@ DECLARE_GLOBAL_DATA_PTR; #define TFTP_ERROR 5 #define TFTP_OACK 6 - -static int TftpServerPort; /* The UDP port at their end */ -static int TftpOurPort; /* The UDP port at our end */ +static int TftpServerPort; /* The UDP port at their end */ +static int TftpOurPort; /* The UDP port at our end */ static int TftpTimeoutCount; static int TftpState; -static int TftpWriting; /* 1 if writing, else 0 */ -static int TftpFinalBlock; /* 1 if we have sent the last block */ - -static ulong TftpBlock; /* packet sequence number */ -static ulong TftpLastBlock; /* last packet sequence number received */ -static ulong TftpBlockWrap; /* count of sequence number wraparounds */ -static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */ - -#define STATE_RRQ 1 -#define STATE_DATA 2 -#define STATE_TOO_LARGE 3 -#define STATE_BAD_MAGIC 4 -#define STATE_OACK 5 +static int TftpWriting; /* 1 if writing, else 0 */ +static int TftpFinalBlock; /* 1 if we have sent the last block */ + +static ulong TftpBlock; /* Packet sequence number */ +static ulong TftpLastBlock; /* Last packet sequence number received */ +static ulong TftpBlockWrap; /* Count of sequence number wraparounds */ +static ulong TftpBlockWrapOffset; /* Memory offset due to wrapping */ + +#define STATE_RRQ 1 +#define STATE_DATA 2 +#define STATE_TOO_LARGE 3 +#define STATE_BAD_MAGIC 4 +#define STATE_OACK 5 #define STATE_WRQ 7 -#define TFTP_BLOCK_SIZE 512 /* default TFTP block size */ -#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */ +#define TFTP_BLOCK_SIZE 512 /* Default TFTP block size */ +#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* Sequence number is 16 bit */ #define DEFAULT_NAME_LEN (8 + 4 + 1) static char default_filename[DEFAULT_NAME_LEN]; static char *tftp_filename; -#ifdef CFG_DIRECT_FLASH_TFTP +#if defined(CFG_DIRECT_FLASH_TFTP) extern flash_info_t flash_info[]; #endif -static __inline__ void store_block(unsigned block, uchar * src, unsigned len){ - ulong offset = block * TFTP_BLOCK_SIZE + TftpBlockWrapOffset; +static __inline__ void store_block(unsigned block, uchar *src, unsigned len) +{ + ulong offset = block * TFTP_BLOCK_SIZE + TftpBlockWrapOffset; ulong newsize = offset + len; -#ifdef CFG_DIRECT_FLASH_TFTP +#if defined(CFG_DIRECT_FLASH_TFTP) int i, rc = 0; - for(i=0; i= flash_info[i].start[0]){ + for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) { + /* Start address in flash? */ + if (load_addr + offset >= flash_info[i].start[0]) { rc = 1; break; } } - if(rc){ /* Flash is destination for this packet */ - rc = flash_write((char *)src, (ulong)(load_addr+offset), len); + /* Flash is destination for this packet */ + if (rc) { + rc = flash_write((char *)src, (ulong)(load_addr + offset), + len); - if(rc){ + if (rc) { flash_perror(rc); NetState = NETLOOP_FAIL; return; @@ -99,9 +100,8 @@ static __inline__ void store_block(unsigned block, uchar * src, unsigned len){ (void)memcpy((void *)(load_addr + offset), src, len); } - if(NetBootFileXferSize < newsize){ + if (NetBootFileXferSize < newsize) NetBootFileXferSize = newsize; - } } /* Load the next block from memory to be sent over tftp */ @@ -128,243 +128,240 @@ static void show_progress(const ulong TftpBlock) static void show_success(void) { puts("\n\nTFTP transfer complete!\n"); - NetState = NETLOOP_SUCCESS; } static void TftpSend(void); static void TftpTimeout(void); -/**********************************************************************/ - -static void TftpSend(void){ - uchar *pkt; - volatile uchar *xp; +static void TftpSend(void) +{ int len = 0; + uchar *pkt, *xp; volatile ushort *s; /* - * We will always be sending some sort of packet, so - * cobble together the packet headers now. + * We will always be sending some sort of packet, so + * cobble together the packet headers now. */ pkt = (uchar *)(NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE); - switch(TftpState){ - case STATE_RRQ: - case STATE_WRQ: - xp = pkt; - s = (ushort *)pkt; - *s++ = htons(TftpState == STATE_RRQ ? TFTP_RRQ : TFTP_WRQ); - - pkt = (uchar *)s; - strcpy ((char *)pkt, tftp_filename); - - pkt += strlen(tftp_filename) + 1; - strcpy ((char *)pkt, "octet"); - - pkt += 5 /*strlen("octet")*/ + 1; - strcpy ((char *)pkt, "timeout"); - - pkt += 7 /*strlen("timeout")*/ + 1; - sprintf((char *)pkt, "%d", TIMEOUT); - #ifdef ET_DEBUG - printf("send option \"timeout %s\"\n", (char *)pkt); - #endif - pkt += strlen((char *)pkt) + 1; - len = pkt - xp; - break; + switch (TftpState) { + case STATE_RRQ: + case STATE_WRQ: + xp = pkt; + s = (ushort *)pkt; + *s++ = htons(TftpState == STATE_RRQ ? TFTP_RRQ : TFTP_WRQ); - case STATE_DATA: - case STATE_OACK: - xp = pkt; - s = (ushort *)pkt; + pkt = (uchar *)s; + strcpy((char *)pkt, tftp_filename); - s[0] = htons(TFTP_ACK); - s[1] = htons(TftpBlock); - pkt = (uchar *)(s + 2); + pkt += strlen(tftp_filename) + 1; + strcpy((char *)pkt, "octet"); - if (TftpWriting) { - int toload = TFTP_BLOCK_SIZE; - int loaded = load_block(TftpBlock, pkt, toload); + pkt += 6; /* strlen("octet") + 1 */ + strcpy((char *)pkt, "timeout"); - s[0] = htons(TFTP_DATA); - pkt += loaded; + pkt += 8; /* strlen("timeout") + 1 */ + sprintf((char *)pkt, "%d", TIMEOUT); - TftpFinalBlock = (loaded < toload); - } +#if defined(ET_DEBUG) + printf("send option \"timeout %s\"\n", (char *)pkt); +#endif - len = pkt - xp; - break; + pkt += strlen((char *)pkt) + 1; + len = pkt - xp; + break; + case STATE_DATA: + case STATE_OACK: + xp = pkt; + s = (ushort *)pkt; - case STATE_TOO_LARGE: - xp = pkt; - s = (ushort *)pkt; - *s++ = htons(TFTP_ERROR); - *s++ = htons(3); - pkt = (uchar *)s; - strcpy((char *)pkt, "File too large"); - pkt += 14 /*strlen("File too large")*/ + 1; - len = pkt - xp; - break; + s[0] = htons(TFTP_ACK); + s[1] = htons(TftpBlock); + pkt = (uchar *)(s + 2); - case STATE_BAD_MAGIC: - xp = pkt; - s = (ushort *)pkt; - *s++ = htons(TFTP_ERROR); - *s++ = htons(2); - pkt = (uchar *)s; - strcpy((char *)pkt, "File has bad magic"); - pkt += 18 /*strlen("File has bad magic")*/ + 1; - len = pkt - xp; - break; + if (TftpWriting) { + int toload = TFTP_BLOCK_SIZE; + int loaded = load_block(TftpBlock, pkt, toload); + + s[0] = htons(TFTP_DATA); + pkt += loaded; + + TftpFinalBlock = (loaded < toload); + } + + len = pkt - xp; + break; + case STATE_TOO_LARGE: + xp = pkt; + s = (ushort *)pkt; + *s++ = htons(TFTP_ERROR); + *s++ = htons(3); + pkt = (uchar *)s; + strcpy((char *)pkt, "File too large"); + pkt += 15; /* strlen("File too large") + 1 */ + len = pkt - xp; + break; + case STATE_BAD_MAGIC: + xp = pkt; + s = (ushort *)pkt; + *s++ = htons(TFTP_ERROR); + *s++ = htons(2); + pkt = (uchar *)s; + strcpy((char *)pkt, "File has bad magic"); + pkt += 19; /* strlen("File has bad magic") + 1 */ + len = pkt - xp; + break; } - NetSendUDPPacket(NetServerEther, NetServerIP, TftpServerPort, TftpOurPort, len); + NetSendUDPPacket(NetServerEther, NetServerIP, TftpServerPort, + TftpOurPort, len); } -static void TftpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len){ - bd_t *bd = gd->bd; - ushort proto; - ushort *s; +static void TftpHandler(uchar *pkt, unsigned dest, unsigned src, unsigned len) +{ int block; + ushort proto, *s; + bd_t *bd = gd->bd; - if(dest != TftpOurPort){ + if (dest != TftpOurPort) return; - } if (TftpState != STATE_RRQ && TftpState != STATE_WRQ && - src != TftpServerPort) { + src != TftpServerPort) return; - } - if(len < 2){ + if (len < 2) return; - } len -= 2; - /* warning: don't use increment (++) in ntohs() macros!! */ + /* Warning: don't use increment (++) in ntohs() macros!! */ s = (ushort *)pkt; proto = *s++; pkt = (uchar *)s; block = ntohs(*s); - switch(ntohs(proto)){ - case TFTP_RRQ: - case TFTP_WRQ: - break; + switch (ntohs(proto)) { + case TFTP_RRQ: + case TFTP_WRQ: + break; + case TFTP_ACK: + if (TftpWriting) { + if (TftpFinalBlock) { + show_success(); + } else { + show_progress(TftpBlock); + TftpBlock = block + 1; - case TFTP_ACK: - if (TftpWriting) { - if (TftpFinalBlock) { - show_success(); - } else { - show_progress(TftpBlock); - TftpBlock = block + 1; - TftpSend(); /* Send next data block */ - } + NetSetTimeout(TIMEOUT * CFG_HZ, TftpTimeout); + + TftpSend(); } - break; + } - default: - break; + break; + case TFTP_OACK: +#if defined(ET_DEBUG) + printf("Got OACK: %s %s\n", pkt, pkt+strlen(pkt)+1); +#endif + TftpState = STATE_OACK; + TftpServerPort = src; - case TFTP_OACK: - #ifdef ET_DEBUG - printf("Got OACK: %s %s\n", pkt, pkt+strlen(pkt)+1); - #endif - TftpState = STATE_OACK; - TftpServerPort = src; + if (TftpWriting) { + /* Get ready to send the first block */ + TftpState = STATE_DATA; + TftpBlock++; + } - if (TftpWriting) { - /* Get ready to send the first block */ - TftpState = STATE_DATA; - TftpBlock++; - } + NetSetTimeout(TIMEOUT * CFG_HZ, TftpTimeout); - TftpSend(); /* Send ACK */ - break; + TftpSend(); + break; + case TFTP_DATA: + if (len < 2) + return; - // TFTP DATA PACKET - case TFTP_DATA: - if(len < 2){ - return; - } + len -= 2; + TftpBlock = ntohs(*(ushort *)pkt); - len -= 2; - TftpBlock = ntohs(*(ushort *)pkt); - - /* - * RFC1350 specifies that the first data packet will - * have sequence number 1. If we receive a sequence - * number of 0 this means that there was a wrap - * around of the (16 bit) counter. - */ - if(TftpBlock == 0){ - TftpBlockWrap++; - TftpBlockWrapOffset += TFTP_BLOCK_SIZE * TFTP_SEQUENCE_SIZE; - printf("\n %lu MB received\n ", TftpBlockWrapOffset>>20); - } else - show_progress(TftpBlock); + /* + * RFC1350 specifies that the first data packet will + * have sequence number 1. If we receive a sequence + * number of 0 this means that there was a wrap + * around of the (16 bit) counter. + */ + if (TftpBlock == 0) { + TftpBlockWrap++; + TftpBlockWrapOffset += TFTP_BLOCK_SIZE * TFTP_SEQUENCE_SIZE; - #ifdef ET_DEBUG - if(TftpState == STATE_RRQ){ - printf_err("server did not acknowledge timeout option!\n"); - } - #endif - - if(TftpState == STATE_RRQ || TftpState == STATE_OACK){ - /* first block received */ - TftpState = STATE_DATA; - TftpServerPort = src; - TftpLastBlock = 0; - TftpBlockWrap = 0; - TftpBlockWrapOffset = 0; - - if(TftpBlock != 1){ /* Assertion */ - puts("\n"); - printf_err("first block is not block 1 (%ld), starting again!\n\n", TftpBlock); - NetStartAgain(); - break; - } - } + puts("\n"); + puts("\n "); + printf("[ %lu MB ]\n", + TftpBlockWrapOffset >> 20); + puts("\n "); + } else + show_progress(TftpBlock); + +#if defined(ET_DEBUG) + if (TftpState == STATE_RRQ) + printf_err("server did not ACK timeout option!\n"); +#endif - if(TftpBlock == TftpLastBlock){ - /* - * Same block again; ignore it. - */ + if (TftpState == STATE_RRQ || TftpState == STATE_OACK) { + /* First block received */ + TftpState = STATE_DATA; + TftpServerPort = src; + TftpLastBlock = 0; + TftpBlockWrap = 0; + TftpBlockWrapOffset = 0; + + if (TftpBlock != 1) { + puts("\n"); + printf_err("first block is not block 1 (%ld), starting again!\n\n", TftpBlock); + NetStartAgain(); break; } + } - TftpLastBlock = TftpBlock; - NetSetTimeout(TIMEOUT * CFG_HZ, TftpTimeout); + /* Same block again; ignore it */ + if (TftpBlock == TftpLastBlock) + break; - store_block(TftpBlock - 1, pkt + 2, len); + TftpLastBlock = TftpBlock; + NetSetTimeout(TIMEOUT * CFG_HZ, TftpTimeout); - /* - * Acknoledge the block just received, which will prompt - * the server for the next one. - */ - TftpSend(); + store_block(TftpBlock - 1, pkt + 2, len); - if(len < TFTP_BLOCK_SIZE) - show_success(); + /* + * Acknoledge the block just received, which will prompt + * the server for the next one. + */ + TftpSend(); - break; + if (len < TFTP_BLOCK_SIZE) + show_success(); - case TFTP_ERROR: - puts("\n"); - printf_err("'%s' (%d), starting again!\n\n", pkt + 2, ntohs(*(ushort *)pkt)); - NetStartAgain(); - break; + break; + case TFTP_ERROR: + puts("\n"); + printf_err("'%s' (%d), starting again!\n\n", + pkt + 2, ntohs(*(ushort *)pkt)); + + NetStartAgain(); + break; + default: + break; } } -static void TftpTimeout(void){ +static void TftpTimeout(void) +{ bd_t *bd = gd->bd; - if(++TftpTimeoutCount > TIMEOUT_COUNT){ + if (++TftpTimeoutCount > TIMEOUT_COUNT) { puts("\n\n"); printf_err("retry count exceeded, starting again!\n\n"); NetStartAgain(); @@ -375,21 +372,28 @@ static void TftpTimeout(void){ } } -void TftpStart(proto_t protocol){ +void TftpStart(proto_t protocol) +{ bd_t *bd = gd->bd; #ifdef CONFIG_TFTP_PORT - char *ep; /* Environment pointer */ + /* Environment pointer */ + char *ep; #endif - if(BootFile[0] == '\0'){ - sprintf(default_filename, "%02lX%02lX%02lX%02lX.img", NetOurIP & 0xFF, (NetOurIP >> 8) & 0xFF, (NetOurIP >> 16) & 0xFF, (NetOurIP >> 24) & 0xFF); + if (BootFile[0] == '\0') { + sprintf(default_filename, + "%02lX%02lX%02lX%02lX.img", + (NetOurIP >> 0) & 0xFF, + (NetOurIP >> 8) & 0xFF, + (NetOurIP >> 16) & 0xFF, + (NetOurIP >> 24) & 0xFF); + tftp_filename = default_filename; printf_wrn("no boot file name, using: '%s'\n", tftp_filename); - } else { + } else tftp_filename = BootFile; - } printf("\n%s ", protocol == TFTPPUT ? " TFTP to IP:" : "TFTP from IP:"); print_IPaddr(NetServerIP); @@ -398,19 +402,18 @@ void TftpStart(proto_t protocol){ print_IPaddr(NetOurIP); /* Check if we need to send across this subnet */ - if(NetOurGatewayIP && NetOurSubnetMask){ - IPaddr_t OurNet = NetOurIP & NetOurSubnetMask; - IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask; - - if(OurNet != ServerNet){ - puts("\n Gateway IP: "); - print_IPaddr(NetOurGatewayIP) ; - } + if (NetOurGatewayIP && NetOurSubnetMask) { + IPaddr_t OurNet = NetOurIP & NetOurSubnetMask; + IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask; + + if (OurNet != ServerNet) { + puts("\n Gateway IP: "); + print_IPaddr(NetOurGatewayIP) ; + } } printf("\n Filename: %s", tftp_filename); - #if defined(CONFIG_NET_MULTI) printf("\n Using: %s", eth_get_name()); #endif @@ -418,8 +421,9 @@ void TftpStart(proto_t protocol){ TftpWriting = (protocol == TFTPPUT); if (TftpWriting) { - printf("\nSave address: 0x%lx", save_addr); - printf("\n Save size: 0x%lx", save_size); + printf("\nSave address: 0x%lX", save_addr); + printf("\n Save size: 0x%lX (", save_size); + print_size(save_size, ")"); puts("\n\n Sending: *\b"); NetBootFileXferSize = save_size; @@ -427,10 +431,12 @@ void TftpStart(proto_t protocol){ TftpState = STATE_WRQ; } else { - printf("\nLoad address: 0x%lx", load_addr); + printf("\nLoad address: 0x%lX", load_addr); - if (NetBootFileSize) - printf("\n Load size: 0x%lx", NetBootFileSize << 9); + if (NetBootFileSize) { + printf("\n Load size: 0x%lX (", NetBootFileSize << 9); + print_size(NetBootFileSize << 9, ")"); + } puts("\n\n Loading: *\b"); @@ -440,27 +446,25 @@ void TftpStart(proto_t protocol){ NetSetTimeout(TIMEOUT * CFG_HZ, TftpTimeout); NetSetHandler(TftpHandler); - TftpServerPort = WELL_KNOWN_PORT; + TftpServerPort = WELL_KNOWN_PORT; TftpTimeoutCount = 0; /* Use a pseudo-random port unless a specific port is set */ TftpOurPort = 1024 + (get_timer(0) % 3072); -#ifdef CONFIG_TFTP_PORT - if((ep = getenv("tftpdstp")) != NULL){ +#if defined(CONFIG_TFTP_PORT) + if ((ep = getenv("tftpdstp")) != NULL) TftpServerPort = simple_strtol(ep, NULL, 10); - } - if((ep = getenv("tftpsrcp")) != NULL){ + + if ((ep = getenv("tftpsrcp")) != NULL) TftpOurPort= simple_strtol(ep, NULL, 10); - } #endif TftpBlock = 0; - /* zero out server ether in case the server ip has changed */ + /* Zero out server ether in case the server ip has changed */ memset(NetServerEther, 0, 6); TftpSend(); } - #endif /* CONFIG_CMD_NET */ diff --git a/u-boot/net/tftp.h b/u-boot/net/tftp.h index abba625..172e1bf 100644 --- a/u-boot/net/tftp.h +++ b/u-boot/net/tftp.h @@ -8,14 +8,11 @@ #ifndef __TFTP_H__ #define __TFTP_H__ -/**********************************************************************/ /* - * Global functions and variables. + * Global functions and variables */ -/* tftp.c */ -extern void TftpStart(proto_t protocol); /* Begin TFTP get */ - -/**********************************************************************/ +/* Begin TFTP get */ +extern void TftpStart(proto_t protocol); #endif /* __TFTP_H__ */