[PATCH v2 2/4] parted: add support for gpt-location

Marco Felsch m.felsch at pengutronix.de
Thu Apr 24 13:34:29 PDT 2025


On 25-04-24, Sascha Hauer wrote:
> On Wed, Apr 23, 2025 at 04:09:11PM +0200, Marco Felsch wrote:
> > Add support to place the GPT partition entries to random places instead
> > of LBA2.
> > 
> > Signed-off-by: Marco Felsch <m.felsch at pengutronix.de>
> > ---
> > v2:
> > - new
> > 
> >  commands/parted.c | 29 +++++++++++++++++++++++++++--
> >  1 file changed, 27 insertions(+), 2 deletions(-)
> > 
> > diff --git a/commands/parted.c b/commands/parted.c
> > index 90cee1ede10e..895c06524d4c 100644
> > --- a/commands/parted.c
> > +++ b/commands/parted.c
> > @@ -224,6 +224,30 @@ static int do_mklabel(struct block_device *blk, int argc, char *argv[])
> >  		return -EINVAL;
> >  	}
> >  
> > +	if (argc == 3) {
> > +		uint64_t mult;
> > +		int ret;
> > +
> > +		if (!strcmp(argv[1], "msdos")) {
> > +			printf("gpt-location not supported for msdos partitions\n");
> > +			return -EINVAL;
> > +		}
> > +
> > +		ret = parted_strtoull(argv[2], &gpt_location, &mult);
> > +		if (ret)
> > +			return ret;
> > +
> > +		if (!mult)
> > +			mult = gunit;
> > +
> > +		gpt_location *= mult;
> > +
> > +		/* Ensure alignment */
> > +		gpt_location = ALIGN(gpt_location, SECTOR_SIZE);
> > +		/* convert to LBA */
> > +		gpt_location >>= SECTOR_SHIFT;
> > +	}
> > +
> >  	pdesc = partition_table_new(blk, argv[1], gpt_location);
> >  	if (IS_ERR(pdesc)) {
> >  		printf("Error: Cannot create partition table: %pe\n", pdesc);
> > @@ -236,7 +260,7 @@ static int do_mklabel(struct block_device *blk, int argc, char *argv[])
> >  		partition_table_free(gpdesc);
> >  	gpdesc = pdesc;
> >  
> > -	return 2;
> > +	return argc > 2 ? 3 : 2;
> 
> Optional arguments are not supported by parted. You can pass multiple
> commands to parted, e.g.
> 
> parted /dev/mmc0 mklabel gpt mkpart foo ext4 4MiB 1024MiB
> 
> argc will contain the remaining arguments, when another command follows
> it won't be exactly three and you can't tell if the argument belongs to
> this command or is the start of the next one.

Argh.. damn, I wasn't aware of such use-cases. Thanks for the hint :)

> You could implement it as an option.

Sure.

Regards,
  Marco



More information about the barebox mailing list