diff mbox series

[3/3] cmd: fix return code of 'sf erase'

Message ID 20230102172116.25369-4-heinrich.schuchardt@canonical.com
State Accepted
Commit ee0b0be204aad0cee41167546fa3fc82f2aa57e8
Delegated to: Jagannadha Sutradharudu Teki
Headers show
Series cmd: fix return code of sf command | expand

Commit Message

Heinrich Schuchardt Jan. 2, 2023, 5:21 p.m. UTC
If the offset or the size passed to the 'sf erase' command exceeds
the size of the SPI flash displaying the command usage is not
helpful. Return CMD_RET_FAILURE instead of CMD_RET_USAGE.

Use the CMD_RET_* constants instead of 0, 1, -1.

Simplify a logical expression in the final return statement.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
---
 cmd/sf.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Comments

Dhruva Gole Jan. 3, 2023, 9:13 a.m. UTC | #1
On 02/01/23 22:51, Heinrich Schuchardt wrote:
> If the offset or the size passed to the 'sf erase' command exceeds
> the size of the SPI flash displaying the command usage is not
> helpful. Return CMD_RET_FAILURE instead of CMD_RET_USAGE.
> 
> Use the CMD_RET_* constants instead of 0, 1, -1.
> 
> Simplify a logical expression in the final return statement.
> 
> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
> ---

This series makes the code more readable and so,

Acked-by: Dhruva Gole <d-gole@ti.com>

>   cmd/sf.c | 12 ++++++------
>   1 file changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/cmd/sf.c b/cmd/sf.c
> index 2a88430681..a28cccb338 100644
> --- a/cmd/sf.c
> +++ b/cmd/sf.c
> @@ -343,27 +343,27 @@ static int do_spi_flash_erase(int argc, char *const argv[])
>   	ulong size;
>   
>   	if (argc < 3)
> -		return -1;
> +		return CMD_RET_USAGE;
>   
>   	if (mtd_arg_off(argv[1], &dev, &offset, &len, &maxsize,
>   			MTD_DEV_TYPE_NOR, flash->size))
> -		return -1;
> +		return CMD_RET_FAILURE;
>   
>   	ret = sf_parse_len_arg(argv[2], &size);
>   	if (ret != 1)
> -		return -1;
> +		return CMD_RET_USAGE;
>   
>   	/* Consistency checking */
>   	if (offset + size > flash->size) {
>   		printf("ERROR: attempting %s past flash size (%#x)\n",
>   		       argv[0], flash->size);
> -		return 1;
> +		return CMD_RET_FAILURE;
>   	}
>   
>   	if (flash->flash_is_unlocked &&
>   	    !flash->flash_is_unlocked(flash, offset, len)) {
>   		printf("ERROR: flash area is locked\n");
> -		return 1;
> +		return CMD_RET_FAILURE;
>   	}
>   
>   	ret = spi_flash_erase(flash, offset, size);
> @@ -373,7 +373,7 @@ static int do_spi_flash_erase(int argc, char *const argv[])
>   	else
>   		printf("OK\n");
>   
> -	return ret == 0 ? 0 : 1;
> +	return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
>   }
>   
>   static int do_spi_protect(int argc, char *const argv[])
diff mbox series

Patch

diff --git a/cmd/sf.c b/cmd/sf.c
index 2a88430681..a28cccb338 100644
--- a/cmd/sf.c
+++ b/cmd/sf.c
@@ -343,27 +343,27 @@  static int do_spi_flash_erase(int argc, char *const argv[])
 	ulong size;
 
 	if (argc < 3)
-		return -1;
+		return CMD_RET_USAGE;
 
 	if (mtd_arg_off(argv[1], &dev, &offset, &len, &maxsize,
 			MTD_DEV_TYPE_NOR, flash->size))
-		return -1;
+		return CMD_RET_FAILURE;
 
 	ret = sf_parse_len_arg(argv[2], &size);
 	if (ret != 1)
-		return -1;
+		return CMD_RET_USAGE;
 
 	/* Consistency checking */
 	if (offset + size > flash->size) {
 		printf("ERROR: attempting %s past flash size (%#x)\n",
 		       argv[0], flash->size);
-		return 1;
+		return CMD_RET_FAILURE;
 	}
 
 	if (flash->flash_is_unlocked &&
 	    !flash->flash_is_unlocked(flash, offset, len)) {
 		printf("ERROR: flash area is locked\n");
-		return 1;
+		return CMD_RET_FAILURE;
 	}
 
 	ret = spi_flash_erase(flash, offset, size);
@@ -373,7 +373,7 @@  static int do_spi_flash_erase(int argc, char *const argv[])
 	else
 		printf("OK\n");
 
-	return ret == 0 ? 0 : 1;
+	return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
 }
 
 static int do_spi_protect(int argc, char *const argv[])