1 From patchwork Mon Aug 8 08:16:43 2011
2 Content-Type: text/plain; charset="utf-8"
4 Content-Transfer-Encoding: 7bit
5 Subject: mtd-utils: fix corrupt cleanmarker with flash_erase -j command
6 Date: Sun, 07 Aug 2011 22:16:43 -0000
7 From: b35362@freescale.com
9 Message-Id: <1312791403-13473-1-git-send-email-b35362@freescale.com>
10 To: <dwmw2@infradead.org>
11 Cc: Liu Shuo <b35362@freescale.com>, Artem.Bityutskiy@nokia.com,
12 Li Yang <leoli@freescale.com>, linux-mtd@lists.infradead.org
14 From: Liu Shuo <b35362@freescale.com>
16 Flash_erase -j should fill discrete freeoob areas with required bytes
17 of JFFS2 cleanmarker in jffs2_check_nand_cleanmarker(). Not just fill
18 the first freeoob area.
20 The below is the result without this workaround:
22 JFFS2: Erase block at 0x00000000 is not formatted. It will be erased
23 JFFS2: Erase block at 0x00004000 is not formatted. It will be erased
24 JFFS2: Erase block at 0x00008000 is not formatted. It will be erased
25 JFFS2: Erase block at 0x0000c000 is not formatted. It will be erased
26 JFFS2: Erase block at 0x00010000 is not formatted. It will be erased
27 JFFS2: Erase block at 0x00014000 is not formatted. It will be erased
28 JFFS2: Erase block at 0x00018000 is not formatted. It will be erased
29 JFFS2: Erase block at 0x0001c000 is not formatted. It will be erased
30 JFFS2: Erase block at 0x00020000 is not formatted. It will be erased
31 JFFS2: Erase block at 0x00024000 is not formatted. It will be erased
32 JFFS2: Erase block at 0x00028000 is not formatted. It will be erased
35 Signed-off-by: Liu Shuo <b35362@freescale.com>
36 Signed-off-by: Li Yang <leoli@freescale.com>
39 v2 : get length of availble freeoob bytes from oobinfo information,
40 not use the ioctl ECCGETLAYOUT which is being deprecated.
42 flash_erase.c | 46 +++++++++++++++++++++++++++++++++++++++-------
43 1 files changed, 39 insertions(+), 7 deletions(-)
45 diff --git a/flash_erase.c b/flash_erase.c
46 index fe2eaca..3e94495 100644
49 @@ -98,6 +98,7 @@ int main(int argc, char *argv[])
53 + void *oob_data = NULL;
56 * Process user arguments
57 @@ -197,15 +198,43 @@ int main(int argc, char *argv[])
58 if (ioctl(fd, MEMGETOOBSEL, &oobinfo) != 0)
59 return sys_errmsg("%s: unable to get NAND oobinfo", mtd_device);
61 + cleanmarker.totlen = cpu_to_je32(8);
62 /* Check for autoplacement */
63 if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) {
64 - /* Get the position of the free bytes */
65 - if (!oobinfo.oobfree[0][1])
66 + struct nand_ecclayout_user ecclayout;
69 + for (i = 0, oobavail = 0; oobinfo.oobfree[i][1]; i++)
70 + oobavail += oobinfo.oobfree[i][1];
73 return errmsg(" Eeep. Autoplacement selected and no empty space in oob");
75 + /* Get the position of the free bytes */
76 clmpos = oobinfo.oobfree[0][0];
77 - clmlen = oobinfo.oobfree[0][1];
80 + clmlen = MIN(oobavail, 8);
82 + if (oobinfo.oobfree[0][1] < 8 && oobavail >= 8) {
83 + int left, n, last = 0;
86 + oob_data = malloc(mtd.oob_size);
90 + memset(oob_data, 0xff, mtd.oob_size);
92 + for (i = 0, left = clmlen; left ; i++) {
93 + n = MIN(left, oobinfo.oobfree[i][1]);
94 + memcpy(oob_data + oobinfo.oobfree[i][0],
98 + last = oobinfo.oobfree[i][0] + n;
101 + clmlen = last - clmpos;
105 switch (mtd.oob_size) {
106 @@ -223,7 +252,6 @@ int main(int argc, char *argv[])
110 - cleanmarker.totlen = cpu_to_je32(8);
112 cleanmarker.hdr_crc = cpu_to_je32(mtd_crc32(0, &cleanmarker, sizeof(cleanmarker) - 4));
114 @@ -272,7 +300,8 @@ int main(int argc, char *argv[])
116 /* write cleanmarker */
118 - if (mtd_write_oob(mtd_desc, &mtd, fd, offset + clmpos, clmlen, &cleanmarker) != 0) {
119 + void *data = oob_data ? oob_data + clmpos : &cleanmarker;
120 + if (mtd_write_oob(mtd_desc, &mtd, fd, offset + clmpos, clmlen, data) != 0) {
121 sys_errmsg("%s: MTD writeoob failure", mtd_device);
124 @@ -291,5 +320,8 @@ int main(int argc, char *argv[])
125 show_progress(&mtd, offset, eb, eb_start, eb_cnt);
126 bareverbose(!quiet, "\n");