[PATCH 7/8] cdev: Add function to get unallocated start of device

Ahmad Fatoum a.fatoum at pengutronix.de
Tue Oct 12 00:52:51 PDT 2021


On 12.10.21 09:33, Sascha Hauer wrote:
> On several SoCs barebox is written to the raw device in front of the
> first partition. So far we blindly trust that there is enough space
> available for the barebox image. Start changing this by adding a
> function that retrieves the available space.
> 
> Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
> ---
>  common/partitions.c | 28 ++++++++++++++++++++++++++++
>  include/driver.h    |  1 +
>  2 files changed, 29 insertions(+)
> 
> diff --git a/common/partitions.c b/common/partitions.c
> index d80878e065..e36341fc1e 100644
> --- a/common/partitions.c
> +++ b/common/partitions.c
> @@ -156,3 +156,31 @@ int partition_parser_register(struct partition_parser *p)
>  
>  	return 0;
>  }
> +
> +/**
> + * cdev_unallocated_start - return unallocated space
> + * @name: The cdev name
> + *
> + * This function returns the space that is not allocated by any partition
> + * at the start of a device.
> + *
> + * Return: The unallocated space at the start of the device in bytes
> + */
> +loff_t cdev_unallocated_start(const char *name)

That name is a bit misleading. It's either cdev_allocated_start
or cdev_unallocated_space. Also cdev_ for a function not taking a cdev
is unexpected. Perhaps define devpath_to_cdev(s) as
cdev_by_name(devpath_to_name(s)) and have the caller pass in the cdev?

> +{
> +	struct cdev *cdev, *partcdev;
> +	loff_t start;
> +
> +	cdev = cdev_by_name(name);
> +	if (!cdev)
> +		return 0;
> +
> +	start = cdev->size;
> +
> +	list_for_each_entry(partcdev, &cdev->partitions, partition_entry) {
> +		if (partcdev->offset < start)
> +			start = partcdev->offset;
> +	}
> +
> +	return start;
> +}
> diff --git a/include/driver.h b/include/driver.h
> index c7f5903fce..38aa3cf8f5 100644
> --- a/include/driver.h
> +++ b/include/driver.h
> @@ -494,6 +494,7 @@ ssize_t cdev_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulo
>  ssize_t cdev_write(struct cdev *cdev, const void *buf, size_t count, loff_t offset, ulong flags);
>  int cdev_ioctl(struct cdev *cdev, int cmd, void *buf);
>  int cdev_erase(struct cdev *cdev, loff_t count, loff_t offset);
> +loff_t cdev_unallocated_start(const char *name);
>  
>  #define DEVFS_PARTITION_FIXED		(1U << 0)
>  #define DEVFS_PARTITION_READONLY	(1U << 1)
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list