From patchwork Tue May 17 10:03:39 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luca Ceresoli X-Patchwork-Id: 95899 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id F0FF4B6EE7 for ; Tue, 17 May 2011 20:05:09 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 0556B2810C; Tue, 17 May 2011 12:04:54 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 0wpbK-Lx8MOk; Tue, 17 May 2011 12:04:53 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 6EDA528105; Tue, 17 May 2011 12:04:39 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 7BC14280F9 for ; Tue, 17 May 2011 12:04:33 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id r1sGimASRIni for ; Tue, 17 May 2011 12:04:29 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from relayer.comelit.it (mail.comelit.it [217.56.59.218]) by theia.denx.de (Postfix) with ESMTP id 054FB280E3 for ; Tue, 17 May 2011 12:04:21 +0200 (CEST) Received: from com-exc-01.comelit.it ([172.24.0.31]) by relayer.comelit.it with Microsoft SMTPSVC(6.0.3790.1830); Tue, 17 May 2011 12:02:18 +0200 Received: from wallace.comelit.it ([172.20.0.35]) by com-exc-01.comelit.it with Microsoft SMTPSVC(6.0.3790.4675); Tue, 17 May 2011 12:04:19 +0200 From: Luca Ceresoli To: u-boot@lists.denx.de Date: Tue, 17 May 2011 12:03:39 +0200 Message-Id: <1305626621-15008-4-git-send-email-luca.ceresoli@comelit.it> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1303143594-5345-1-git-send-email-luca.ceresoli@comelit.it> References: <1303143594-5345-1-git-send-email-luca.ceresoli@comelit.it> X-OriginalArrivalTime: 17 May 2011 10:04:19.0995 (UTC) FILETIME=[CA06E6B0:01CC1479] X-TM-AS-Product-Ver: SMEX-8.6.0.1168-6.500.1024-18140.005 X-TM-AS-Result: No--13.966700-8.000000-31 X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No Cc: Luca Ceresoli Subject: [U-Boot] [PATCH v3 3/5] TFTP: net/tftp.c: add server mode receive X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.9 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de Signed-off-by: Luca Ceresoli Cc: Wolfgang Denk Acked-by: Detlev Zundel --- Changes in v2: none. Changes in v3: - rebased on top of the net/tftp.c cleanup; - removed all #ifdefs that used to remove negligible amounts of compiled code, at the cost of a much less readable source file; after measurements, it turned out this change does not increase code size. net/tftp.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- net/tftp.h | 6 +++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/net/tftp.c b/net/tftp.c index 6386740..6d44298 100644 --- a/net/tftp.c +++ b/net/tftp.c @@ -2,6 +2,8 @@ * Copyright 1994, 1995, 2000 Neil Russell. * (See License) * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de + * Copyright 2011 Comelit Group SpA, + * Luca Ceresoli */ #include @@ -85,6 +87,7 @@ static short TftpNumchars; #define STATE_TOO_LARGE 3 #define STATE_BAD_MAGIC 4 #define STATE_OACK 5 +#define STATE_RECV_WRQ 6 /* default TFTP block size */ #define TFTP_BLOCK_SIZE 512 @@ -257,6 +260,8 @@ TftpSend(void) (Mapsize*8), 0); /*..falling..*/ #endif + + case STATE_RECV_WRQ: case STATE_DATA: xp = pkt; s = (ushort *)pkt; @@ -309,7 +314,8 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, #endif return; } - if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort) + if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort && + TftpState != STATE_RECV_WRQ) return; if (len < 2) @@ -322,12 +328,24 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, switch (ntohs(proto)) { case TFTP_RRQ: - case TFTP_WRQ: case TFTP_ACK: break; default: break; +#ifdef CONFIG_CMD_TFTPSRV + case TFTP_WRQ: + debug("Got WRQ\n"); + TftpRemoteIP = sip; + TftpRemotePort = src; + TftpOurPort = 1024 + (get_timer(0) % 3072); + TftpLastBlock = 0; + TftpBlockWrap = 0; + TftpBlockWrapOffset = 0; + TftpSend(); /* Send ACK(0) */ + break; +#endif + case TFTP_OACK: debug("Got OACK: %s %s\n", pkt, @@ -402,7 +420,8 @@ TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, if (TftpState == STATE_SEND_RRQ) debug("Server did not acknowledge timeout option!\n"); - if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK) { + if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK || + TftpState == STATE_RECV_WRQ) { /* first block received */ TftpState = STATE_DATA; TftpRemotePort = src; @@ -537,7 +556,8 @@ TftpTimeout(void) } else { puts("T "); NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); - TftpSend(); + if (TftpState != STATE_RECV_WRQ) + TftpSend(); } } @@ -661,6 +681,40 @@ TftpStart(void) TftpSend(); } +#ifdef CONFIG_CMD_TFTPSRV +void +TftpStartServer(void) +{ + tftp_filename[0] = 0; + +#if defined(CONFIG_NET_MULTI) + printf("Using %s device\n", eth_get_name()); +#endif + printf("Listening for TFTP transfer on %pI4\n", &NetOurIP); + printf("Load address: 0x%lx\n", load_addr); + + puts("Loading: *\b"); + + TftpTimeoutCountMax = TIMEOUT_COUNT; + TftpTimeoutCount = 0; + TftpTimeoutMSecs = TIMEOUT; + NetSetTimeout(TftpTimeoutMSecs, TftpTimeout); + + /* Revert TftpBlkSize to dflt */ + TftpBlkSize = TFTP_BLOCK_SIZE; + TftpBlock = 0; + TftpOurPort = WELL_KNOWN_PORT; + +#ifdef CONFIG_TFTP_TSIZE + TftpTsize = 0; + TftpNumchars = 0; +#endif + + TftpState = STATE_RECV_WRQ; + NetSetHandler(TftpHandler); +} +#endif /* CONFIG_CMD_TFTPSRV */ + #ifdef CONFIG_MCAST_TFTP /* Credits: atftp project. */ diff --git a/net/tftp.h b/net/tftp.h index e3dfb26..3abdf7b 100644 --- a/net/tftp.h +++ b/net/tftp.h @@ -2,6 +2,8 @@ * LiMon - BOOTP/TFTP. * * Copyright 1994, 1995, 2000 Neil Russell. + * Copyright 2011 Comelit Group SpA + * Luca Ceresoli * (See License) */ @@ -16,6 +18,10 @@ /* tftp.c */ extern void TftpStart (void); /* Begin TFTP get */ +#ifdef CONFIG_CMD_TFTPSRV +extern void TftpStartServer(void); /* Wait for incoming TFTP put */ +#endif + /**********************************************************************/ #endif /* __TFTP_H__ */