[msm_shared/nand]: Fix bad-block calculation for flash read

Fix calculation of offset according to bad blocks present before the
current page.

Without this change flash read would fail if number of bad blocks before
current_block are more than or equal to difference between current_block
and start_block.

Change-Id: I3c68639a534c0e1ede9c6152b9c0094baede6b55
diff --git a/platform/msm_shared/nand.c b/platform/msm_shared/nand.c
index 0a72902..6be6179 100644
--- a/platform/msm_shared/nand.c
+++ b/platform/msm_shared/nand.c
@@ -3318,6 +3318,7 @@
 	unsigned start_block = ptn->start;
 	int result = 0;
 	int isbad = 0;
+	int start_block_count = 0;
 
 	ASSERT(ptn->type == TYPE_APPS_PARTITION);
 	set_nand_configuration(TYPE_APPS_PARTITION);
@@ -3325,16 +3326,21 @@
 	if(offset & (flash_pagesize - 1))
 		return -1;
 
-	// Adjust page offset based on number of bad blocks from start to current page
-	while (start_block < current_block) {
-		isbad = _flash_block_isbad(flash_cmdlist, flash_ptrlist, start_block*64);
-		if (isbad)
-			page += 64;
-
-		start_block++;
+// Adjust page offset based on number of bad blocks from start to current page
+	if (start_block < current_block)
+	{
+		start_block_count = (current_block - start_block);
+		while (start_block_count && (start_block < (ptn->start + ptn->length))) {
+			isbad = _flash_block_isbad(flash_cmdlist, flash_ptrlist, start_block*64);
+			if (isbad)
+				page += 64;
+			else
+				start_block_count--;
+			start_block++;
+		}
 	}
 
-	while(page < lastpage) {
+	while((page < lastpage) && !start_block_count) {
 		if(count == 0) {
 			dprintf(INFO, "flash_read_image: success (%d errors)\n", errors);
 			return 0;