blob: 444a77bb7692134ada6fd673502f9ea03c1891bb [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Thomas Gleixner97894cd2005-11-07 11:15:26 +00002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * NFTL mount code with extensive checks
4 *
Thomas Gleixner97894cd2005-11-07 11:15:26 +00005 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
David Woodhousea1452a32010-08-08 20:58:20 +01006 * Copyright © 2000 Netgem S.A.
7 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/kernel.h>
11#include <asm/errno.h>
12#include <linux/delay.h>
13#include <linux/slab.h>
14#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020015#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/mtd/nftl.h>
17
18#define SECTORSIZE 512
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/* find_boot_record: Find the NFTL Media Header and its Spare copy which contains the
21 * various device information of the NFTL partition and Bad Unit Table. Update
Brian Norris92394b5c2011-07-20 09:53:42 -070022 * the ReplUnitTable[] table according to the Bad Unit Table. ReplUnitTable[]
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * is used for management of Erase Unit in other routines in nftl.c and nftlmount.c
24 */
25static int find_boot_record(struct NFTLrecord *nftl)
26{
27 struct nftl_uci1 h1;
28 unsigned int block, boot_record_count = 0;
29 size_t retlen;
30 u8 buf[SECTORSIZE];
31 struct NFTLMediaHeader *mh = &nftl->MediaHdr;
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +020032 struct mtd_info *mtd = nftl->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 unsigned int i;
34
Thomas Gleixner97894cd2005-11-07 11:15:26 +000035 /* Assume logical EraseSize == physical erasesize for starting the scan.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 We'll sort it out later if we find a MediaHeader which says otherwise */
37 /* Actually, we won't. The new DiskOnChip driver has already scanned
38 the MediaHeader and adjusted the virtual erasesize it presents in
39 the mtd device accordingly. We could even get rid of
40 nftl->EraseSize if there were any point in doing so. */
41 nftl->EraseSize = nftl->mbd.mtd->erasesize;
Adrian Hunter69423d92008-12-10 13:37:21 +000042 nftl->nb_blocks = (u32)nftl->mbd.mtd->size / nftl->EraseSize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 nftl->MediaUnit = BLOCK_NIL;
45 nftl->SpareMediaUnit = BLOCK_NIL;
46
47 /* search for a valid boot record */
48 for (block = 0; block < nftl->nb_blocks; block++) {
49 int ret;
50
51 /* Check for ANAND header first. Then can whinge if it's found but later
52 checks fail */
Artem Bityutskiy329ad392011-12-23 17:30:16 +020053 ret = mtd_read(mtd, block * nftl->EraseSize, SECTORSIZE,
54 &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 /* We ignore ret in case the ECC of the MediaHeader is invalid
56 (which is apparently acceptable) */
57 if (retlen != SECTORSIZE) {
58 static int warncount = 5;
59
60 if (warncount) {
61 printk(KERN_WARNING "Block read at 0x%x of mtd%d failed: %d\n",
62 block * nftl->EraseSize, nftl->mbd.mtd->index, ret);
63 if (!--warncount)
64 printk(KERN_WARNING "Further failures for this block will not be printed\n");
65 }
66 continue;
67 }
68
69 if (retlen < 6 || memcmp(buf, "ANAND", 6)) {
70 /* ANAND\0 not found. Continue */
71#if 0
Thomas Gleixner97894cd2005-11-07 11:15:26 +000072 printk(KERN_DEBUG "ANAND header not found at 0x%x in mtd%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 block * nftl->EraseSize, nftl->mbd.mtd->index);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000074#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 continue;
76 }
77
78 /* To be safer with BIOS, also use erase mark as discriminant */
Andy Shevchenko768c57c2015-01-07 22:37:20 +020079 ret = nftl_read_oob(mtd, block * nftl->EraseSize +
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +020080 SECTORSIZE + 8, 8, &retlen,
Andy Shevchenko768c57c2015-01-07 22:37:20 +020081 (char *)&h1);
82 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 printk(KERN_WARNING "ANAND header found at 0x%x in mtd%d, but OOB data read failed (err %d)\n",
84 block * nftl->EraseSize, nftl->mbd.mtd->index, ret);
85 continue;
86 }
87
88#if 0 /* Some people seem to have devices without ECC or erase marks
89 on the Media Header blocks. There are enough other sanity
90 checks in here that we can probably do without it.
91 */
92 if (le16_to_cpu(h1.EraseMark | h1.EraseMark1) != ERASE_MARK) {
93 printk(KERN_NOTICE "ANAND header found at 0x%x in mtd%d, but erase mark not present (0x%04x,0x%04x instead)\n",
Thomas Gleixner97894cd2005-11-07 11:15:26 +000094 block * nftl->EraseSize, nftl->mbd.mtd->index,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 le16_to_cpu(h1.EraseMark), le16_to_cpu(h1.EraseMark1));
96 continue;
97 }
98
99 /* Finally reread to check ECC */
Andy Shevchenko768c57c2015-01-07 22:37:20 +0200100 ret = mtd->read(mtd, block * nftl->EraseSize, SECTORSIZE,
101 &retlen, buf);
102 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 printk(KERN_NOTICE "ANAND header found at 0x%x in mtd%d, but ECC read failed (err %d)\n",
104 block * nftl->EraseSize, nftl->mbd.mtd->index, ret);
105 continue;
106 }
107
108 /* Paranoia. Check the ANAND header is still there after the ECC read */
109 if (memcmp(buf, "ANAND", 6)) {
110 printk(KERN_NOTICE "ANAND header found at 0x%x in mtd%d, but went away on reread!\n",
111 block * nftl->EraseSize, nftl->mbd.mtd->index);
Antonio Cardaceac9cd362018-02-13 17:52:45 +0000112 printk(KERN_NOTICE "New data are: %6ph\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 continue;
114 }
115#endif
116 /* OK, we like it. */
117
118 if (boot_record_count) {
119 /* We've already processed one. So we just check if
120 this one is the same as the first one we found */
121 if (memcmp(mh, buf, sizeof(struct NFTLMediaHeader))) {
122 printk(KERN_NOTICE "NFTL Media Headers at 0x%x and 0x%x disagree.\n",
123 nftl->MediaUnit * nftl->EraseSize, block * nftl->EraseSize);
124 /* if (debug) Print both side by side */
125 if (boot_record_count < 2) {
126 /* We haven't yet seen two real ones */
127 return -1;
128 }
129 continue;
130 }
131 if (boot_record_count == 1)
132 nftl->SpareMediaUnit = block;
133
134 /* Mark this boot record (NFTL MediaHeader) block as reserved */
135 nftl->ReplUnitTable[block] = BLOCK_RESERVED;
136
137
138 boot_record_count++;
139 continue;
140 }
141
142 /* This is the first we've seen. Copy the media header structure into place */
143 memcpy(mh, buf, sizeof(struct NFTLMediaHeader));
144
145 /* Do some sanity checks on it */
146#if 0
147The new DiskOnChip driver scans the MediaHeader itself, and presents a virtual
148erasesize based on UnitSizeFactor. So the erasesize we read from the mtd
149device is already correct.
150 if (mh->UnitSizeFactor == 0) {
151 printk(KERN_NOTICE "NFTL: UnitSizeFactor 0x00 detected. This violates the spec but we think we know what it means...\n");
152 } else if (mh->UnitSizeFactor < 0xfc) {
153 printk(KERN_NOTICE "Sorry, we don't support UnitSizeFactor 0x%02x\n",
154 mh->UnitSizeFactor);
155 return -1;
156 } else if (mh->UnitSizeFactor != 0xff) {
157 printk(KERN_NOTICE "WARNING: Support for NFTL with UnitSizeFactor 0x%02x is experimental\n",
158 mh->UnitSizeFactor);
159 nftl->EraseSize = nftl->mbd.mtd->erasesize << (0xff - mh->UnitSizeFactor);
Adrian Hunter69423d92008-12-10 13:37:21 +0000160 nftl->nb_blocks = (u32)nftl->mbd.mtd->size / nftl->EraseSize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162#endif
163 nftl->nb_boot_blocks = le16_to_cpu(mh->FirstPhysicalEUN);
164 if ((nftl->nb_boot_blocks + 2) >= nftl->nb_blocks) {
165 printk(KERN_NOTICE "NFTL Media Header sanity check failed:\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000166 printk(KERN_NOTICE "nb_boot_blocks (%d) + 2 > nb_blocks (%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 nftl->nb_boot_blocks, nftl->nb_blocks);
168 return -1;
169 }
170
171 nftl->numvunits = le32_to_cpu(mh->FormattedSize) / nftl->EraseSize;
172 if (nftl->numvunits > (nftl->nb_blocks - nftl->nb_boot_blocks - 2)) {
173 printk(KERN_NOTICE "NFTL Media Header sanity check failed:\n");
174 printk(KERN_NOTICE "numvunits (%d) > nb_blocks (%d) - nb_boot_blocks(%d) - 2\n",
175 nftl->numvunits, nftl->nb_blocks, nftl->nb_boot_blocks);
176 return -1;
177 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 nftl->mbd.size = nftl->numvunits * (nftl->EraseSize / SECTORSIZE);
180
181 /* If we're not using the last sectors in the device for some reason,
182 reduce nb_blocks accordingly so we forget they're there */
183 nftl->nb_blocks = le16_to_cpu(mh->NumEraseUnits) + le16_to_cpu(mh->FirstPhysicalEUN);
184
185 /* XXX: will be suppressed */
186 nftl->lastEUN = nftl->nb_blocks - 1;
187
188 /* memory alloc */
Kees Cook6da2ec52018-06-12 13:55:00 -0700189 nftl->EUNtable = kmalloc_array(nftl->nb_blocks, sizeof(u16),
190 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (!nftl->EUNtable) {
192 printk(KERN_NOTICE "NFTL: allocation of EUNtable failed\n");
193 return -ENOMEM;
194 }
195
Kees Cook6da2ec52018-06-12 13:55:00 -0700196 nftl->ReplUnitTable = kmalloc_array(nftl->nb_blocks,
197 sizeof(u16),
198 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!nftl->ReplUnitTable) {
200 kfree(nftl->EUNtable);
201 printk(KERN_NOTICE "NFTL: allocation of ReplUnitTable failed\n");
202 return -ENOMEM;
203 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* mark the bios blocks (blocks before NFTL MediaHeader) as reserved */
206 for (i = 0; i < nftl->nb_boot_blocks; i++)
207 nftl->ReplUnitTable[i] = BLOCK_RESERVED;
208 /* mark all remaining blocks as potentially containing data */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000209 for (; i < nftl->nb_blocks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 nftl->ReplUnitTable[i] = BLOCK_NOTEXPLORED;
211 }
212
213 /* Mark this boot record (NFTL MediaHeader) block as reserved */
214 nftl->ReplUnitTable[block] = BLOCK_RESERVED;
215
216 /* read the Bad Erase Unit Table and modify ReplUnitTable[] accordingly */
217 for (i = 0; i < nftl->nb_blocks; i++) {
218#if 0
219The new DiskOnChip driver already scanned the bad block table. Just query it.
220 if ((i & (SECTORSIZE - 1)) == 0) {
221 /* read one sector for every SECTORSIZE of blocks */
Andy Shevchenko768c57c2015-01-07 22:37:20 +0200222 ret = mtd->read(nftl->mbd.mtd,
223 block * nftl->EraseSize + i +
224 SECTORSIZE, SECTORSIZE,
225 &retlen, buf);
226 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 printk(KERN_NOTICE "Read of bad sector table failed (err %d)\n",
228 ret);
229 kfree(nftl->ReplUnitTable);
230 kfree(nftl->EUNtable);
231 return -1;
232 }
233 }
234 /* mark the Bad Erase Unit as RESERVED in ReplUnitTable */
235 if (buf[i & (SECTORSIZE - 1)] != 0xff)
236 nftl->ReplUnitTable[i] = BLOCK_RESERVED;
237#endif
Artem Bityutskiy7086c192011-12-23 19:35:30 +0200238 if (mtd_block_isbad(nftl->mbd.mtd,
239 i * nftl->EraseSize))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 nftl->ReplUnitTable[i] = BLOCK_RESERVED;
241 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 nftl->MediaUnit = block;
244 boot_record_count++;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 } /* foreach (block) */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return boot_record_count?0:-1;
249}
250
251static int memcmpb(void *a, int c, int n)
252{
253 int i;
254 for (i = 0; i < n; i++) {
255 if (c != ((unsigned char *)a)[i])
256 return 1;
257 }
258 return 0;
259}
260
261/* check_free_sector: check if a free sector is actually FREE, i.e. All 0xff in data and oob area */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000262static int check_free_sectors(struct NFTLrecord *nftl, unsigned int address, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 int check_oob)
264{
Thomas Gleixner9223a452006-05-23 17:21:03 +0200265 struct mtd_info *mtd = nftl->mbd.mtd;
266 size_t retlen;
Kees Cook27ab41e2018-04-29 08:00:53 -0700267 int i, ret;
268 u8 *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Kees Cook27ab41e2018-04-29 08:00:53 -0700270 buf = kmalloc(SECTORSIZE + mtd->oobsize, GFP_KERNEL);
271 if (!buf)
272 return -1;
273
274 ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 for (i = 0; i < len; i += SECTORSIZE) {
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200276 if (mtd_read(mtd, address, SECTORSIZE, &retlen, buf))
Kees Cook27ab41e2018-04-29 08:00:53 -0700277 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
Kees Cook27ab41e2018-04-29 08:00:53 -0700279 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (check_oob) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200282 if(nftl_read_oob(mtd, address, mtd->oobsize,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200283 &retlen, &buf[SECTORSIZE]) < 0)
Kees Cook27ab41e2018-04-29 08:00:53 -0700284 goto out;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200285 if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
Kees Cook27ab41e2018-04-29 08:00:53 -0700286 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 address += SECTORSIZE;
289 }
290
Kees Cook27ab41e2018-04-29 08:00:53 -0700291 ret = 0;
292
293out:
294 kfree(buf);
295 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298/* NFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase Unit and
299 * Update NFTL metadata. Each erase operation is checked with check_free_sectors
300 *
301 * Return: 0 when succeed, -1 on error.
302 *
Brian Norris92394b5c2011-07-20 09:53:42 -0700303 * ToDo: 1. Is it necessary to check_free_sector after erasing ??
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305int NFTL_formatblock(struct NFTLrecord *nftl, int block)
306{
307 size_t retlen;
308 unsigned int nb_erases, erase_mark;
309 struct nftl_uci1 uci;
310 struct erase_info *instr = &nftl->instr;
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200311 struct mtd_info *mtd = nftl->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /* Read the Unit Control Information #1 for Wear-Leveling */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200314 if (nftl_read_oob(mtd, block * nftl->EraseSize + SECTORSIZE + 8,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200315 8, &retlen, (char *)&uci) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 goto default_uci1;
317
318 erase_mark = le16_to_cpu ((uci.EraseMark | uci.EraseMark1));
319 if (erase_mark != ERASE_MARK) {
320 default_uci1:
321 uci.EraseMark = cpu_to_le16(ERASE_MARK);
322 uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
323 uci.WearInfo = cpu_to_le32(0);
324 }
325
326 memset(instr, 0, sizeof(struct erase_info));
327
328 /* XXX: use async erase interface, XXX: test return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 instr->addr = block * nftl->EraseSize;
330 instr->len = nftl->EraseSize;
Boris Brezillon884cfd92018-02-12 22:03:09 +0100331 if (mtd_erase(mtd, instr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 printk("Error while formatting block %d\n", block);
333 goto fail;
334 }
335
Colin Ian King4845a072018-11-18 16:36:56 +0000336 /* increase and write Wear-Leveling info */
337 nb_erases = le32_to_cpu(uci.WearInfo);
338 nb_erases++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Colin Ian King4845a072018-11-18 16:36:56 +0000340 /* wrap (almost impossible with current flash) or free block */
341 if (nb_erases == 0)
342 nb_erases = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Colin Ian King4845a072018-11-18 16:36:56 +0000344 /* check the "freeness" of Erase Unit before updating metadata
345 * FixMe: is this check really necessary ? since we have check the
346 * return code after the erase operation.
347 */
348 if (check_free_sectors(nftl, instr->addr, nftl->EraseSize, 1) != 0)
349 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Colin Ian King4845a072018-11-18 16:36:56 +0000351 uci.WearInfo = le32_to_cpu(nb_erases);
352 if (nftl_write_oob(mtd, block * nftl->EraseSize + SECTORSIZE +
353 8, 8, &retlen, (char *)&uci) < 0)
354 goto fail;
355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356fail:
357 /* could not format, update the bad block table (caller is responsible
358 for setting the ReplUnitTable to BLOCK_RESERVED on failure) */
Artem Bityutskiy5942ddb2011-12-23 19:37:38 +0200359 mtd_block_markbad(nftl->mbd.mtd, instr->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return -1;
361}
362
363/* check_sectors_in_chain: Check that each sector of a Virtual Unit Chain is correct.
364 * Mark as 'IGNORE' each incorrect sector. This check is only done if the chain
365 * was being folded when NFTL was interrupted.
366 *
Brian Norris92394b5c2011-07-20 09:53:42 -0700367 * The check_free_sectors in this function is necessary. There is a possible
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * situation that after writing the Data area, the Block Control Information is
369 * not updated according (due to power failure or something) which leaves the block
Brian Norris92394b5c2011-07-20 09:53:42 -0700370 * in an inconsistent state. So we have to check if a block is really FREE in this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * case. */
372static void check_sectors_in_chain(struct NFTLrecord *nftl, unsigned int first_block)
373{
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200374 struct mtd_info *mtd = nftl->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 unsigned int block, i, status;
376 struct nftl_bci bci;
377 int sectors_per_block;
378 size_t retlen;
379
380 sectors_per_block = nftl->EraseSize / SECTORSIZE;
381 block = first_block;
382 for (;;) {
383 for (i = 0; i < sectors_per_block; i++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200384 if (nftl_read_oob(mtd,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200385 block * nftl->EraseSize + i * SECTORSIZE,
386 8, &retlen, (char *)&bci) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 status = SECTOR_IGNORE;
388 else
389 status = bci.Status | bci.Status1;
390
391 switch(status) {
392 case SECTOR_FREE:
393 /* verify that the sector is really free. If not, mark
394 as ignore */
395 if (memcmpb(&bci, 0xff, 8) != 0 ||
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000396 check_free_sectors(nftl, block * nftl->EraseSize + i * SECTORSIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 SECTORSIZE, 0) != 0) {
398 printk("Incorrect free sector %d in block %d: "
399 "marking it as ignored\n",
400 i, block);
401
402 /* sector not free actually : mark it as SECTOR_IGNORE */
403 bci.Status = SECTOR_IGNORE;
404 bci.Status1 = SECTOR_IGNORE;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200405 nftl_write_oob(mtd, block *
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200406 nftl->EraseSize +
407 i * SECTORSIZE, 8,
408 &retlen, (char *)&bci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410 break;
411 default:
412 break;
413 }
414 }
415
416 /* proceed to next Erase Unit on the chain */
417 block = nftl->ReplUnitTable[block];
418 if (!(block == BLOCK_NIL || block < nftl->nb_blocks))
419 printk("incorrect ReplUnitTable[] : %d\n", block);
420 if (block == BLOCK_NIL || block >= nftl->nb_blocks)
421 break;
422 }
423}
424
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200425/* calc_chain_length: Walk through a Virtual Unit Chain and estimate chain length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426static int calc_chain_length(struct NFTLrecord *nftl, unsigned int first_block)
427{
428 unsigned int length = 0, block = first_block;
429
430 for (;;) {
431 length++;
Brian Norris92394b5c2011-07-20 09:53:42 -0700432 /* avoid infinite loops, although this is guaranteed not to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 happen because of the previous checks */
434 if (length >= nftl->nb_blocks) {
435 printk("nftl: length too long %d !\n", length);
436 break;
437 }
438
439 block = nftl->ReplUnitTable[block];
440 if (!(block == BLOCK_NIL || block < nftl->nb_blocks))
441 printk("incorrect ReplUnitTable[] : %d\n", block);
442 if (block == BLOCK_NIL || block >= nftl->nb_blocks)
443 break;
444 }
445 return length;
446}
447
448/* format_chain: Format an invalid Virtual Unit chain. It frees all the Erase Units in a
449 * Virtual Unit Chain, i.e. all the units are disconnected.
450 *
Brian Norris92394b5c2011-07-20 09:53:42 -0700451 * It is not strictly correct to begin from the first block of the chain because
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * if we stop the code, we may see again a valid chain if there was a first_block
453 * flag in a block inside it. But is it really a problem ?
454 *
Brian Norris92394b5c2011-07-20 09:53:42 -0700455 * FixMe: Figure out what the last statement means. What if power failure when we are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * in the for (;;) loop formatting blocks ??
457 */
458static void format_chain(struct NFTLrecord *nftl, unsigned int first_block)
459{
460 unsigned int block = first_block, block1;
461
462 printk("Formatting chain at block %d\n", first_block);
463
464 for (;;) {
465 block1 = nftl->ReplUnitTable[block];
466
467 printk("Formatting block %d\n", block);
468 if (NFTL_formatblock(nftl, block) < 0) {
469 /* cannot format !!!! Mark it as Bad Unit */
470 nftl->ReplUnitTable[block] = BLOCK_RESERVED;
471 } else {
472 nftl->ReplUnitTable[block] = BLOCK_FREE;
473 }
474
475 /* goto next block on the chain */
476 block = block1;
477
478 if (!(block == BLOCK_NIL || block < nftl->nb_blocks))
479 printk("incorrect ReplUnitTable[] : %d\n", block);
480 if (block == BLOCK_NIL || block >= nftl->nb_blocks)
481 break;
482 }
483}
484
485/* check_and_mark_free_block: Verify that a block is free in the NFTL sense (valid erase mark) or
486 * totally free (only 0xff).
487 *
488 * Definition: Free Erase Unit -- A properly erased/formatted Free Erase Unit should have meet the
Brian Norris92394b5c2011-07-20 09:53:42 -0700489 * following criteria:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * 1. */
491static int check_and_mark_free_block(struct NFTLrecord *nftl, int block)
492{
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200493 struct mtd_info *mtd = nftl->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct nftl_uci1 h1;
495 unsigned int erase_mark;
496 size_t retlen;
497
498 /* check erase mark. */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200499 if (nftl_read_oob(mtd, block * nftl->EraseSize + SECTORSIZE + 8, 8,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200500 &retlen, (char *)&h1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -1;
502
503 erase_mark = le16_to_cpu ((h1.EraseMark | h1.EraseMark1));
504 if (erase_mark != ERASE_MARK) {
505 /* if no erase mark, the block must be totally free. This is
Brian Norris92394b5c2011-07-20 09:53:42 -0700506 possible in two cases : empty filesystem or interrupted erase (very unlikely) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (check_free_sectors (nftl, block * nftl->EraseSize, nftl->EraseSize, 1) != 0)
508 return -1;
509
510 /* free block : write erase mark */
511 h1.EraseMark = cpu_to_le16(ERASE_MARK);
512 h1.EraseMark1 = cpu_to_le16(ERASE_MARK);
513 h1.WearInfo = cpu_to_le32(0);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200514 if (nftl_write_oob(mtd,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200515 block * nftl->EraseSize + SECTORSIZE + 8, 8,
516 &retlen, (char *)&h1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return -1;
518 } else {
519#if 0
520 /* if erase mark present, need to skip it when doing check */
521 for (i = 0; i < nftl->EraseSize; i += SECTORSIZE) {
522 /* check free sector */
523 if (check_free_sectors (nftl, block * nftl->EraseSize + i,
524 SECTORSIZE, 0) != 0)
525 return -1;
526
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200527 if (nftl_read_oob(mtd, block * nftl->EraseSize + i,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200528 16, &retlen, buf) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return -1;
530 if (i == SECTORSIZE) {
531 /* skip erase mark */
532 if (memcmpb(buf, 0xff, 8))
533 return -1;
534 } else {
535 if (memcmpb(buf, 0xff, 16))
536 return -1;
537 }
538 }
539#endif
540 }
541
542 return 0;
543}
544
545/* get_fold_mark: Read fold mark from Unit Control Information #2, we use FOLD_MARK_IN_PROGRESS
546 * to indicate that we are in the progression of a Virtual Unit Chain folding. If the UCI #2
547 * is FOLD_MARK_IN_PROGRESS when mounting the NFTL, the (previous) folding process is interrupted
Brian Norris92394b5c2011-07-20 09:53:42 -0700548 * for some reason. A clean up/check of the VUC is necessary in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *
550 * WARNING: return 0 if read error
551 */
552static int get_fold_mark(struct NFTLrecord *nftl, unsigned int block)
553{
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200554 struct mtd_info *mtd = nftl->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct nftl_uci2 uci;
556 size_t retlen;
557
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200558 if (nftl_read_oob(mtd, block * nftl->EraseSize + 2 * SECTORSIZE + 8,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200559 8, &retlen, (char *)&uci) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return 0;
561
562 return le16_to_cpu((uci.FoldMark | uci.FoldMark1));
563}
564
565int NFTL_mount(struct NFTLrecord *s)
566{
567 int i;
Colin Ian Kingfb60e872018-07-04 07:46:09 +0100568 unsigned int first_logical_block, logical_block, rep_block, erase_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 unsigned int block, first_block, is_first_block;
570 int chain_length, do_format_chain;
571 struct nftl_uci0 h0;
572 struct nftl_uci1 h1;
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200573 struct mtd_info *mtd = s->mbd.mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 size_t retlen;
575
576 /* search for NFTL MediaHeader and Spare NFTL Media Header */
577 if (find_boot_record(s) < 0) {
578 printk("Could not find valid boot record\n");
579 return -1;
580 }
581
582 /* init the logical to physical table */
583 for (i = 0; i < s->nb_blocks; i++) {
584 s->EUNtable[i] = BLOCK_NIL;
585 }
586
587 /* first pass : explore each block chain */
588 first_logical_block = 0;
589 for (first_block = 0; first_block < s->nb_blocks; first_block++) {
590 /* if the block was not already explored, we can look at it */
591 if (s->ReplUnitTable[first_block] == BLOCK_NOTEXPLORED) {
592 block = first_block;
593 chain_length = 0;
594 do_format_chain = 0;
595
596 for (;;) {
597 /* read the block header. If error, we format the chain */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200598 if (nftl_read_oob(mtd,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200599 block * s->EraseSize + 8, 8,
600 &retlen, (char *)&h0) < 0 ||
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200601 nftl_read_oob(mtd,
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200602 block * s->EraseSize +
603 SECTORSIZE + 8, 8,
604 &retlen, (char *)&h1) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 s->ReplUnitTable[block] = BLOCK_NIL;
606 do_format_chain = 1;
607 break;
608 }
609
610 logical_block = le16_to_cpu ((h0.VirtUnitNum | h0.SpareVirtUnitNum));
611 rep_block = le16_to_cpu ((h0.ReplUnitNum | h0.SpareReplUnitNum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 erase_mark = le16_to_cpu ((h1.EraseMark | h1.EraseMark1));
613
614 is_first_block = !(logical_block >> 15);
615 logical_block = logical_block & 0x7fff;
616
617 /* invalid/free block test */
618 if (erase_mark != ERASE_MARK || logical_block >= s->nb_blocks) {
619 if (chain_length == 0) {
620 /* if not currently in a chain, we can handle it safely */
621 if (check_and_mark_free_block(s, block) < 0) {
622 /* not really free: format it */
623 printk("Formatting block %d\n", block);
624 if (NFTL_formatblock(s, block) < 0) {
625 /* could not format: reserve the block */
626 s->ReplUnitTable[block] = BLOCK_RESERVED;
627 } else {
628 s->ReplUnitTable[block] = BLOCK_FREE;
629 }
630 } else {
631 /* free block: mark it */
632 s->ReplUnitTable[block] = BLOCK_FREE;
633 }
634 /* directly examine the next block. */
635 goto examine_ReplUnitTable;
636 } else {
637 /* the block was in a chain : this is bad. We
638 must format all the chain */
639 printk("Block %d: free but referenced in chain %d\n",
640 block, first_block);
641 s->ReplUnitTable[block] = BLOCK_NIL;
642 do_format_chain = 1;
643 break;
644 }
645 }
646
647 /* we accept only first blocks here */
648 if (chain_length == 0) {
649 /* this block is not the first block in chain :
650 ignore it, it will be included in a chain
651 later, or marked as not explored */
652 if (!is_first_block)
653 goto examine_ReplUnitTable;
654 first_logical_block = logical_block;
655 } else {
656 if (logical_block != first_logical_block) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000657 printk("Block %d: incorrect logical block: %d expected: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 block, logical_block, first_logical_block);
659 /* the chain is incorrect : we must format it,
Brian Norris92394b5c2011-07-20 09:53:42 -0700660 but we need to read it completely */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 do_format_chain = 1;
662 }
663 if (is_first_block) {
664 /* we accept that a block is marked as first
665 block while being last block in a chain
666 only if the chain is being folded */
667 if (get_fold_mark(s, block) != FOLD_MARK_IN_PROGRESS ||
668 rep_block != 0xffff) {
669 printk("Block %d: incorrectly marked as first block in chain\n",
670 block);
671 /* the chain is incorrect : we must format it,
Brian Norris92394b5c2011-07-20 09:53:42 -0700672 but we need to read it completely */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 do_format_chain = 1;
674 } else {
675 printk("Block %d: folding in progress - ignoring first block flag\n",
676 block);
677 }
678 }
679 }
680 chain_length++;
681 if (rep_block == 0xffff) {
682 /* no more blocks after */
683 s->ReplUnitTable[block] = BLOCK_NIL;
684 break;
685 } else if (rep_block >= s->nb_blocks) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000686 printk("Block %d: referencing invalid block %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 block, rep_block);
688 do_format_chain = 1;
689 s->ReplUnitTable[block] = BLOCK_NIL;
690 break;
691 } else if (s->ReplUnitTable[rep_block] != BLOCK_NOTEXPLORED) {
692 /* same problem as previous 'is_first_block' test:
693 we accept that the last block of a chain has
694 the first_block flag set if folding is in
695 progress. We handle here the case where the
696 last block appeared first */
697 if (s->ReplUnitTable[rep_block] == BLOCK_NIL &&
698 s->EUNtable[first_logical_block] == rep_block &&
699 get_fold_mark(s, first_block) == FOLD_MARK_IN_PROGRESS) {
700 /* EUNtable[] will be set after */
701 printk("Block %d: folding in progress - ignoring first block flag\n",
702 rep_block);
703 s->ReplUnitTable[block] = rep_block;
704 s->EUNtable[first_logical_block] = BLOCK_NIL;
705 } else {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000706 printk("Block %d: referencing block %d already in another chain\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 block, rep_block);
708 /* XXX: should handle correctly fold in progress chains */
709 do_format_chain = 1;
710 s->ReplUnitTable[block] = BLOCK_NIL;
711 }
712 break;
713 } else {
714 /* this is OK */
715 s->ReplUnitTable[block] = rep_block;
716 block = rep_block;
717 }
718 }
719
720 /* the chain was completely explored. Now we can decide
721 what to do with it */
722 if (do_format_chain) {
723 /* invalid chain : format it */
724 format_chain(s, first_block);
725 } else {
726 unsigned int first_block1, chain_to_format, chain_length1;
727 int fold_mark;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* valid chain : get foldmark */
730 fold_mark = get_fold_mark(s, first_block);
731 if (fold_mark == 0) {
732 /* cannot get foldmark : format the chain */
733 printk("Could read foldmark at block %d\n", first_block);
734 format_chain(s, first_block);
735 } else {
736 if (fold_mark == FOLD_MARK_IN_PROGRESS)
737 check_sectors_in_chain(s, first_block);
738
739 /* now handle the case where we find two chains at the
740 same virtual address : we select the longer one,
741 because the shorter one is the one which was being
742 folded if the folding was not done in place */
743 first_block1 = s->EUNtable[first_logical_block];
744 if (first_block1 != BLOCK_NIL) {
745 /* XXX: what to do if same length ? */
746 chain_length1 = calc_chain_length(s, first_block1);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000747 printk("Two chains at blocks %d (len=%d) and %d (len=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 first_block1, chain_length1, first_block, chain_length);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (chain_length >= chain_length1) {
751 chain_to_format = first_block1;
752 s->EUNtable[first_logical_block] = first_block;
753 } else {
754 chain_to_format = first_block;
755 }
756 format_chain(s, chain_to_format);
757 } else {
758 s->EUNtable[first_logical_block] = first_block;
759 }
760 }
761 }
762 }
763 examine_ReplUnitTable:;
764 }
765
766 /* second pass to format unreferenced blocks and init free block count */
767 s->numfreeEUNs = 0;
768 s->LastFreeEUN = le16_to_cpu(s->MediaHdr.FirstPhysicalEUN);
769
770 for (block = 0; block < s->nb_blocks; block++) {
771 if (s->ReplUnitTable[block] == BLOCK_NOTEXPLORED) {
772 printk("Unreferenced block %d, formatting it\n", block);
773 if (NFTL_formatblock(s, block) < 0)
774 s->ReplUnitTable[block] = BLOCK_RESERVED;
775 else
776 s->ReplUnitTable[block] = BLOCK_FREE;
777 }
778 if (s->ReplUnitTable[block] == BLOCK_FREE) {
779 s->numfreeEUNs++;
780 s->LastFreeEUN = block;
781 }
782 }
783
784 return 0;
785}