public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Miquel Raynal <miquel.raynal@bootlin.com>
To: Peter Suti <peter.suti@streamunlimited.com>
Cc: u-boot@lists.denx.de,
	"Radek Dostál" <radek.dostal@streamunlimited.com>,
	"Tom Rini" <trini@konsulko.com>,
	"Michael Trimarchi" <michael@amarulasolutions.com>,
	"Mikhail Kshevetskiy" <mikhail.kshevetskiy@iopsys.eu>,
	"Heinrich Schuchardt" <xypron.glpk@gmx.de>,
	"Andrew Goodbody" <andrew.goodbody@linaro.org>
Subject: Re: [PATCH 1/2] cmd: mtd: fix read failure on default full partition read with bad blocks
Date: Thu, 08 Jan 2026 18:17:46 +0100	[thread overview]
Message-ID: <87zf6oxbut.fsf@bootlin.com> (raw)
In-Reply-To: <722abb2d211a68f8a6867b2e03186245a443fbb9.1765892031.git.peter.suti@streamunlimited.com> (Peter Suti's message of "Tue, 16 Dec 2025 14:40:41 +0100")

Hi,

On 16/12/2025 at 14:40:41 +01, Peter Suti <peter.suti@streamunlimited.com> wrote:

> When performing an 'mtd read' on a NAND partition, the presence of bad
> blocks can cause the command to fail with error -22 (EINVAL) if the
> requested size is equal to (or close to) the partition size.
> When size is not provided, the whole partition is read by default.
>
> This issue occurs because the bad block skipping logic increments the
> physical read offset ('off') without decrementing the 'remaining' byte
> count. If enough bad blocks are skipped, the 'off' pointer eventually
> slides past the end of the partition boundary ('mtd->size') while
> 'remaining' is still non-zero. The subsequent call to 'mtd_read_oob'
> then fails its internal boundary checks.
>
> The Scenario:
>   Partition Size: 0x1200000
>   Bad Blocks:     1
>   Command:        mtd read swufit ${loadaddr}
>                   (Defaults to reading full 0x1200000 bytes)
>
>   1. U-Boot attempts to read 0x1200000 valid bytes.
>   2. It encounters the bad block and skips it (off += mtd->erasesize).
>   3. To satisfy the full request, it must read
>      (0x1200000 + mtd->erasesize) physical bytes.
>   4. The read pointer 'off' exceeds 'mtd->size'.
>   5. Command aborts with: "Read on swufit failed with error -22".
>
> Add a boundary check at the start of the read loop. If the read pointer
> 'off' reaches the end of the physical device, check if the user explicitly
> requested a size (argc >= 2).
>
> * If no size was specified (default read): Stop the read gracefully,
>   print a notice, and return success. We assume the user wants "all
>   available data" up to the physical limit.
> * If a size was explicitly specified: Continue standard execution. This
>   will result in an error (-22) as expected, since the specific requirement
>   cannot be met.
>
> It is physically impossible to read the full logical size of a partition
> if it contains bad blocks (as the bad blocks consume physical space
> required for the data). In this case, truncating the read is reasonable.
> For common use cases like loading FIT images (which often
> contain padding at the end), missing the trailing 0xFF data is acceptable,
> whereas failing the entire load operation is not.
>
> Also add some logging when bad blocks are encountered.
>
> NOTE: This changes the behavior of 'mtd read' when no size is provided.
>       Previously, it failed if the full read was impossible. Now, it
>       succeeds with a truncated buffer. The caller is responsible for
>       verifying data integrity (e.g., via fitImage hash verification).

Thanks for the improvement!

Reviewed-by: Miquel Raynal <miquel.raynal@bootlin.com>

Thanks,
Miquèl

  parent reply	other threads:[~2026-01-08 17:51 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1765892031.git.peter.suti@streamunlimited.com>
2025-12-16 13:40 ` [PATCH 1/2] cmd: mtd: fix read failure on default full partition read with bad blocks Peter Suti
2025-12-20  8:39   ` Michael Nazzareno Trimarchi
2026-01-12  8:36     ` Peter Suti
2026-01-08 17:17   ` Miquel Raynal [this message]
2025-12-16 13:40 ` [PATCH 2/2] cmd: mtd: fix default length calculation when offset is specified Peter Suti
2025-12-20  8:42   ` Michael Nazzareno Trimarchi
2026-01-12  8:39     ` Peter Suti
2026-01-08 17:19   ` Miquel Raynal

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=87zf6oxbut.fsf@bootlin.com \
    --to=miquel.raynal@bootlin.com \
    --cc=andrew.goodbody@linaro.org \
    --cc=michael@amarulasolutions.com \
    --cc=mikhail.kshevetskiy@iopsys.eu \
    --cc=peter.suti@streamunlimited.com \
    --cc=radek.dostal@streamunlimited.com \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=xypron.glpk@gmx.de \
    /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