[PATCH u-boot-marvell v3 35/39] tools: kwboot: Avoid code repetition in kwboot_img_patch()

Stefan Roese sr at denx.de
Fri Oct 1 08:30:22 CEST 2021


On 24.09.21 23:07, Marek Behún wrote:
> From: Marek Behún <marek.behun at nic.cz>
> 
> Change kwboot_img_patch() to avoid code repetition of setting errno to
> EINVAL.
> 
> 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, 20 insertions(+), 35 deletions(-)
> 
> diff --git a/tools/kwboot.c b/tools/kwboot.c
> index 9eb007f1bb..9da5b42ebf 100644
> --- a/tools/kwboot.c
> +++ b/tools/kwboot.c
> @@ -1386,7 +1386,6 @@ _copy_baudrate_change_code(struct main_hdr_v1 *hdr, void *dst, int pre,
>   static int
>   kwboot_img_patch(void *img, size_t *size, int baudrate)
>   {
> -	int rc;
>   	struct main_hdr_v1 *hdr;
>   	uint32_t srcaddr;
>   	uint8_t csum;
> @@ -1394,33 +1393,25 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
>   	int image_ver;
>   	int is_secure;
>   
> -	rc = -1;
>   	hdr = img;
>   
> -	if (*size < sizeof(struct main_hdr_v1)) {
> -		errno = EINVAL;
> -		goto out;
> -	}
> +	if (*size < sizeof(struct main_hdr_v1))
> +		goto err;
>   
>   	image_ver = kwbimage_version(img);
>   	if (image_ver != 0 && image_ver != 1) {
>   		fprintf(stderr, "Invalid image header version\n");
> -		errno = EINVAL;
> -		goto out;
> +		goto err;
>   	}
>   
>   	hdrsz = kwbheader_size(hdr);
>   
> -	if (*size < hdrsz) {
> -		errno = EINVAL;
> -		goto out;
> -	}
> +	if (*size < hdrsz)
> +		goto err;
>   
>   	csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
> -	if (csum != hdr->checksum) {
> -		errno = EINVAL;
> -		goto out;
> -	}
> +	if (csum != hdr->checksum)
> +		goto err;
>   
>   	if (image_ver == 0) {
>   		struct main_hdr_v0 *hdr_v0 = img;
> @@ -1433,10 +1424,9 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
>   
>   	switch (hdr->blockid) {
>   	case IBR_HDR_SATA_ID:
> -		if (srcaddr < 1) {
> -			errno = EINVAL;
> -			goto out;
> -		}
> +		if (srcaddr < 1)
> +			goto err;
> +
>   		hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512);
>   		break;
>   
> @@ -1459,10 +1449,8 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
>   	}
>   
>   	if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
> -	    *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize)) {
> -		errno = EINVAL;
> -		goto out;
> -	}
> +	    *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize))
> +		goto err;
>   
>   	is_secure = kwboot_img_is_secure(img);
>   
> @@ -1470,8 +1458,7 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
>   		if (is_secure) {
>   			fprintf(stderr,
>   				"Image has secure header with signature for non-UART booting\n");
> -			errno = EINVAL;
> -			goto out;
> +			goto err;
>   		}
>   
>   		kwboot_printv("Patching image boot signature to UART\n");
> @@ -1485,15 +1472,13 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
>   		if (image_ver == 0) {
>   			fprintf(stderr,
>   				"Cannot inject code for changing baudrate into v0 image header\n");
> -			errno = EINVAL;
> -			goto out;
> +			goto err;
>   		}
>   
>   		if (is_secure) {
>   			fprintf(stderr,
>   				"Cannot inject code for changing baudrate into image with secure header\n");
> -			errno = EINVAL;
> -			goto out;
> +			goto err;
>   		}
>   
>   		/*
> @@ -1529,8 +1514,7 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
>   
>   		if (is_secure) {
>   			fprintf(stderr, "Cannot align image with secure header\n");
> -			errno = EINVAL;
> -			goto out;
> +			goto err;
>   		}
>   
>   		kwboot_printv("Aligning image header to Xmodem block size\n");
> @@ -1540,9 +1524,10 @@ kwboot_img_patch(void *img, size_t *size, int baudrate)
>   	hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
>   
>   	*size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
> -	rc = 0;
> -out:
> -	return rc;
> +	return 0;
> +err:
> +	errno = EINVAL;
> +	return -1;
>   }
>   
>   static void
> 


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