diff mbox series

[V2] : Allow flash updates to proceed without decompressed-size set

Message ID b5ec7715957f48aeac8df128b5cdc830@kumkeo.de
State Changes Requested
Headers show
Series [V2] : Allow flash updates to proceed without decompressed-size set | expand

Commit Message

Ulrich Teichert Nov. 7, 2023, 11:57 a.m. UTC
For backward compatibility, allow flash updates to proceed without the
decompression-size property set. In that case, the flash device is erased
from the offset until its end.

Signed-off-by: Ulrich Teichert <ulrich.teichert@kumkeo.de>

---


Mit freundlichen Grüßen / Kind regards

Dipl.-Inform. Ulrich Teichert
Senior Software Developer

kumkeo GmbH
Heidenkampsweg 82a
20097 Hamburg
Germany

T: +49 40 2846761-0
F: +49 40 2846761-99

ulrich.teichert@kumkeo.de
www.kumkeo.de

Amtsgericht Hamburg / Hamburg District Court, HRB 108558
Geschäftsführer / Managing Director: Dipl.-Ing. Bernd Sager; Dipl.-Ing. Sven Tanneberger, MBA

Comments

Stefano Babic Nov. 7, 2023, 12:33 p.m. UTC | #1
Hi Uli,

On 07.11.23 12:57, Ulrich Teichert wrote:
> For backward compatibility, allow flash updates to proceed without the
> decompression-size property set. In that case, the flash device is erased
> from the offset until its end.
> 
> Signed-off-by: Ulrich Teichert <ulrich.teichert@kumkeo.de>
> 
> ---
> 
> diff --git a/corelib/mtd-interface.c b/corelib/mtd-interface.c
> index 22e86bd..5d9e708 100644
> --- a/corelib/mtd-interface.c
> +++ b/corelib/mtd-interface.c
> @@ -217,6 +217,20 @@ int get_mtd_from_name(const char *s)
>          return -1;
>   }
> 
> +long long get_mtd_size(int mtdnum)
> +{
> +       struct flash_description *flash = get_flash_info();
> +       struct mtd_dev_info dev_info;
> +
> +       int err = mtd_get_dev_info1(flash->libmtd, mtdnum, &dev_info);
> +       if (err != 0) {
> +               ERROR("Could not get MTD %d info: %d, %d", mtdnum, err, errno);
> +               return -ENODEV;
> +       }
> +
> +       return dev_info.size;
> +}
> +
>   void ubi_init(void)
>   {
>          struct flash_description *nand = get_flash_info();
> diff --git a/handlers/flash_handler.c b/handlers/flash_handler.c
> index 3cca02b..7117d8a 100644
> --- a/handlers/flash_handler.c
> +++ b/handlers/flash_handler.c
> @@ -310,13 +310,25 @@ static int flash_write_nor(int mtdnum, struct img_type *img)
> 
>          long long size = get_output_size(img, true);
>          if (size < 0) {
> -               ERROR("Failed to determine output size, bailing out.");
> -               return -1;
> +               size = get_mtd_size(mtdnum);
> +               if (size < 0) {
> +                       ERROR("Could not get MTD %d device size", mtdnum);
> +                       return -ENODEV;
> +               }
> +
> +               WARN("decompression-size not set, erasing flash device %s from %lld to %lld",
> +                       img->device, img->seek, size);
> +               if (flash_erase_sector(mtdnum, img->seek, size)) {
> +                       ERROR("Failed to erase %s", img->device);
> +                       return -1;
> +    

Do we need to duplicate this ? This branch just set size, the main 
branch is already calling flash_erase_sector().

            }
>          }
> -       if (flash_erase_sector(mtdnum, img->seek, size)) {
> -               ERROR("Failed to erase sectors on /dev/mtd%d (start: %llu, size: %lld)",
> -                       mtdnum, img->seek, size);
> -               return -1;
> +       else {
> +               if (flash_erase_sector(mtdnum, img->seek, size)) {
> +                       ERROR("Failed to erase sectors on /dev/mtd%d (start: %llu, size: %lld)",
> +                               mtdnum, img->seek, size);
> +                       return -1;
> +               }
>          }

I think it is cleaner if:

           long long size = get_output_size(img, true);
           if (size < 0) {
               size = get_mtd_size(mtdnum);
                if (size < 0) {
                      ERROR("Could not get MTD %d device size", mtdnum);
                       return -ENODEV;
               }
              WARN("decompression-size not set, erasing flash device %s 
from %lld to %lld",
                     img->device, img->seek, size);
	}


         if (flash_erase_sector(mtdnum, img->seek, size)) {
             ERROR("Failed to erase sectors on /dev/mtd%d (start: %llu, 
size: %lld)",
               mtdnum, img->seek, size);
               return -1;
        }

Best regards,
Stefano Babic

> 
>          snprintf(mtd_device, sizeof(mtd_device), "/dev/mtd%d", mtdnum);
> diff --git a/include/flash.h b/include/flash.h
> index 9a055a7..69a0e99 100644
> --- a/include/flash.h
> +++ b/include/flash.h
> @@ -50,6 +50,7 @@ int scan_mtd_devices (void);
>   void mtd_cleanup (void);
>   int get_mtd_from_device(char *s);
>   int get_mtd_from_name(const char *s);
> +long long get_mtd_size(int mtdnum);
>   int flash_erase(int mtdnum);
>   int flash_erase_sector(int mtdnum, off_t start, size_t size);
> 
> 
> Mit freundlichen Grüßen / Kind regards
> 
> Dipl.-Inform. Ulrich Teichert
> Senior Software Developer
> 
> kumkeo GmbH
> Heidenkampsweg 82a
> 20097 Hamburg
> Germany
> 
> T: +49 40 2846761-0
> F: +49 40 2846761-99
> 
> ulrich.teichert@kumkeo.de
> www.kumkeo.de
> 
> Amtsgericht Hamburg / Hamburg District Court, HRB 108558
> Geschäftsführer / Managing Director: Dipl.-Ing. Bernd Sager; Dipl.-Ing. Sven Tanneberger, MBA
>
Ulrich Teichert Nov. 7, 2023, 4:09 p.m. UTC | #2
Hi Stefano,

[del]
>> +               if (size < 0) {
>> +                       ERROR("Could not get MTD %d device size", mtdnum);
>> +                       return -ENODEV;
>> +               }
>> +
>> +               WARN("decompression-size not set, erasing flash device %s from %lld to %lld",
>> +                       img->device, img->seek, size);
>> +               if (flash_erase_sector(mtdnum, img->seek, size)) {
>> +                       ERROR("Failed to erase %s", img->device);
>> +                       return -1;
>> +

>Do we need to duplicate this ? This branch just set size, the main
>branch is already calling flash_erase_sector().

Right, this is a useless duplication now. I'll post a V3 tomorrow hopefully,
I had some problems with my testing setup...

CU,
Uli

Mit freundlichen Grüßen / Kind regards

Dipl.-Inform. Ulrich Teichert
Senior Software Developer

kumkeo GmbH
Heidenkampsweg 82a
20097 Hamburg
Germany

T: +49 40 2846761-0
F: +49 40 2846761-99

ulrich.teichert@kumkeo.de
www.kumkeo.de

Amtsgericht Hamburg / Hamburg District Court, HRB 108558
Geschäftsführer / Managing Director: Dipl.-Ing. Bernd Sager; Dipl.-Ing. Sven Tanneberger, MBA
diff mbox series

Patch

diff --git a/corelib/mtd-interface.c b/corelib/mtd-interface.c
index 22e86bd..5d9e708 100644
--- a/corelib/mtd-interface.c
+++ b/corelib/mtd-interface.c
@@ -217,6 +217,20 @@  int get_mtd_from_name(const char *s)
        return -1;
 }

+long long get_mtd_size(int mtdnum)
+{
+       struct flash_description *flash = get_flash_info();
+       struct mtd_dev_info dev_info;
+
+       int err = mtd_get_dev_info1(flash->libmtd, mtdnum, &dev_info);
+       if (err != 0) {
+               ERROR("Could not get MTD %d info: %d, %d", mtdnum, err, errno);
+               return -ENODEV;
+       }
+
+       return dev_info.size;
+}
+
 void ubi_init(void)
 {
        struct flash_description *nand = get_flash_info();
diff --git a/handlers/flash_handler.c b/handlers/flash_handler.c
index 3cca02b..7117d8a 100644
--- a/handlers/flash_handler.c
+++ b/handlers/flash_handler.c
@@ -310,13 +310,25 @@  static int flash_write_nor(int mtdnum, struct img_type *img)

        long long size = get_output_size(img, true);
        if (size < 0) {
-               ERROR("Failed to determine output size, bailing out.");
-               return -1;
+               size = get_mtd_size(mtdnum);
+               if (size < 0) {
+                       ERROR("Could not get MTD %d device size", mtdnum);
+                       return -ENODEV;
+               }
+
+               WARN("decompression-size not set, erasing flash device %s from %lld to %lld",
+                       img->device, img->seek, size);
+               if (flash_erase_sector(mtdnum, img->seek, size)) {
+                       ERROR("Failed to erase %s", img->device);
+                       return -1;
+               }
        }
-       if (flash_erase_sector(mtdnum, img->seek, size)) {
-               ERROR("Failed to erase sectors on /dev/mtd%d (start: %llu, size: %lld)",
-                       mtdnum, img->seek, size);
-               return -1;
+       else {
+               if (flash_erase_sector(mtdnum, img->seek, size)) {
+                       ERROR("Failed to erase sectors on /dev/mtd%d (start: %llu, size: %lld)",
+                               mtdnum, img->seek, size);
+                       return -1;
+               }
        }

        snprintf(mtd_device, sizeof(mtd_device), "/dev/mtd%d", mtdnum);
diff --git a/include/flash.h b/include/flash.h
index 9a055a7..69a0e99 100644
--- a/include/flash.h
+++ b/include/flash.h
@@ -50,6 +50,7 @@  int scan_mtd_devices (void);
 void mtd_cleanup (void);
 int get_mtd_from_device(char *s);
 int get_mtd_from_name(const char *s);
+long long get_mtd_size(int mtdnum);
 int flash_erase(int mtdnum);
 int flash_erase_sector(int mtdnum, off_t start, size_t size);