@@ -98,6 +98,7 @@ int main(int argc, char *argv[])
int isNAND;
int error = 0;
uint64_t offset = 0;
+ void *oob_data = NULL;
/*
* Process user arguments
@@ -197,15 +198,43 @@ int main(int argc, char *argv[])
if (ioctl(fd, MEMGETOOBSEL, &oobinfo) != 0)
return sys_errmsg("%s: unable to get NAND oobinfo", mtd_device);
+ cleanmarker.totlen = cpu_to_je32(8);
/* Check for autoplacement */
if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) {
- /* Get the position of the free bytes */
- if (!oobinfo.oobfree[0][1])
+ struct nand_ecclayout_user ecclayout;
+ int i, oobavail;
+
+ for (i = 0, oobavail = 0; oobinfo.oobfree[i][1]; i++)
+ oobavail += oobinfo.oobfree[i][1];
+
+ if (!oobavail)
return errmsg(" Eeep. Autoplacement selected and no empty space in oob");
+
+ /* Get the position of the free bytes */
clmpos = oobinfo.oobfree[0][0];
- clmlen = oobinfo.oobfree[0][1];
- if (clmlen > 8)
- clmlen = 8;
+ clmlen = MIN(oobavail, 8);
+
+ if (oobinfo.oobfree[0][1] < 8 && oobavail >= 8) {
+ int left, n, last = 0;
+ void *cm;
+
+ oob_data = malloc(mtd.oob_size);
+ if (!oob_data)
+ return -ENOMEM;
+
+ memset(oob_data, 0xff, mtd.oob_size);
+ cm = &cleanmarker;
+ for (i = 0, left = clmlen; left ; i++) {
+ n = MIN(left, oobinfo.oobfree[i][1]);
+ memcpy(oob_data + oobinfo.oobfree[i][0],
+ cm, n);
+ left -= n;
+ cm += n;
+ last = oobinfo.oobfree[i][0] + n;
+ }
+
+ clmlen = last - clmpos;
+ }
} else {
/* Legacy mode */
switch (mtd.oob_size) {
@@ -223,7 +252,6 @@ int main(int argc, char *argv[])
break;
}
}
- cleanmarker.totlen = cpu_to_je32(8);
}
cleanmarker.hdr_crc = cpu_to_je32(mtd_crc32(0, &cleanmarker, sizeof(cleanmarker) - 4));
}
@@ -272,7 +300,8 @@ int main(int argc, char *argv[])
/* write cleanmarker */
if (isNAND) {
- if (mtd_write_oob(mtd_desc, &mtd, fd, offset + clmpos, clmlen, &cleanmarker) != 0) {
+ void *data = oob_data ? oob_data + clmpos : &cleanmarker;
+ if (mtd_write_oob(mtd_desc, &mtd, fd, offset + clmpos, clmlen, data) != 0) {
sys_errmsg("%s: MTD writeoob failure", mtd_device);
continue;
}
@@ -291,5 +320,8 @@ int main(int argc, char *argv[])
show_progress(&mtd, offset, eb, eb_start, eb_cnt);
bareverbose(!quiet, "\n");
+ if (oob_data)
+ free(oob_data);
+
return 0;
}