[PATCH 1/3] common: bbu: refactor flash operations into separate function

Ahmad Fatoum a.fatoum at pengutronix.de
Fri Nov 28 01:35:44 PST 2025


On 11/28/25 1:51 AM, Sohaib Mohamed wrote:
> Extract flash logic (protect, erase, write) from bbu_std_file_handler()
> into a new flash() function that accepts an offset parameter, so it can
> be used from further barebox update handlers as well.
> 
> Signed-off-by: Sohaib Mohamed <sohaib.amhmd at gmail.com>

Reviewed-by: Ahmad Fatoum <a.fatoum at pengutronix.de>

> ---
>  common/bbu.c  | 17 +++++++++++------
>  include/bbu.h |  2 ++
>  2 files changed, 13 insertions(+), 6 deletions(-)
> 
> diff --git a/common/bbu.c b/common/bbu.c
> index 03261583fe..39db87e823 100644
> --- a/common/bbu.c
> +++ b/common/bbu.c
> @@ -431,8 +431,7 @@ int bbu_mmcboot_register_handler(const char *name,
>  	return ret;
>  }
>  
> -int bbu_std_file_handler(struct bbu_handler *handler,
> -			 struct bbu_data *data)
> +int bbu_flash(struct bbu_data *data, loff_t offset)
>  {
>  	int fd, ret;
>  	struct stat s;
> @@ -458,25 +457,25 @@ int bbu_std_file_handler(struct bbu_handler *handler,
>  	if (fd < 0)
>  		return fd;
>  
> -	ret = protect(fd, data->len, 0, 0);
> +	ret = protect(fd, data->len, offset, 0);
>  	if (ret && (ret != -ENOSYS) && (ret != -ENOTSUPP)) {
>  		printf("unprotecting %s failed with %pe\n", data->devicefile,
>  				ERR_PTR(ret));
>  		goto err_close;
>  	}
>  
> -	ret = erase(fd, data->len, 0, ERASE_TO_WRITE);
> +	ret = erase(fd, data->len, offset, ERASE_TO_WRITE);
>  	if (ret && ret != -ENOSYS) {
>  		printf("erasing %s failed with %pe\n", data->devicefile,
>  				ERR_PTR(ret));
>  		goto err_close;
>  	}
>  
> -	ret = write_full(fd, data->image, data->len);
> +	ret = pwrite_full(fd, data->image, data->len, offset);
>  	if (ret < 0)
>  		goto err_close;
>  
> -	protect(fd, data->len, 0, 1);
> +	protect(fd, data->len, offset, 1);
>  
>  	ret = 0;
>  
> @@ -486,6 +485,12 @@ int bbu_std_file_handler(struct bbu_handler *handler,
>  	return ret;
>  }
>  
> +int bbu_std_file_handler(struct bbu_handler *handler,
> +			 struct bbu_data *data)
> +{
> +	return bbu_flash(data, 0);
> +}
> +
>  static int bbu_std_file_handler_checked(struct bbu_handler *handler,
>  					struct bbu_data *data)
>  {
> diff --git a/include/bbu.h b/include/bbu.h
> index 94006563c9..f5b74b32ac 100644
> --- a/include/bbu.h
> +++ b/include/bbu.h
> @@ -56,6 +56,8 @@ int bbu_mmcboot_handler(struct bbu_handler *, struct bbu_data *,
>  int bbu_std_file_handler(struct bbu_handler *handler,
>  			 struct bbu_data *data);
>  
> +int bbu_flash(struct bbu_data *data, loff_t offset);
> +
>  #ifdef CONFIG_BAREBOX_UPDATE
>  
>  bool bbu_handlers_available(void);
> 

-- 
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