diff mbox series

[1/1] diskpart: fix bug in partition comparision of autogenerated sizes

Message ID 20210517092623.36806-1-james.hilliard1@gmail.com
State Accepted
Headers show
Series [1/1] diskpart: fix bug in partition comparision of autogenerated sizes | expand

Commit Message

James Hilliard May 17, 2021, 9:26 a.m. UTC
In order for diskpart_partition_cmp to properly compare the size
of partitions that are autogenerated we need to reload the table
against the context before the comparision.

Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 handlers/diskpart_handler.c | 37 +++++++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)

Comments

Stefano Babic May 17, 2021, 9:32 a.m. UTC | #1
Hi James,

I sent yesterday a patch to fix this - have you seen it ?

Best regards,
Stefano Babic

On 17.05.21 11:26, James Hilliard wrote:
> In order for diskpart_partition_cmp to properly compare the size
> of partitions that are autogenerated we need to reload the table
> against the context before the comparision.
> 
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> ---
>   handlers/diskpart_handler.c | 37 +++++++++++++++++++++++++++++++------
>   1 file changed, 31 insertions(+), 6 deletions(-)
> 
> diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
> index b10ffc6..2cb524a 100644
> --- a/handlers/diskpart_handler.c
> +++ b/handlers/diskpart_handler.c
> @@ -161,6 +161,29 @@ static bool diskpart_partition_cmp(const char *lbtype, struct fdisk_partition *f
>   	return false;
>   }
>   
> +static int diskpart_reload_table(struct fdisk_context *cxt, struct fdisk_table *tb)
> +{
> +	int ret = 0;
> +
> +	ret = fdisk_delete_all_partitions(cxt);
> +	if (ret) {
> +		ERROR("Partition table cannot be deleted: %d", ret);
> +		return ret;
> +	}
> +	ret = fdisk_apply_table(cxt, tb);
> +	if (ret) {
> +		ERROR("Partition table cannot be applied: %d", ret);
> +		return ret;
> +	}
> +	fdisk_reset_table(tb);
> +	ret = fdisk_get_partitions(cxt, &tb);
> +	if (ret) {
> +		ERROR("Error loading applied table %d:", ret);
> +		return ret;
> +	}
> +	return ret;
> +}
> +
>   static int diskpart(struct img_type *img,
>   	void __attribute__ ((__unused__)) *data)
>   {
> @@ -368,6 +391,14 @@ static int diskpart(struct img_type *img,
>   		i++;
>   	}
>   
> +	/*
> +	 * Reload new table against the context to populate default values
> +	 * so that we can compare partitions properly.
> +	 */
> +	ret = diskpart_reload_table(cxt, tb);
> +	if (ret)
> +		goto handler_exit;
> +
>   	/*
>   	 * A partiton table was found on disk, now compares the two tables
>   	 * to check if they differ.
> @@ -407,12 +438,6 @@ static int diskpart(struct img_type *img,
>   
>   	if (createtable) {
>   		TRACE("Partitions on disk differ, write to disk;");
> -		fdisk_delete_all_partitions(cxt);
> -		ret = fdisk_apply_table(cxt, tb);
> -		if (ret) {
> -			ERROR("Partition table cannot be applied !");
> -			goto handler_exit;
> -		}
>   
>   		/*
>   		 * Everything done, write into disk
>
James Hilliard May 17, 2021, 9:47 a.m. UTC | #2
On Mon, May 17, 2021 at 3:32 AM Stefano Babic <sbabic@denx.de> wrote:
>
> Hi James,
>
> I sent yesterday a patch to fix this - have you seen it ?
Yes, but it doesn't fix the issue, and I think that approach is
inherently unreliable
as it doesn't appear to appropriately replicate the fdisk auto generation logic
which is rather complex.

See this example test case which appears to work with my patch but not yours:
software =
{
        version = "0.1.0";

        partitions: (
        {
                type = "diskpart";
                device = "/dev/sdb";
                properties: {
                        labeltype = "gpt";
                        partition-1 = [
                                "size=550M",
                                "start=2048",
                                "name=boot",
                                "type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B",
                                "fstype=vfat"
                        ];
                        partition-2 = [
                                "size=1G",
                                "name=main",
                                "type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
                                "fstype=ext4"
                        ];
                        partition-3 = [
                                "size=1G",
                                "name=alt",
                                "type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
                                "fstype=ext4"
                        ];
                        partition-4 = [
                                "name=srv",
                                "type=3B8F8425-20E0-4F3B-907F-1A25A76F98E8",
                                "fstype=ext4"
                        ];
                }
        });
}

>
> Best regards,
> Stefano Babic
>
> On 17.05.21 11:26, James Hilliard wrote:
> > In order for diskpart_partition_cmp to properly compare the size
> > of partitions that are autogenerated we need to reload the table
> > against the context before the comparision.
> >
> > Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> > ---
> >   handlers/diskpart_handler.c | 37 +++++++++++++++++++++++++++++++------
> >   1 file changed, 31 insertions(+), 6 deletions(-)
> >
> > diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
> > index b10ffc6..2cb524a 100644
> > --- a/handlers/diskpart_handler.c
> > +++ b/handlers/diskpart_handler.c
> > @@ -161,6 +161,29 @@ static bool diskpart_partition_cmp(const char *lbtype, struct fdisk_partition *f
> >       return false;
> >   }
> >
> > +static int diskpart_reload_table(struct fdisk_context *cxt, struct fdisk_table *tb)
> > +{
> > +     int ret = 0;
> > +
> > +     ret = fdisk_delete_all_partitions(cxt);
> > +     if (ret) {
> > +             ERROR("Partition table cannot be deleted: %d", ret);
> > +             return ret;
> > +     }
> > +     ret = fdisk_apply_table(cxt, tb);
> > +     if (ret) {
> > +             ERROR("Partition table cannot be applied: %d", ret);
> > +             return ret;
> > +     }
> > +     fdisk_reset_table(tb);
> > +     ret = fdisk_get_partitions(cxt, &tb);
> > +     if (ret) {
> > +             ERROR("Error loading applied table %d:", ret);
> > +             return ret;
> > +     }
> > +     return ret;
> > +}
> > +
> >   static int diskpart(struct img_type *img,
> >       void __attribute__ ((__unused__)) *data)
> >   {
> > @@ -368,6 +391,14 @@ static int diskpart(struct img_type *img,
> >               i++;
> >       }
> >
> > +     /*
> > +      * Reload new table against the context to populate default values
> > +      * so that we can compare partitions properly.
> > +      */
> > +     ret = diskpart_reload_table(cxt, tb);
> > +     if (ret)
> > +             goto handler_exit;
> > +
> >       /*
> >        * A partiton table was found on disk, now compares the two tables
> >        * to check if they differ.
> > @@ -407,12 +438,6 @@ static int diskpart(struct img_type *img,
> >
> >       if (createtable) {
> >               TRACE("Partitions on disk differ, write to disk;");
> > -             fdisk_delete_all_partitions(cxt);
> > -             ret = fdisk_apply_table(cxt, tb);
> > -             if (ret) {
> > -                     ERROR("Partition table cannot be applied !");
> > -                     goto handler_exit;
> > -             }
> >
> >               /*
> >                * Everything done, write into disk
> >
>
>
> --
> =====================================================================
> DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
> HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
> Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic@denx.de
> =====================================================================
Stefano Babic May 17, 2021, 9:57 a.m. UTC | #3
Hi James,

On 17.05.21 11:47, James Hilliard wrote:
> On Mon, May 17, 2021 at 3:32 AM Stefano Babic <sbabic@denx.de> wrote:
>>
>> Hi James,
>>
>> I sent yesterday a patch to fix this - have you seen it ?
> Yes, but it doesn't fix the issue, and I think that approach is
> inherently unreliable
> as it doesn't appear to appropriately replicate the fdisk auto generation logic
> which is rather complex.
> 
> See this example test case which appears to work with my patch but not yours:
> software =
> {
>          version = "0.1.0";
> 
>          partitions: (
>          {
>                  type = "diskpart";
>                  device = "/dev/sdb";
>                  properties: {
>                          labeltype = "gpt";
>                          partition-1 = [
>                                  "size=550M",
>                                  "start=2048",
>                                  "name=boot",
>                                  "type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B",
>                                  "fstype=vfat"
>                          ];
>                          partition-2 = [
>                                  "size=1G",
>                                  "name=main",
>                                  "type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
>                                  "fstype=ext4"
>                          ];
>                          partition-3 = [
>                                  "size=1G",
>                                  "name=alt",
>                                  "type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
>                                  "fstype=ext4"
>                          ];
>                          partition-4 = [
>                                  "name=srv",
>                                  "type=3B8F8425-20E0-4F3B-907F-1A25A76F98E8",
>                                  "fstype=ext4"
>                          ];
>                  }
>          });
> }

mmmhhh...I tested this, it works with my patch. I get:

[TRACE] : SWUPDATE running :  [diskpart_partition_cmp] : Partition 
differ : srv(57198559) <--> srv(18446744073709551615)
[TRACE] : SWUPDATE running :  [diskpart] : Disk full partitioned, do not 
check size for last part
[TRACE] : SWUPDATE running :  [diskpart] : Same partition table on disk, 
do not touch partition table !

Can you report what is not working ?

And I will review your patch and compare solution.

Best regards,
Stefano

> 
>>
>> Best regards,
>> Stefano Babic
>>
>> On 17.05.21 11:26, James Hilliard wrote:
>>> In order for diskpart_partition_cmp to properly compare the size
>>> of partitions that are autogenerated we need to reload the table
>>> against the context before the comparision.
>>>
>>> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
>>> ---
>>>    handlers/diskpart_handler.c | 37 +++++++++++++++++++++++++++++++------
>>>    1 file changed, 31 insertions(+), 6 deletions(-)
>>>
>>> diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
>>> index b10ffc6..2cb524a 100644
>>> --- a/handlers/diskpart_handler.c
>>> +++ b/handlers/diskpart_handler.c
>>> @@ -161,6 +161,29 @@ static bool diskpart_partition_cmp(const char *lbtype, struct fdisk_partition *f
>>>        return false;
>>>    }
>>>
>>> +static int diskpart_reload_table(struct fdisk_context *cxt, struct fdisk_table *tb)
>>> +{
>>> +     int ret = 0;
>>> +
>>> +     ret = fdisk_delete_all_partitions(cxt);
>>> +     if (ret) {
>>> +             ERROR("Partition table cannot be deleted: %d", ret);
>>> +             return ret;
>>> +     }
>>> +     ret = fdisk_apply_table(cxt, tb);
>>> +     if (ret) {
>>> +             ERROR("Partition table cannot be applied: %d", ret);
>>> +             return ret;
>>> +     }
>>> +     fdisk_reset_table(tb);
>>> +     ret = fdisk_get_partitions(cxt, &tb);
>>> +     if (ret) {
>>> +             ERROR("Error loading applied table %d:", ret);
>>> +             return ret;
>>> +     }
>>> +     return ret;
>>> +}
>>> +
>>>    static int diskpart(struct img_type *img,
>>>        void __attribute__ ((__unused__)) *data)
>>>    {
>>> @@ -368,6 +391,14 @@ static int diskpart(struct img_type *img,
>>>                i++;
>>>        }
>>>
>>> +     /*
>>> +      * Reload new table against the context to populate default values
>>> +      * so that we can compare partitions properly.
>>> +      */
>>> +     ret = diskpart_reload_table(cxt, tb);
>>> +     if (ret)
>>> +             goto handler_exit;
>>> +
>>>        /*
>>>         * A partiton table was found on disk, now compares the two tables
>>>         * to check if they differ.
>>> @@ -407,12 +438,6 @@ static int diskpart(struct img_type *img,
>>>
>>>        if (createtable) {
>>>                TRACE("Partitions on disk differ, write to disk;");
>>> -             fdisk_delete_all_partitions(cxt);
>>> -             ret = fdisk_apply_table(cxt, tb);
>>> -             if (ret) {
>>> -                     ERROR("Partition table cannot be applied !");
>>> -                     goto handler_exit;
>>> -             }
>>>
>>>                /*
>>>                 * Everything done, write into disk
>>>
>>
>>
>> --
>> =====================================================================
>> DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
>> HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
>> Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic@denx.de
>> =====================================================================
>
James Hilliard May 17, 2021, 10:09 a.m. UTC | #4
On Mon, May 17, 2021 at 3:57 AM Stefano Babic <sbabic@denx.de> wrote:
>
> Hi James,
>
> On 17.05.21 11:47, James Hilliard wrote:
> > On Mon, May 17, 2021 at 3:32 AM Stefano Babic <sbabic@denx.de> wrote:
> >>
> >> Hi James,
> >>
> >> I sent yesterday a patch to fix this - have you seen it ?
> > Yes, but it doesn't fix the issue, and I think that approach is
> > inherently unreliable
> > as it doesn't appear to appropriately replicate the fdisk auto generation logic
> > which is rather complex.
> >
> > See this example test case which appears to work with my patch but not yours:
> > software =
> > {
> >          version = "0.1.0";
> >
> >          partitions: (
> >          {
> >                  type = "diskpart";
> >                  device = "/dev/sdb";
> >                  properties: {
> >                          labeltype = "gpt";
> >                          partition-1 = [
> >                                  "size=550M",
> >                                  "start=2048",
> >                                  "name=boot",
> >                                  "type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B",
> >                                  "fstype=vfat"
> >                          ];
> >                          partition-2 = [
> >                                  "size=1G",
> >                                  "name=main",
> >                                  "type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
> >                                  "fstype=ext4"
> >                          ];
> >                          partition-3 = [
> >                                  "size=1G",
> >                                  "name=alt",
> >                                  "type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
> >                                  "fstype=ext4"
> >                          ];
> >                          partition-4 = [
> >                                  "name=srv",
> >                                  "type=3B8F8425-20E0-4F3B-907F-1A25A76F98E8",
> >                                  "fstype=ext4"
> >                          ];
> >                  }
> >          });
> > }
>
> mmmhhh...I tested this, it works with my patch. I get:
>
> [TRACE] : SWUPDATE running :  [diskpart_partition_cmp] : Partition
> differ : srv(57198559) <--> srv(18446744073709551615)
This is the failure I was seeing, I guess I just assumed that meant it was
broken still.
> [TRACE] : SWUPDATE running :  [diskpart] : Disk full partitioned, do not
> check size for last part
> [TRACE] : SWUPDATE running :  [diskpart] : Same partition table on disk,
> do not touch partition table !
>
> Can you report what is not working ?
>
> And I will review your patch and compare solution.
Yeah, I think my approach works better and should work for anything libfdisk
auto generated as it should just use the existing libfdisk logic
essentially as is.
>
> Best regards,
> Stefano
>
> >
> >>
> >> Best regards,
> >> Stefano Babic
> >>
> >> On 17.05.21 11:26, James Hilliard wrote:
> >>> In order for diskpart_partition_cmp to properly compare the size
> >>> of partitions that are autogenerated we need to reload the table
> >>> against the context before the comparision.
> >>>
> >>> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> >>> ---
> >>>    handlers/diskpart_handler.c | 37 +++++++++++++++++++++++++++++++------
> >>>    1 file changed, 31 insertions(+), 6 deletions(-)
> >>>
> >>> diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
> >>> index b10ffc6..2cb524a 100644
> >>> --- a/handlers/diskpart_handler.c
> >>> +++ b/handlers/diskpart_handler.c
> >>> @@ -161,6 +161,29 @@ static bool diskpart_partition_cmp(const char *lbtype, struct fdisk_partition *f
> >>>        return false;
> >>>    }
> >>>
> >>> +static int diskpart_reload_table(struct fdisk_context *cxt, struct fdisk_table *tb)
> >>> +{
> >>> +     int ret = 0;
> >>> +
> >>> +     ret = fdisk_delete_all_partitions(cxt);
> >>> +     if (ret) {
> >>> +             ERROR("Partition table cannot be deleted: %d", ret);
> >>> +             return ret;
> >>> +     }
> >>> +     ret = fdisk_apply_table(cxt, tb);
> >>> +     if (ret) {
> >>> +             ERROR("Partition table cannot be applied: %d", ret);
> >>> +             return ret;
> >>> +     }
> >>> +     fdisk_reset_table(tb);
> >>> +     ret = fdisk_get_partitions(cxt, &tb);
> >>> +     if (ret) {
> >>> +             ERROR("Error loading applied table %d:", ret);
> >>> +             return ret;
> >>> +     }
> >>> +     return ret;
> >>> +}
> >>> +
> >>>    static int diskpart(struct img_type *img,
> >>>        void __attribute__ ((__unused__)) *data)
> >>>    {
> >>> @@ -368,6 +391,14 @@ static int diskpart(struct img_type *img,
> >>>                i++;
> >>>        }
> >>>
> >>> +     /*
> >>> +      * Reload new table against the context to populate default values
> >>> +      * so that we can compare partitions properly.
> >>> +      */
> >>> +     ret = diskpart_reload_table(cxt, tb);
> >>> +     if (ret)
> >>> +             goto handler_exit;
> >>> +
> >>>        /*
> >>>         * A partiton table was found on disk, now compares the two tables
> >>>         * to check if they differ.
> >>> @@ -407,12 +438,6 @@ static int diskpart(struct img_type *img,
> >>>
> >>>        if (createtable) {
> >>>                TRACE("Partitions on disk differ, write to disk;");
> >>> -             fdisk_delete_all_partitions(cxt);
> >>> -             ret = fdisk_apply_table(cxt, tb);
> >>> -             if (ret) {
> >>> -                     ERROR("Partition table cannot be applied !");
> >>> -                     goto handler_exit;
> >>> -             }
> >>>
> >>>                /*
> >>>                 * Everything done, write into disk
> >>>
> >>
> >>
> >> --
> >> =====================================================================
> >> DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
> >> HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
> >> Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic@denx.de
> >> =====================================================================
> >
>
>
> --
> =====================================================================
> DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
> HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
> Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic@denx.de
> =====================================================================
Stefano Babic May 17, 2021, 10:30 a.m. UTC | #5
On 17.05.21 12:09, James Hilliard wrote:
> On Mon, May 17, 2021 at 3:57 AM Stefano Babic <sbabic@denx.de> wrote:
>>
>> Hi James,
>>
>> On 17.05.21 11:47, James Hilliard wrote:
>>> On Mon, May 17, 2021 at 3:32 AM Stefano Babic <sbabic@denx.de> wrote:
>>>>
>>>> Hi James,
>>>>
>>>> I sent yesterday a patch to fix this - have you seen it ?
>>> Yes, but it doesn't fix the issue, and I think that approach is
>>> inherently unreliable
>>> as it doesn't appear to appropriately replicate the fdisk auto generation logic
>>> which is rather complex.
>>>
>>> See this example test case which appears to work with my patch but not yours:
>>> software =
>>> {
>>>           version = "0.1.0";
>>>
>>>           partitions: (
>>>           {
>>>                   type = "diskpart";
>>>                   device = "/dev/sdb";
>>>                   properties: {
>>>                           labeltype = "gpt";
>>>                           partition-1 = [
>>>                                   "size=550M",
>>>                                   "start=2048",
>>>                                   "name=boot",
>>>                                   "type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B",
>>>                                   "fstype=vfat"
>>>                           ];
>>>                           partition-2 = [
>>>                                   "size=1G",
>>>                                   "name=main",
>>>                                   "type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
>>>                                   "fstype=ext4"
>>>                           ];
>>>                           partition-3 = [
>>>                                   "size=1G",
>>>                                   "name=alt",
>>>                                   "type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709",
>>>                                   "fstype=ext4"
>>>                           ];
>>>                           partition-4 = [
>>>                                   "name=srv",
>>>                                   "type=3B8F8425-20E0-4F3B-907F-1A25A76F98E8",
>>>                                   "fstype=ext4"
>>>                           ];
>>>                   }
>>>           });
>>> }
>>
>> mmmhhh...I tested this, it works with my patch. I get:
>>
>> [TRACE] : SWUPDATE running :  [diskpart_partition_cmp] : Partition
>> differ : srv(57198559) <--> srv(18446744073709551615)
> This is the failure I was seeing, I guess I just assumed that meant it was
> broken still.
>> [TRACE] : SWUPDATE running :  [diskpart] : Disk full partitioned, do not
>> check size for last part
>> [TRACE] : SWUPDATE running :  [diskpart] : Same partition table on disk,
>> do not touch partition table !
>>
>> Can you report what is not working ?
>>
>> And I will review your patch and compare solution.
> Yeah, I think my approach works better and should work for anything libfdisk
> auto generated as it should just use the existing libfdisk logic
> essentially as is.

I see, it is a different approach. I am fine with it, I just test myself 
and add my tested-by.

Best regards,
Stefano Babic

>>
>> Best regards,
>> Stefano
>>
>>>
>>>>
>>>> Best regards,
>>>> Stefano Babic
>>>>
>>>> On 17.05.21 11:26, James Hilliard wrote:
>>>>> In order for diskpart_partition_cmp to properly compare the size
>>>>> of partitions that are autogenerated we need to reload the table
>>>>> against the context before the comparision.
>>>>>
>>>>> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
>>>>> ---
>>>>>     handlers/diskpart_handler.c | 37 +++++++++++++++++++++++++++++++------
>>>>>     1 file changed, 31 insertions(+), 6 deletions(-)
>>>>>
>>>>> diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
>>>>> index b10ffc6..2cb524a 100644
>>>>> --- a/handlers/diskpart_handler.c
>>>>> +++ b/handlers/diskpart_handler.c
>>>>> @@ -161,6 +161,29 @@ static bool diskpart_partition_cmp(const char *lbtype, struct fdisk_partition *f
>>>>>         return false;
>>>>>     }
>>>>>
>>>>> +static int diskpart_reload_table(struct fdisk_context *cxt, struct fdisk_table *tb)
>>>>> +{
>>>>> +     int ret = 0;
>>>>> +
>>>>> +     ret = fdisk_delete_all_partitions(cxt);
>>>>> +     if (ret) {
>>>>> +             ERROR("Partition table cannot be deleted: %d", ret);
>>>>> +             return ret;
>>>>> +     }
>>>>> +     ret = fdisk_apply_table(cxt, tb);
>>>>> +     if (ret) {
>>>>> +             ERROR("Partition table cannot be applied: %d", ret);
>>>>> +             return ret;
>>>>> +     }
>>>>> +     fdisk_reset_table(tb);
>>>>> +     ret = fdisk_get_partitions(cxt, &tb);
>>>>> +     if (ret) {
>>>>> +             ERROR("Error loading applied table %d:", ret);
>>>>> +             return ret;
>>>>> +     }
>>>>> +     return ret;
>>>>> +}
>>>>> +
>>>>>     static int diskpart(struct img_type *img,
>>>>>         void __attribute__ ((__unused__)) *data)
>>>>>     {
>>>>> @@ -368,6 +391,14 @@ static int diskpart(struct img_type *img,
>>>>>                 i++;
>>>>>         }
>>>>>
>>>>> +     /*
>>>>> +      * Reload new table against the context to populate default values
>>>>> +      * so that we can compare partitions properly.
>>>>> +      */
>>>>> +     ret = diskpart_reload_table(cxt, tb);
>>>>> +     if (ret)
>>>>> +             goto handler_exit;
>>>>> +
>>>>>         /*
>>>>>          * A partiton table was found on disk, now compares the two tables
>>>>>          * to check if they differ.
>>>>> @@ -407,12 +438,6 @@ static int diskpart(struct img_type *img,
>>>>>
>>>>>         if (createtable) {
>>>>>                 TRACE("Partitions on disk differ, write to disk;");
>>>>> -             fdisk_delete_all_partitions(cxt);
>>>>> -             ret = fdisk_apply_table(cxt, tb);
>>>>> -             if (ret) {
>>>>> -                     ERROR("Partition table cannot be applied !");
>>>>> -                     goto handler_exit;
>>>>> -             }
>>>>>
>>>>>                 /*
>>>>>                  * Everything done, write into disk
>>>>>
>>>>
>>>>
>>>> --
>>>> =====================================================================
>>>> DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
>>>> HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
>>>> Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic@denx.de
>>>> =====================================================================
>>>
>>
>>
>> --
>> =====================================================================
>> DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
>> HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
>> Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic@denx.de
>> =====================================================================
Stefano Babic May 17, 2021, 10:32 a.m. UTC | #6
On 17.05.21 11:26, James Hilliard wrote:
> In order for diskpart_partition_cmp to properly compare the size
> of partitions that are autogenerated we need to reload the table
> against the context before the comparision.
> 
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
> ---
>   handlers/diskpart_handler.c | 37 +++++++++++++++++++++++++++++++------
>   1 file changed, 31 insertions(+), 6 deletions(-)
> 
> diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
> index b10ffc6..2cb524a 100644
> --- a/handlers/diskpart_handler.c
> +++ b/handlers/diskpart_handler.c
> @@ -161,6 +161,29 @@ static bool diskpart_partition_cmp(const char *lbtype, struct fdisk_partition *f
>   	return false;
>   }
>   
> +static int diskpart_reload_table(struct fdisk_context *cxt, struct fdisk_table *tb)
> +{
> +	int ret = 0;
> +
> +	ret = fdisk_delete_all_partitions(cxt);
> +	if (ret) {
> +		ERROR("Partition table cannot be deleted: %d", ret);
> +		return ret;
> +	}
> +	ret = fdisk_apply_table(cxt, tb);
> +	if (ret) {
> +		ERROR("Partition table cannot be applied: %d", ret);
> +		return ret;
> +	}
> +	fdisk_reset_table(tb);
> +	ret = fdisk_get_partitions(cxt, &tb);
> +	if (ret) {
> +		ERROR("Error loading applied table %d:", ret);
> +		return ret;
> +	}
> +	return ret;
> +}
> +
>   static int diskpart(struct img_type *img,
>   	void __attribute__ ((__unused__)) *data)
>   {
> @@ -368,6 +391,14 @@ static int diskpart(struct img_type *img,
>   		i++;
>   	}
>   
> +	/*
> +	 * Reload new table against the context to populate default values
> +	 * so that we can compare partitions properly.
> +	 */
> +	ret = diskpart_reload_table(cxt, tb);
> +	if (ret)
> +		goto handler_exit;
> +
>   	/*
>   	 * A partiton table was found on disk, now compares the two tables
>   	 * to check if they differ.
> @@ -407,12 +438,6 @@ static int diskpart(struct img_type *img,
>   
>   	if (createtable) {
>   		TRACE("Partitions on disk differ, write to disk;");
> -		fdisk_delete_all_partitions(cxt);
> -		ret = fdisk_apply_table(cxt, tb);
> -		if (ret) {
> -			ERROR("Partition table cannot be applied !");
> -			goto handler_exit;
> -		}
>   
>   		/*
>   		 * Everything done, write into disk
> 

Tested-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
index b10ffc6..2cb524a 100644
--- a/handlers/diskpart_handler.c
+++ b/handlers/diskpart_handler.c
@@ -161,6 +161,29 @@  static bool diskpart_partition_cmp(const char *lbtype, struct fdisk_partition *f
 	return false;
 }
 
+static int diskpart_reload_table(struct fdisk_context *cxt, struct fdisk_table *tb)
+{
+	int ret = 0;
+
+	ret = fdisk_delete_all_partitions(cxt);
+	if (ret) {
+		ERROR("Partition table cannot be deleted: %d", ret);
+		return ret;
+	}
+	ret = fdisk_apply_table(cxt, tb);
+	if (ret) {
+		ERROR("Partition table cannot be applied: %d", ret);
+		return ret;
+	}
+	fdisk_reset_table(tb);
+	ret = fdisk_get_partitions(cxt, &tb);
+	if (ret) {
+		ERROR("Error loading applied table %d:", ret);
+		return ret;
+	}
+	return ret;
+}
+
 static int diskpart(struct img_type *img,
 	void __attribute__ ((__unused__)) *data)
 {
@@ -368,6 +391,14 @@  static int diskpart(struct img_type *img,
 		i++;
 	}
 
+	/*
+	 * Reload new table against the context to populate default values
+	 * so that we can compare partitions properly.
+	 */
+	ret = diskpart_reload_table(cxt, tb);
+	if (ret)
+		goto handler_exit;
+
 	/*
 	 * A partiton table was found on disk, now compares the two tables
 	 * to check if they differ.
@@ -407,12 +438,6 @@  static int diskpart(struct img_type *img,
 
 	if (createtable) {
 		TRACE("Partitions on disk differ, write to disk;");
-		fdisk_delete_all_partitions(cxt);
-		ret = fdisk_apply_table(cxt, tb);
-		if (ret) {
-			ERROR("Partition table cannot be applied !");
-			goto handler_exit;
-		}
 
 		/*
 		 * Everything done, write into disk