diff mbox series

[v1] mtd: fix flash_erase_sector

Message ID 20211013075425.988569-1-hs@denx.de
State Accepted
Headers show
Series [v1] mtd: fix flash_erase_sector | expand

Commit Message

Heiko Schocher Oct. 13, 2021, 7:54 a.m. UTC
commit f0170ea671f: ("mtd: add flash_erase_sector")

introduced some problems with correct start and end
sector calculation. Fix it!

Signed-off-by: Heiko Schocher <hs@denx.de>
---

 corelib/mtd-interface.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

Comments

Stefano Babic Oct. 13, 2021, 1:24 p.m. UTC | #1
On 13.10.21 09:54, Heiko Schocher wrote:
> commit f0170ea671f: ("mtd: add flash_erase_sector")
> 
> introduced some problems with correct start and end
> sector calculation. Fix it!
> 
> Signed-off-by: Heiko Schocher <hs@denx.de>
> ---
> 
>   corelib/mtd-interface.c | 14 +++++++-------
>   1 file changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/corelib/mtd-interface.c b/corelib/mtd-interface.c
> index abed7a1..586a9ba 100644
> --- a/corelib/mtd-interface.c
> +++ b/corelib/mtd-interface.c
> @@ -35,7 +35,7 @@ int flash_erase_sector(int mtdnum, off_t start, size_t size)
>   	struct mtd_dev_info *mtd;
>   	int noskipbad = 0;
>   	int ret = 0;
> -	unsigned int eb, eb_start, eb_cnt, i;
> +	unsigned int eb, eb_start, i, eb_end, end;
>   	uint8_t *buf;
>   	struct flash_description *flash = get_flash_info();
>   
> @@ -48,14 +48,14 @@ int flash_erase_sector(int mtdnum, off_t start, size_t size)
>   
>   	eb_start = start;
>   	size = size ? size : mtd->size;
> -	if (eb_start > size)
> -		return -EINVAL;
>   	if (!mtd->eb_size)
>   		return -EINVAL;
> -	eb_cnt = (size - eb_start) / mtd->eb_size;
> -	if (!eb_cnt)
> -		return -EINVAL;
> +
> +	end = start + size;
>   	eb_start /= mtd->eb_size;
> +	eb_end = end / mtd->eb_size;
> +	if (end % mtd->eb_size)
> +		eb_end++;
>   
>   	if ((fd = open(mtd_device, O_RDWR)) < 0) {
>   		ERROR( "%s: %s: %s", __func__, mtd_device, strerror(errno));
> @@ -73,7 +73,7 @@ int flash_erase_sector(int mtdnum, off_t start, size_t size)
>   		return -ENOMEM;
>   	}
>   
> -	for (eb = 0; eb < eb_start + eb_cnt; eb++) {
> +	for (eb = eb_start; eb < eb_end; eb++) {
>   
>   		/* Always skip bad sectors */
>   		if (!noskipbad) {
> 

Applied to -master, thanks !

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/corelib/mtd-interface.c b/corelib/mtd-interface.c
index abed7a1..586a9ba 100644
--- a/corelib/mtd-interface.c
+++ b/corelib/mtd-interface.c
@@ -35,7 +35,7 @@  int flash_erase_sector(int mtdnum, off_t start, size_t size)
 	struct mtd_dev_info *mtd;
 	int noskipbad = 0;
 	int ret = 0;
-	unsigned int eb, eb_start, eb_cnt, i;
+	unsigned int eb, eb_start, i, eb_end, end;
 	uint8_t *buf;
 	struct flash_description *flash = get_flash_info();
 
@@ -48,14 +48,14 @@  int flash_erase_sector(int mtdnum, off_t start, size_t size)
 
 	eb_start = start;
 	size = size ? size : mtd->size;
-	if (eb_start > size)
-		return -EINVAL;
 	if (!mtd->eb_size)
 		return -EINVAL;
-	eb_cnt = (size - eb_start) / mtd->eb_size;
-	if (!eb_cnt)
-		return -EINVAL;
+
+	end = start + size;
 	eb_start /= mtd->eb_size;
+	eb_end = end / mtd->eb_size;
+	if (end % mtd->eb_size)
+		eb_end++;
 
 	if ((fd = open(mtd_device, O_RDWR)) < 0) {
 		ERROR( "%s: %s: %s", __func__, mtd_device, strerror(errno));
@@ -73,7 +73,7 @@  int flash_erase_sector(int mtdnum, off_t start, size_t size)
 		return -ENOMEM;
 	}
 
-	for (eb = 0; eb < eb_start + eb_cnt; eb++) {
+	for (eb = eb_start; eb < eb_end; eb++) {
 
 		/* Always skip bad sectors */
 		if (!noskipbad) {