diff --git a/nand-image-builder.c b/nand-image-builder.c
index 8b37d00dc4fda2faf02328357c425c19c9eb76ba..a9e394e1820297a6d855ac0776840e5abb938a7c 100644
--- a/nand-image-builder.c
+++ b/nand-image-builder.c
@@ -122,11 +122,13 @@ int main(int argc, char **argv)
     int pages_in_block, i = 0;
     const char *input_file = NULL;
     const char *output_file = NULL;
-    FILE *in_file = NULL, *out_file = NULL;
+    char *tes_file = "test.out";
+    FILE *in_file = NULL, *out_file = NULL, *test_file = NULL;
     struct stat st;
     uint8_t *buffer;
     uint8_t cmp_buffer[SECTOR_SZ * SECTORS_PER_PAGE];
     uint8_t ecc[OOB_ECC_LEN];
+    int erased_block = 1;
 
     /* Initialize BCH lib data and parameters */
 
@@ -224,6 +226,13 @@ int main(int argc, char **argv)
         goto exit;
     }
 
+    test_file = fopen(tes_file, "wb");
+    if (out_file == NULL)
+    {
+        printf("Out file open error\n");
+        goto exit;
+    }
+
     fseek(in_file, 0, SEEK_SET);
     fseek(out_file, 0, SEEK_SET);
 
@@ -234,7 +243,7 @@ int main(int argc, char **argv)
         goto exit;
     }
 
-   /* if ((st.st_size % block_size) != 0)
+    /* if ((st.st_size % block_size) != 0)
     {
         printf("File size not multiple of block size\n");
         goto exit;
@@ -297,14 +306,13 @@ int main(int argc, char **argv)
     printf("OOB_ECC_LEN %d\n", oob_ecc_len);
     printf("OOB_ECC_OFS %d\n", oob_ecc_ofs);
 
-    
-   while (!feof(in_file))
- 
+    while (!feof(in_file))
+
     {
 
         clear_pagebuffer();
 
-        numBytesRead = fread(page_buffer, (size_t)1, (sector_sz ) * SECTORS_PER_PAGE, in_file);
+        numBytesRead = fread(page_buffer, (size_t)1, (sector_sz)*SECTORS_PER_PAGE, in_file);
         totalBytesRead += numBytesRead;
 
         /* printf("\r\nRead %d bytes [%d%%] from file...\r\n",
@@ -317,7 +325,8 @@ int main(int argc, char **argv)
             goto exit;
         }
 
-	if (numBytesRead != (sector_sz ) * SECTORS_PER_PAGE) break; // skip if byte is less than bytes read from fread api.
+        if (numBytesRead != (sector_sz)*SECTORS_PER_PAGE)
+            break; // skip if byte is less than bytes read from fread api.
 
         memset(cmp_buffer, 0, sector_sz * SECTORS_PER_PAGE);
 
@@ -327,24 +336,62 @@ int main(int argc, char **argv)
 		  for(i=0; i < 2048; i++)
 			printf("0x%X\t", cmp_buffer[i]); */
 
-        for (i = 0; i != SECTORS_PER_PAGE; ++i)
-        {
-
-            const uint8_t *sector_data = page_buffer + sector_sz * i;
-            uint8_t *sector_oob = page_buffer + sector_sz * SECTORS_PER_PAGE + oob_sz * i;
-
-            memset(sector_oob + oob_ecc_ofs, 0xff, oob_ecc_len);
-
-            memset(buffer, 0, sector_sz + oob_ecc_ofs + 1);
-            shift_half_byte(sector_data, buffer, sector_sz);
-            shift_half_byte(sector_oob, buffer + sector_sz, oob_ecc_ofs);
-            // compute ECC
+        //for (i = 0; i != SECTOR_SZ * SECTORS_PER_PAGE; ++i)
+        //		if (page_buffer[i] == 0xff)
+        //		{
+        //			++erased_block;
+        //			//break;
+        //		}
 
-            memset(ecc, 0, oob_ecc_len);
-            encode_bch(bch, buffer, sector_sz + oob_ecc_ofs + 1, ecc);
-            // copy the result in its OOB block, shifting right by 4 bits
-            shift_half_byte(ecc, sector_oob + oob_ecc_ofs, oob_ecc_len - 1);
-            sector_oob[oob_ecc_ofs + oob_ecc_len - 1] |= ecc[oob_ecc_len - 1] >> 4;
+        for (i = 0; i < SECTOR_SZ * SECTORS_PER_PAGE; i += 4)
+        {
+            if (*(uint32_t *)(page_buffer + i) != 0xffffffff)
+            {
+                for (i = 0; i != SECTORS_PER_PAGE; ++i)
+                {
+
+                    const uint8_t *sector_data = page_buffer + sector_sz * i;
+                    uint8_t *sector_oob = page_buffer + sector_sz * SECTORS_PER_PAGE + oob_sz * i;
+
+                    //if (erased_block)
+                    //{
+                    // erased page ECC consumes full 7 bytes, including high 4 bits set to 0xf
+                    memset(sector_oob + oob_ecc_ofs, 0xff, oob_ecc_len);
+                    //}
+
+                    //  memset(sector_oob + oob_ecc_ofs, 0xff, oob_ecc_len);
+
+                    memset(buffer, 0, sector_sz + oob_ecc_ofs);
+
+                    shift_half_byte(sector_data, buffer, sector_sz);
+                    //if(i == 1)
+                    //fwrite(buffer, sector_sz , 1, out_file);
+
+                    shift_half_byte(sector_oob, buffer + sector_sz, oob_ecc_ofs);
+                    //  if(i == 1) {
+                    //fwrite(buffer, sector_sz + oob_ecc_ofs  , 1, test_file);
+                    //goto exit;
+                    //}
+                    // compute ECC
+                    // if(i == 2)
+                    // fwrite(buffer, sector_sz , 1, out_file);
+
+                    memset(ecc, 0, oob_ecc_len);
+                    encode_bch(bch, buffer, sector_sz, ecc);
+                    // copy the result in its OOB block, shifting right by 4 bits
+
+                    shift_half_byte(ecc, sector_oob + oob_ecc_ofs - 1, oob_ecc_len);
+
+                    sector_oob[oob_ecc_ofs + oob_ecc_len - 1] |= ecc[oob_ecc_len - 1] >> 4;
+
+                    //fwrite(sector_oob , oob_ecc_ofs + oob_ecc_len , 1, test_file);
+                    //if(i == 2) {
+                    //fwrite(page_buffer, (sector_sz + oob_sz) * SECTORS_PER_PAGE, 1, test_file);
+                    //	goto exit;
+                    //	}
+                }
+            }
+            break;
         }
 
         fwrite(page_buffer, (sector_sz + oob_sz) * SECTORS_PER_PAGE, 1, out_file);
@@ -362,10 +409,9 @@ int main(int argc, char **argv)
 
         if (!(count & countMask))
         {
-           /* printf("block No  %d\r\n", blocks_num); */
+            /* printf("block No  %d\r\n", blocks_num); */
             blocks_num++;
         }
-
     }
 
 exit: