[PATCH u-boot-marvell v3 04/39] tools: kwboot: Refactor and fix writing buffer

Stefan Roese sr at denx.de
Fri Oct 1 08:14:46 CEST 2021


On 24.09.21 23:06, Marek Behún wrote:
> From: Marek Behún <marek.behun at nic.cz>
> 
> There are 3 instances in kwboot.c where we need to write() a given
> buffer whole (iteratively writing until all data are written), and 2 of
> those instances are wrong, for they do not increment the buffer pointer.
> 
> Refactor the code into a new function kwboot_write() where it is fixed.
> 
> Signed-off-by: Marek Behún <marek.behun at nic.cz>

Reviewed-by: Stefan Roese <sr at denx.de>

Thanks,
Stefan

> ---
>   tools/kwboot.c | 55 ++++++++++++++++++++++++--------------------------
>   1 file changed, 26 insertions(+), 29 deletions(-)
> 
> diff --git a/tools/kwboot.c b/tools/kwboot.c
> index f18f6d2134..22cdd137ad 100644
> --- a/tools/kwboot.c
> +++ b/tools/kwboot.c
> @@ -72,6 +72,23 @@ static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
>   static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
>   static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
>   
> +static ssize_t
> +kwboot_write(int fd, const char *buf, size_t len)
> +{
> +	size_t tot = 0;
> +
> +	while (tot < len) {
> +		ssize_t wr = write(fd, buf + tot, len - tot);
> +
> +		if (wr < 0)
> +			return -1;
> +
> +		tot += wr;
> +	}
> +
> +	return tot;
> +}
> +
>   static void
>   kwboot_printv(const char *fmt, ...)
>   {
> @@ -191,26 +208,13 @@ out:
>   static int
>   kwboot_tty_send(int fd, const void *buf, size_t len)
>   {
> -	int rc;
> -	ssize_t n;
> -
>   	if (!buf)
>   		return 0;
>   
> -	rc = -1;
> -
> -	do {
> -		n = write(fd, buf, len);
> -		if (n < 0)
> -			goto out;
> -
> -		buf = (char *)buf + n;
> -		len -= n;
> -	} while (len > 0);
> +	if (kwboot_write(fd, buf, len) < 0)
> +		return -1;
>   
> -	rc = tcdrain(fd);
> -out:
> -	return rc;
> +	return tcdrain(fd);
>   }
>   
>   static int
> @@ -462,7 +466,7 @@ can:
>   static int
>   kwboot_term_pipe(int in, int out, const char *quit, int *s)
>   {
> -	ssize_t nin, nout;
> +	ssize_t nin;
>   	char _buf[128], *buf = _buf;
>   
>   	nin = read(in, buf, sizeof(_buf));
> @@ -480,22 +484,15 @@ kwboot_term_pipe(int in, int out, const char *quit, int *s)
>   				buf++;
>   				nin--;
>   			} else {
> -				while (*s > 0) {
> -					nout = write(out, quit, *s);
> -					if (nout <= 0)
> -						return -1;
> -					(*s) -= nout;
> -				}
> +				if (kwboot_write(out, quit, *s) < 0)
> +					return -1;
> +				*s = 0;
>   			}
>   		}
>   	}
>   
> -	while (nin > 0) {
> -		nout = write(out, buf, nin);
> -		if (nout <= 0)
> -			return -1;
> -		nin -= nout;
> -	}
> +	if (kwboot_write(out, buf, nin) < 0)
> +		return -1;
>   
>   	return 0;
>   }
> 


Viele Grüße,
Stefan

-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr at denx.de


More information about the U-Boot mailing list