From owner-p4-projects@FreeBSD.ORG Mon Apr 10 04:17:27 2006 Return-Path: X-Original-To: p4-projects@freebsd.org Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id E881D16A404; Mon, 10 Apr 2006 04:17:26 +0000 (UTC) X-Original-To: perforce@freebsd.org Delivered-To: perforce@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 9386D16A402 for ; Mon, 10 Apr 2006 04:17:26 +0000 (UTC) (envelope-from imp@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id 5B85343D48 for ; Mon, 10 Apr 2006 04:17:26 +0000 (GMT) (envelope-from imp@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.13.1/8.13.1) with ESMTP id k3A4HQGk018071 for ; Mon, 10 Apr 2006 04:17:26 GMT (envelope-from imp@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.1/8.13.1/Submit) id k3A4HQ04018063 for perforce@freebsd.org; Mon, 10 Apr 2006 04:17:26 GMT (envelope-from imp@freebsd.org) Date: Mon, 10 Apr 2006 04:17:26 GMT Message-Id: <200604100417.k3A4HQ04018063@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to imp@freebsd.org using -f From: Warner Losh To: Perforce Change Reviews Cc: Subject: PERFORCE change 94896 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 10 Apr 2006 04:17:27 -0000 http://perforce.freebsd.org/chv.cgi?CH=94896 Change 94896 by imp@imp_hammer on 2006/04/10 04:16:57 You don't need a complicated state machine for xmodem, at least the limited subset we're doing. Optimize things a bit. This gets us another 50 bytes: 1164 bytes. 1024 in striking distance, but those last 140 bytes are going to be very hard. Good think we have about 11k to run in :-). Affected files ... .. //depot/projects/arm/src/sys/boot/arm/kb920x/boot0/xmodem.c#2 edit Differences ... ==== //depot/projects/arm/src/sys/boot/arm/kb920x/boot0/xmodem.c#2 (text+ko) ==== @@ -43,11 +43,6 @@ SEND_NAK }; -static char packetNumber; - -#define TransitionState(x, y) (x = y) - - /* * .KB_C_FN_DEFINITION_START * int GetRecord(char , char *) @@ -55,25 +50,27 @@ * returns non-zero on success. * .KB_C_FN_DEFINITION_END */ -static int GetRecord(char blocknum, char *dest) { +static int +GetRecord(char blocknum, char *dest) +{ int size; char nextChar; unsigned chk, j; chk = 0; - if (!WaitForChar(&nextChar, 1)) { - return (0); - } - - if ((char)nextChar != (char)~blocknum) { - return (0); - } - + if (!WaitForChar(&nextChar, 1)) + goto err; + if (nextChar != blocknum) + goto err; + if (!WaitForChar(&nextChar, 1)) + goto err; + if ((char)nextChar != (char)~blocknum) + goto err; + for (size = 0; size < PACKET_SIZE; ++size) { - if (!WaitForChar(&nextChar, 1)) { - return (0); - } + if (!WaitForChar(&nextChar, 1)) + goto err; chk = chk ^(int)nextChar << 8; for (j = 0; j < 8; ++j) { if (chk & 0x8000) @@ -86,17 +83,17 @@ chk &= 0xFFFF; - if ((!WaitForChar(&nextChar, 1)) || (nextChar != ((chk >> 8) & 0xFF))) { - return (0); - } - - if ((!WaitForChar(&nextChar, 1)) || (nextChar != (chk & 0xFF))) { - return (0); - } - + if ((!WaitForChar(&nextChar, 1)) || (nextChar != ((chk >> 8) & 0xFF))) + goto err; + if ((!WaitForChar(&nextChar, 1)) || (nextChar != (chk & 0xFF))) + goto err; DebugPutc(ACK); return (1); +err:; + DebugPutc(NAK); + // We should allow for resend, but we don't. + return (0); } @@ -111,60 +108,31 @@ * -1 on error. * .KB_C_FN_DEFINITION_END */ -int xmodem_rx(char *dest) { +int +xmodem_rx(char *dest) +{ + int starting; + char packetNumber, nextChar, *startAddress = dest; - int state; - char nextChar, *startAddress = dest; - packetNumber = 1; - state = WAITING_START; + starting = 1; while (1) { - - if (state == WAITING_START) { + if (starting) DebugPutc('C'); - if (WaitForChar(&nextChar, 1)) { - if (nextChar == SOH) { - TransitionState(state, RX_PACKET); - } - } + if (!WaitForChar(&nextChar, 1) || + (nextChar != SOH && nextChar != EOT)) + continue; + if (nextChar == EOT) { + DebugPutc(ACK); + return (dest - startAddress); } - - if (state == WAIT_SOH) { - if (!WaitForChar(&nextChar, 1)) { - return (-1); - } - - if (nextChar == SOH) { - TransitionState(state, RX_PACKET); - } - - if (nextChar == EOT) { - // TransitionState(state, RX_EOT); - DebugPutc(ACK); - return (dest - startAddress); - } - } - - if (state == RX_PACKET) { - if (!WaitForChar(&nextChar, 1)) { - return (-1); - } - - if (nextChar != packetNumber) { - // TransitionState(state, SEND_NAK); - DebugPutc(NAK); - return (-1); - } - - if (GetRecord(packetNumber, dest)) { - dest += PACKET_SIZE; - ++packetNumber; - TransitionState(state, WAIT_SOH); - } else { - return (-1); - } - } + starting = 0; + // Xmodem packets: SOH PKT# ~PKT# 128-bytes CRC16 + if (!GetRecord(packetNumber, dest)) + return (-1); + dest += PACKET_SIZE; + packetNumber++; } // the loop above should return in all cases