public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Anshul Dalal <anshuld@ti.com>
To: Anurag Dutta <a-dutta@ti.com>, <jagan@amarulasolutions.com>,
	<trini@konsulko.com>
Cc: <michal.simek@amd.com>, <venkatesh.abbarapu@amd.com>,
	<boon.khai.ng@altera.com>, <s-k6@ti.com>, <gehariprasath@ti.com>,
	<vigneshr@ti.com>, <u-kumar1@ti.com>, <u-boot@lists.denx.de>
Subject: Re: [PATCH 3/7] spl: mtd: Add bad block handling for SPL image loading
Date: Thu, 19 Feb 2026 14:08:06 +0530	[thread overview]
Message-ID: <DGISWASK42DF.1HYLBZFMHMJQF@ti.com> (raw)
In-Reply-To: <20260217112156.272154-4-a-dutta@ti.com>

On Tue Feb 17, 2026 at 4:51 PM IST, Anurag Dutta wrote:
> From: Santhosh Kumar K <s-k6@ti.com>
>
> Implement bad block skipping functionality in SPL MTD loader to ensure
> reliable boot from flash devices with bad blocks. Without this, the
> SPL would fail to load images if bad blocks were encountered.
>
> Signed-off-by: Santhosh Kumar K <s-k6@ti.com>
> Signed-off-by: Anurag Dutta <a-dutta@ti.com>
> ---
>  common/spl/spl_mtd.c | 78 ++++++++++++++++++++++++++++++++++++++------
>  1 file changed, 68 insertions(+), 10 deletions(-)
>
> diff --git a/common/spl/spl_mtd.c b/common/spl/spl_mtd.c
> index 95c0c8ce8cd..d635aa2b476 100644
> --- a/common/spl/spl_mtd.c
> +++ b/common/spl/spl_mtd.c
> @@ -17,15 +17,64 @@ uint32_t __weak spl_mtd_get_uboot_offs(void)
>  	return CONFIG_SYS_MTD_U_BOOT_OFFS;
>  }
>  
> +static ulong spl_mtd_read_skip_bad(struct mtd_info *mtd, loff_t offs,
> +				   size_t size, void *dst)
> +{
> +	size_t remaining = size;
> +	size_t ret_len;
> +	loff_t current_offs = offs;

NIT: Do we really need to have a current_offs here when we aren't gonna
use offs later anyway? Would be more clear to use offs directly imo.

> +	loff_t block_aligned_offs, next_block;
> +	u_char *buf = (u_char *)dst;
> +	int err;
> +
> +	while (remaining > 0) {
> +		size_t read_size;
> +		size_t block_remaining;
> +
> +		block_aligned_offs = current_offs & ~(mtd->erasesize - 1);

We could make use of ALIGN macro here.

> +
> +		if (mtd_block_isbad(mtd, block_aligned_offs)) {
> +			debug("SPL: Skipping bad block at 0x%llx, jumping to 0x%llx\n",
> +			      block_aligned_offs, next_block);

On the first bad bock, next_block would be uninitialized data and
printing it here doesn't make sense.

> +
> +			next_block = block_aligned_offs + mtd->erasesize;
> +			current_offs = next_block;
> +			continue;
> +		}
> +
> +		block_remaining = mtd->erasesize -
> +				  (current_offs & (mtd->erasesize - 1));
> +		read_size = remaining < block_remaining ? remaining : block_remaining;

We could instead do 'read_size = min(remaining, block_remaining).

> +
> +		err = mtd_read(mtd, current_offs, read_size, &ret_len, buf);
> +		if (err) {
> +			printf("SPL: Read error at offset 0x%llx: %d\n",
> +			       current_offs, err);
> +			return size - remaining;
> +		}
> +
> +		buf += ret_len;
> +		current_offs += ret_len;
> +		remaining -= ret_len;
> +
> +		if (current_offs >= mtd->size) {
> +			printf("SPL: Reached end of device, read %zu/%zu bytes\n",
> +			       size - remaining, size);
> +			break;
> +		}
> +	}
> +
> +	return size - remaining;
> +}
> +
>  static ulong spl_mtd_fit_read(struct spl_load_info *load, ulong offs,
>  			      ulong size, void *dst)
>  {
>  	struct mtd_info *mtd = load->priv;
> -	int err;
>  	size_t ret_len;
>  
> -	err = mtd_read(mtd, offs, size, &ret_len, dst);
> -	if (!err)
> +	ret_len = spl_mtd_read_skip_bad(mtd, offs, size, dst);
> +	if (ret_len > 0)
>  		return ret_len;
>  
>  	return 0;
> @@ -52,17 +101,19 @@ struct mtd_info *spl_prepare_mtd(uint boot_device)
>  int spl_mtd_load(struct spl_image_info *spl_image,
>  		 struct mtd_info *mtd, struct spl_boot_device *bootdev)
>  {
> -	int err;
> +	int err = 0;
>  	struct legacy_img_hdr *header;
>  	__maybe_unused struct spl_load_info load;
>  	size_t ret_len;
>  
>  	header = spl_get_load_buffer(0, sizeof(*header));
> -
> -	err = mtd_read(mtd, spl_mtd_get_uboot_offs(), sizeof(*header),
> -		       &ret_len, (void *)header);
> -	if (err)
> +	ret_len = spl_mtd_read_skip_bad(mtd, spl_mtd_get_uboot_offs(),
> +					sizeof(*header), (void *)header);
> +	if (ret_len != sizeof(*header)) {
> +		printf("SPL: Failed to read image header\n");
> +		err = -EIO;
>  		goto out_err;
> +	}
>  
>  	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
>  	    image_get_magic(header) == FDT_MAGIC) {
> @@ -82,8 +133,15 @@ int spl_mtd_load(struct spl_image_info *spl_image,
>  		err = spl_parse_image_header(spl_image, bootdev, header);
>  		if (err)
>  			goto out_err;
> -		err = mtd_read(mtd, spl_mtd_get_uboot_offs(), spl_image->size,
> -			       &ret_len, (void *)(ulong)spl_image->load_addr);
> +
> +		ret_len = spl_mtd_read_skip_bad(mtd, spl_mtd_get_uboot_offs(),
> +						spl_image->size,
> +						(void *)(ulong)spl_image->load_addr);
> +		if (ret_len != spl_image->size) {
> +			printf("SPL: Failed to read full image: %zu/%u bytes\n",
> +			       ret_len, spl_image->size);
> +			err = -EIO;
> +		}
>  	}
>  
>  out_err:


  reply	other threads:[~2026-02-19  8:38 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-17 11:21 [PATCH 0/7] OSPI NAND MTD load and boot support Anurag Dutta
2026-02-17 11:21 ` [PATCH 1/7] common: spl: mtd: Add support for loading images from MTD Anurag Dutta
2026-02-17 14:54   ` Sean Anderson
2026-02-17 15:14     ` Sean Anderson
2026-02-18  8:42   ` Anshul Dalal
2026-02-17 11:21 ` [PATCH 2/7] spl: mtd: Remove MTD device after loading images Anurag Dutta
2026-02-17 11:21 ` [PATCH 3/7] spl: mtd: Add bad block handling for SPL image loading Anurag Dutta
2026-02-19  8:38   ` Anshul Dalal [this message]
2026-02-17 11:21 ` [PATCH 4/7] spl: Add MTD loading support configuration Anurag Dutta
2026-02-17 11:21 ` [PATCH 5/7] mtd: nand: spi: Enable spinand build Kconfig option for spl Anurag Dutta
2026-02-17 11:21 ` [PATCH 6/7] arm: spl: Enumerate SPINAND as a boot device Anurag Dutta
2026-02-18  8:34   ` Anshul Dalal
2026-02-17 11:21 ` [PATCH 7/7] include: environment: ti: Add ospi_nand environment variables Anurag Dutta
2026-02-17 13:31 ` [PATCH 0/7] OSPI NAND MTD load and boot support Santhosh Kumar K
2026-02-17 13:37   ` Santhosh Kumar K

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DGISWASK42DF.1HYLBZFMHMJQF@ti.com \
    --to=anshuld@ti.com \
    --cc=a-dutta@ti.com \
    --cc=boon.khai.ng@altera.com \
    --cc=gehariprasath@ti.com \
    --cc=jagan@amarulasolutions.com \
    --cc=michal.simek@amd.com \
    --cc=s-k6@ti.com \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=u-kumar1@ti.com \
    --cc=venkatesh.abbarapu@amd.com \
    --cc=vigneshr@ti.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox