From bec02d57fb85cc7dd0196a54b0e9530e306623ac Mon Sep 17 00:00:00 2001 From: Doug Zongker Date: Wed, 1 Jul 2009 12:09:29 -0700 Subject: [PATCH] skip over all-zero blocks when reading MTD partition We fail to detect certain bad blocks (marked in the factory as bad, I think?) when reading mtd partitions. These come back as a block of all zeros. Since it's fairly unlikely a legitimate boot or recovery block will contain 128k of zeros, change mtdutils to skip over such blocks. Arve says https://review.source.android.com/10535 may be a long-term fix for this, but he isn't yet sure. --- mtdutils/mtdutils.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mtdutils/mtdutils.c b/mtdutils/mtdutils.c index 2b0106f..fc06766 100644 --- a/mtdutils/mtdutils.c +++ b/mtdutils/mtdutils.c @@ -297,7 +297,14 @@ static int read_block(const MtdPartition *partition, int fd, char *data) after.corrected - before.corrected, after.failed - before.failed, pos); } else { - return 0; // Success! + int i; + for (i = 0; i < size; ++i) { + if (data[i] != 0) { + return 0; // Success! + } + } + fprintf(stderr, "mtd: read all-zero block at 0x%08lx; skipping\n", + pos); } pos += partition->erase_size; @@ -326,6 +333,10 @@ ssize_t mtd_read_data(MtdReadContext *ctx, char *data, size_t len) read += ctx->partition->erase_size; } + if (read >= len) { + return read; + } + // Read the next block into the buffer if (ctx->consumed == ctx->partition->erase_size && read < (int) len) { if (read_block(ctx->partition, ctx->fd, ctx->buffer)) return -1;