All of lore.kernel.org
 help / color / mirror / Atom feed
* [v2, PATCH] lib: sparse: Make CHUNK_TYPE_RAW buffer aligned
@ 2021-11-08  2:29 qianfanguijin
  2021-11-11 15:28 ` Sean Anderson
  0 siblings, 1 reply; 2+ messages in thread
From: qianfanguijin @ 2021-11-08  2:29 UTC (permalink / raw)
  To: u-boot; +Cc: sean.anderson, hs, qianfan Zhao

From: qianfan Zhao <qianfanguijin@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@163.com>
---
 lib/image-sparse.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/lib/image-sparse.c b/lib/image-sparse.c
index d80fdbbf58..07aa3dd6ba 100644
--- a/lib/image-sparse.c
+++ b/lib/image-sparse.c
@@ -49,6 +49,41 @@
 
 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);
+
+	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);
+		blks += write_blks;
+		if (write_blks < n)
+			break;
+
+		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,7 +187,9 @@ int write_sparse_image(struct sparse_storage *info,
 				return -1;
 			}
 
-			blks = info->write(info, blk, blkcnt, data);
+			blks = write_sparse_chunk_raw(info, blk, blkcnt,
+						      data, response);
+
 			/* blks might be > blkcnt (eg. NAND bad-blocks) */
 			if (blks < blkcnt) {
 				printf("%s: %s" LBAFU " [" LBAFU "]\n",
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [v2, PATCH] lib: sparse: Make CHUNK_TYPE_RAW buffer aligned
  2021-11-08  2:29 [v2, PATCH] lib: sparse: Make CHUNK_TYPE_RAW buffer aligned qianfanguijin
@ 2021-11-11 15:28 ` Sean Anderson
  0 siblings, 0 replies; 2+ messages in thread
From: Sean Anderson @ 2021-11-11 15:28 UTC (permalink / raw)
  To: qianfanguijin, u-boot; +Cc: hs, qianfan Zhao



On 11/7/21 9:29 PM, qianfanguijin@qq.com wrote:
> From: qianfan Zhao <qianfanguijin@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@163.com>
> ---
>   lib/image-sparse.c | 39 ++++++++++++++++++++++++++++++++++++++-
>   1 file changed, 38 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/image-sparse.c b/lib/image-sparse.c
> index d80fdbbf58..07aa3dd6ba 100644
> --- a/lib/image-sparse.c
> +++ b/lib/image-sparse.c
> @@ -49,6 +49,41 @@
>   
>   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);
> +
> +	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);
> +		blks += write_blks;
> +		if (write_blks < n)
> +			break;
> +
> +		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,7 +187,9 @@ int write_sparse_image(struct sparse_storage *info,
>   				return -1;
>   			}
>   
> -			blks = info->write(info, blk, blkcnt, data);
> +			blks = write_sparse_chunk_raw(info, blk, blkcnt,
> +						      data, response);
> +
>   			/* blks might be > blkcnt (eg. NAND bad-blocks) */
>   			if (blks < blkcnt) {
>   				printf("%s: %s" LBAFU " [" LBAFU "]\n",

The rest of the patch looks good, but the error check here still need to be updated. It should be something like

			if (IS_ERR_VALUE(blks)) {
				printf("%s: Write failed (error %lld)", __func__, blks);
				info->mssg(...);
				return blks;
    			} else if (blks < blkcnt) {
    				printf("%s: Write failed, block #" LBAFU " [" LBAFU "]\n",
				....
			}

--Sean

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-11-11 15:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-08  2:29 [v2, PATCH] lib: sparse: Make CHUNK_TYPE_RAW buffer aligned qianfanguijin
2021-11-11 15:28 ` Sean Anderson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.