[PATCH] lib: sparse: Make CHUNK_TYPE_RAW buffer aligned

Sean Anderson sean.anderson at seco.com
Mon Nov 15 16:07:46 CET 2021


(This should be [PATCH v3] for those following along at home)

On 11/15/21 12:43 AM, qianfanguijin at qq.com wrote:
> From: qianfan Zhao <qianfanguijin at 163.com>
> 
> CHUNK_TYPE_RAW buffer is not aligned, and flash sparse images by
> fastboot will report "Misaligned operation" if DCACHE is enabled.
> 
> Flashing Sparse Image
> CACHE: Misaligned operation at range [84000028, 84001028]
> CACHE: Misaligned operation at range [84001034, 84002034]
> CACHE: Misaligned operation at range [8401104c, 8401304c]
> 
> Fix it
> 
> Signed-off-by: qianfan Zhao <qianfanguijin at 163.com>
> ---
>   lib/image-sparse.c | 54 +++++++++++++++++++++++++++++++++++++++-------
>   1 file changed, 46 insertions(+), 8 deletions(-)
> 
> diff --git a/lib/image-sparse.c b/lib/image-sparse.c
> index d80fdbbf58..1143c88a79 100644
> --- a/lib/image-sparse.c
> +++ b/lib/image-sparse.c
> @@ -49,6 +49,48 @@
>   
>   static void default_log(const char *ignored, char *response) {}
>   
> +static lbaint_t write_sparse_chunk_raw(struct sparse_storage *info,
> +				       lbaint_t blk, lbaint_t blkcnt,
> +				       void *data,
> +				       char *response)
> +{
> +	lbaint_t n, write_blks, blks = 0, aligned_buf_blks = 100;
> +	uint32_t *aligned_buf = NULL;
> +
> +	if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
> +		return info->write(info, blk, blkcnt, data);

No error message for regular write?

> +
> +	aligned_buf = memalign(ARCH_DMA_MINALIGN, info->blksz * aligned_buf_blks);
> +	if (!aligned_buf) {
> +		info->mssg("Malloc failed for: CHUNK_TYPE_RAW", response);
> +		return -ENOMEM;
> +	}
> +
> +	while (blkcnt > 0) {
> +		n = min(aligned_buf_blks, blkcnt);
> +		memcpy(aligned_buf, data, n * info->blksz);
> +
> +		/* write_blks might be > n due to NAND bad-blocks */
> +		write_blks = info->write(info, blk + blks, n, aligned_buf);
> +		if (write_blks < 0 || write_blks < n) {

n will always be >= 0, so the first condition here does nothing

> +			printf("%s: %s" LBAFU " [" LBAFU "]\n",
> +				__func__, "Write failed, block #",
> +				blk + blks, n);
> +			info->mssg("flash write failure", response);
> +			free(aligned_buf);
> +
> +			return min(write_blks, (lbaint_t)-1);
> +		}
> +
> +		blks += write_blks;
> +		data += n * info->blksz;
> +		blkcnt -= n;
> +	}
> +
> +	free(aligned_buf);
> +	return blks;
> +}
> +
>   int write_sparse_image(struct sparse_storage *info,
>   		       const char *part_name, void *data, char *response)
>   {
> @@ -152,15 +194,11 @@ int write_sparse_image(struct sparse_storage *info,
>   				return -1;
>   			}
>   
> -			blks = info->write(info, blk, blkcnt, data);
> -			/* blks might be > blkcnt (eg. NAND bad-blocks) */
> -			if (blks < blkcnt) {
> -				printf("%s: %s" LBAFU " [" LBAFU "]\n",
> -				       __func__, "Write failed, block #",
> -				       blk, blks);
> -				info->mssg("flash write failure", response);
> +			blks = write_sparse_chunk_raw(info, blk, blkcnt,
> +						      data, response);
> +			if (blks < 0)

This will not catch errors for a short write not using the wrapper above.

--Sean


More information about the U-Boot mailing list