aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/inftlmount.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/mtd/inftlmount.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/mtd/inftlmount.c')
-rw-r--r--drivers/mtd/inftlmount.c804
1 files changed, 804 insertions, 0 deletions
diff --git a/drivers/mtd/inftlmount.c b/drivers/mtd/inftlmount.c
new file mode 100644
index 000000000000..b5dda47395a7
--- /dev/null
+++ b/drivers/mtd/inftlmount.c
@@ -0,0 +1,804 @@
1/*
2 * inftlmount.c -- INFTL mount code with extensive checks.
3 *
4 * Author: Greg Ungerer (gerg@snapgear.com)
5 * (C) Copyright 2002-2003, Greg Ungerer (gerg@snapgear.com)
6 *
7 * Based heavily on the nftlmount.c code which is:
8 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
9 * Copyright (C) 2000 Netgem S.A.
10 *
11 * $Id: inftlmount.c,v 1.16 2004/11/22 13:50:53 kalev Exp $
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <asm/errno.h>
31#include <asm/io.h>
32#include <asm/uaccess.h>
33#include <linux/miscdevice.h>
34#include <linux/pci.h>
35#include <linux/delay.h>
36#include <linux/slab.h>
37#include <linux/sched.h>
38#include <linux/init.h>
39#include <linux/mtd/mtd.h>
40#include <linux/mtd/nftl.h>
41#include <linux/mtd/inftl.h>
42#include <linux/mtd/compatmac.h>
43
44char inftlmountrev[]="$Revision: 1.16 $";
45
46/*
47 * find_boot_record: Find the INFTL Media Header and its Spare copy which
48 * contains the various device information of the INFTL partition and
49 * Bad Unit Table. Update the PUtable[] table according to the Bad
50 * Unit Table. PUtable[] is used for management of Erase Unit in
51 * other routines in inftlcore.c and inftlmount.c.
52 */
53static int find_boot_record(struct INFTLrecord *inftl)
54{
55 struct inftl_unittail h1;
56 //struct inftl_oob oob;
57 unsigned int i, block;
58 u8 buf[SECTORSIZE];
59 struct INFTLMediaHeader *mh = &inftl->MediaHdr;
60 struct INFTLPartition *ip;
61 size_t retlen;
62
63 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: find_boot_record(inftl=%p)\n", inftl);
64
65 /*
66 * Assume logical EraseSize == physical erasesize for starting the
67 * scan. We'll sort it out later if we find a MediaHeader which says
68 * otherwise.
69 */
70 inftl->EraseSize = inftl->mbd.mtd->erasesize;
71 inftl->nb_blocks = inftl->mbd.mtd->size / inftl->EraseSize;
72
73 inftl->MediaUnit = BLOCK_NIL;
74
75 /* Search for a valid boot record */
76 for (block = 0; block < inftl->nb_blocks; block++) {
77 int ret;
78
79 /*
80 * Check for BNAND header first. Then whinge if it's found
81 * but later checks fail.
82 */
83 ret = MTD_READ(inftl->mbd.mtd, block * inftl->EraseSize,
84 SECTORSIZE, &retlen, buf);
85 /* We ignore ret in case the ECC of the MediaHeader is invalid
86 (which is apparently acceptable) */
87 if (retlen != SECTORSIZE) {
88 static int warncount = 5;
89
90 if (warncount) {
91 printk(KERN_WARNING "INFTL: block read at 0x%x "
92 "of mtd%d failed: %d\n",
93 block * inftl->EraseSize,
94 inftl->mbd.mtd->index, ret);
95 if (!--warncount)
96 printk(KERN_WARNING "INFTL: further "
97 "failures for this block will "
98 "not be printed\n");
99 }
100 continue;
101 }
102
103 if (retlen < 6 || memcmp(buf, "BNAND", 6)) {
104 /* BNAND\0 not found. Continue */
105 continue;
106 }
107
108 /* To be safer with BIOS, also use erase mark as discriminant */
109 if ((ret = MTD_READOOB(inftl->mbd.mtd, block * inftl->EraseSize +
110 SECTORSIZE + 8, 8, &retlen, (char *)&h1) < 0)) {
111 printk(KERN_WARNING "INFTL: ANAND header found at "
112 "0x%x in mtd%d, but OOB data read failed "
113 "(err %d)\n", block * inftl->EraseSize,
114 inftl->mbd.mtd->index, ret);
115 continue;
116 }
117
118
119 /*
120 * This is the first we've seen.
121 * Copy the media header structure into place.
122 */
123 memcpy(mh, buf, sizeof(struct INFTLMediaHeader));
124
125 /* Read the spare media header at offset 4096 */
126 MTD_READ(inftl->mbd.mtd, block * inftl->EraseSize + 4096,
127 SECTORSIZE, &retlen, buf);
128 if (retlen != SECTORSIZE) {
129 printk(KERN_WARNING "INFTL: Unable to read spare "
130 "Media Header\n");
131 return -1;
132 }
133 /* Check if this one is the same as the first one we found. */
134 if (memcmp(mh, buf, sizeof(struct INFTLMediaHeader))) {
135 printk(KERN_WARNING "INFTL: Primary and spare Media "
136 "Headers disagree.\n");
137 return -1;
138 }
139
140 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
141 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
142 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
143 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
144 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
145 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
146
147#ifdef CONFIG_MTD_DEBUG_VERBOSE
148 if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
149 printk("INFTL: Media Header ->\n"
150 " bootRecordID = %s\n"
151 " NoOfBootImageBlocks = %d\n"
152 " NoOfBinaryPartitions = %d\n"
153 " NoOfBDTLPartitions = %d\n"
154 " BlockMultiplerBits = %d\n"
155 " FormatFlgs = %d\n"
156 " OsakVersion = 0x%x\n"
157 " PercentUsed = %d\n",
158 mh->bootRecordID, mh->NoOfBootImageBlocks,
159 mh->NoOfBinaryPartitions,
160 mh->NoOfBDTLPartitions,
161 mh->BlockMultiplierBits, mh->FormatFlags,
162 mh->OsakVersion, mh->PercentUsed);
163 }
164#endif
165
166 if (mh->NoOfBDTLPartitions == 0) {
167 printk(KERN_WARNING "INFTL: Media Header sanity check "
168 "failed: NoOfBDTLPartitions (%d) == 0, "
169 "must be at least 1\n", mh->NoOfBDTLPartitions);
170 return -1;
171 }
172
173 if ((mh->NoOfBDTLPartitions + mh->NoOfBinaryPartitions) > 4) {
174 printk(KERN_WARNING "INFTL: Media Header sanity check "
175 "failed: Total Partitions (%d) > 4, "
176 "BDTL=%d Binary=%d\n", mh->NoOfBDTLPartitions +
177 mh->NoOfBinaryPartitions,
178 mh->NoOfBDTLPartitions,
179 mh->NoOfBinaryPartitions);
180 return -1;
181 }
182
183 if (mh->BlockMultiplierBits > 1) {
184 printk(KERN_WARNING "INFTL: sorry, we don't support "
185 "UnitSizeFactor 0x%02x\n",
186 mh->BlockMultiplierBits);
187 return -1;
188 } else if (mh->BlockMultiplierBits == 1) {
189 printk(KERN_WARNING "INFTL: support for INFTL with "
190 "UnitSizeFactor 0x%02x is experimental\n",
191 mh->BlockMultiplierBits);
192 inftl->EraseSize = inftl->mbd.mtd->erasesize <<
193 mh->BlockMultiplierBits;
194 inftl->nb_blocks = inftl->mbd.mtd->size / inftl->EraseSize;
195 block >>= mh->BlockMultiplierBits;
196 }
197
198 /* Scan the partitions */
199 for (i = 0; (i < 4); i++) {
200 ip = &mh->Partitions[i];
201 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
202 ip->firstUnit = le32_to_cpu(ip->firstUnit);
203 ip->lastUnit = le32_to_cpu(ip->lastUnit);
204 ip->flags = le32_to_cpu(ip->flags);
205 ip->spareUnits = le32_to_cpu(ip->spareUnits);
206 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
207
208#ifdef CONFIG_MTD_DEBUG_VERBOSE
209 if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
210 printk(" PARTITION[%d] ->\n"
211 " virtualUnits = %d\n"
212 " firstUnit = %d\n"
213 " lastUnit = %d\n"
214 " flags = 0x%x\n"
215 " spareUnits = %d\n",
216 i, ip->virtualUnits, ip->firstUnit,
217 ip->lastUnit, ip->flags,
218 ip->spareUnits);
219 }
220#endif
221
222 if (ip->Reserved0 != ip->firstUnit) {
223 struct erase_info *instr = &inftl->instr;
224
225 instr->mtd = inftl->mbd.mtd;
226
227 /*
228 * Most likely this is using the
229 * undocumented qiuck mount feature.
230 * We don't support that, we will need
231 * to erase the hidden block for full
232 * compatibility.
233 */
234 instr->addr = ip->Reserved0 * inftl->EraseSize;
235 instr->len = inftl->EraseSize;
236 MTD_ERASE(inftl->mbd.mtd, instr);
237 }
238 if ((ip->lastUnit - ip->firstUnit + 1) < ip->virtualUnits) {
239 printk(KERN_WARNING "INFTL: Media Header "
240 "Partition %d sanity check failed\n"
241 " firstUnit %d : lastUnit %d > "
242 "virtualUnits %d\n", i, ip->lastUnit,
243 ip->firstUnit, ip->Reserved0);
244 return -1;
245 }
246 if (ip->Reserved1 != 0) {
247 printk(KERN_WARNING "INFTL: Media Header "
248 "Partition %d sanity check failed: "
249 "Reserved1 %d != 0\n",
250 i, ip->Reserved1);
251 return -1;
252 }
253
254 if (ip->flags & INFTL_BDTL)
255 break;
256 }
257
258 if (i >= 4) {
259 printk(KERN_WARNING "INFTL: Media Header Partition "
260 "sanity check failed:\n No partition "
261 "marked as Disk Partition\n");
262 return -1;
263 }
264
265 inftl->nb_boot_blocks = ip->firstUnit;
266 inftl->numvunits = ip->virtualUnits;
267 if (inftl->numvunits > (inftl->nb_blocks -
268 inftl->nb_boot_blocks - 2)) {
269 printk(KERN_WARNING "INFTL: Media Header sanity check "
270 "failed:\n numvunits (%d) > nb_blocks "
271 "(%d) - nb_boot_blocks(%d) - 2\n",
272 inftl->numvunits, inftl->nb_blocks,
273 inftl->nb_boot_blocks);
274 return -1;
275 }
276
277 inftl->mbd.size = inftl->numvunits *
278 (inftl->EraseSize / SECTORSIZE);
279
280 /*
281 * Block count is set to last used EUN (we won't need to keep
282 * any meta-data past that point).
283 */
284 inftl->firstEUN = ip->firstUnit;
285 inftl->lastEUN = ip->lastUnit;
286 inftl->nb_blocks = ip->lastUnit + 1;
287
288 /* Memory alloc */
289 inftl->PUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
290 if (!inftl->PUtable) {
291 printk(KERN_WARNING "INFTL: allocation of PUtable "
292 "failed (%zd bytes)\n",
293 inftl->nb_blocks * sizeof(u16));
294 return -ENOMEM;
295 }
296
297 inftl->VUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
298 if (!inftl->VUtable) {
299 kfree(inftl->PUtable);
300 printk(KERN_WARNING "INFTL: allocation of VUtable "
301 "failed (%zd bytes)\n",
302 inftl->nb_blocks * sizeof(u16));
303 return -ENOMEM;
304 }
305
306 /* Mark the blocks before INFTL MediaHeader as reserved */
307 for (i = 0; i < inftl->nb_boot_blocks; i++)
308 inftl->PUtable[i] = BLOCK_RESERVED;
309 /* Mark all remaining blocks as potentially containing data */
310 for (; i < inftl->nb_blocks; i++)
311 inftl->PUtable[i] = BLOCK_NOTEXPLORED;
312
313 /* Mark this boot record (NFTL MediaHeader) block as reserved */
314 inftl->PUtable[block] = BLOCK_RESERVED;
315
316 /* Read Bad Erase Unit Table and modify PUtable[] accordingly */
317 for (i = 0; i < inftl->nb_blocks; i++) {
318 int physblock;
319 /* If any of the physical eraseblocks are bad, don't
320 use the unit. */
321 for (physblock = 0; physblock < inftl->EraseSize; physblock += inftl->mbd.mtd->erasesize) {
322 if (inftl->mbd.mtd->block_isbad(inftl->mbd.mtd, i * inftl->EraseSize + physblock))
323 inftl->PUtable[i] = BLOCK_RESERVED;
324 }
325 }
326
327 inftl->MediaUnit = block;
328 return 0;
329 }
330
331 /* Not found. */
332 return -1;
333}
334
335static int memcmpb(void *a, int c, int n)
336{
337 int i;
338 for (i = 0; i < n; i++) {
339 if (c != ((unsigned char *)a)[i])
340 return 1;
341 }
342 return 0;
343}
344
345/*
346 * check_free_sector: check if a free sector is actually FREE,
347 * i.e. All 0xff in data and oob area.
348 */
349static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
350 int len, int check_oob)
351{
352 u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
353 size_t retlen;
354 int i;
355
356 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: check_free_sectors(inftl=%p,"
357 "address=0x%x,len=%d,check_oob=%d)\n", inftl,
358 address, len, check_oob);
359
360 for (i = 0; i < len; i += SECTORSIZE) {
361 if (MTD_READECC(inftl->mbd.mtd, address, SECTORSIZE, &retlen, buf, &buf[SECTORSIZE], &inftl->oobinfo) < 0)
362 return -1;
363 if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
364 return -1;
365
366 if (check_oob) {
367 if (memcmpb(buf + SECTORSIZE, 0xff, inftl->mbd.mtd->oobsize) != 0)
368 return -1;
369 }
370 address += SECTORSIZE;
371 }
372
373 return 0;
374}
375
376/*
377 * INFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase
378 * Unit and Update INFTL metadata. Each erase operation is
379 * checked with check_free_sectors.
380 *
381 * Return: 0 when succeed, -1 on error.
382 *
383 * ToDo: 1. Is it neceressary to check_free_sector after erasing ??
384 */
385int INFTL_formatblock(struct INFTLrecord *inftl, int block)
386{
387 size_t retlen;
388 struct inftl_unittail uci;
389 struct erase_info *instr = &inftl->instr;
390 int physblock;
391
392 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_formatblock(inftl=%p,"
393 "block=%d)\n", inftl, block);
394
395 memset(instr, 0, sizeof(struct erase_info));
396
397 /* FIXME: Shouldn't we be setting the 'discarded' flag to zero
398 _first_? */
399
400 /* Use async erase interface, test return code */
401 instr->mtd = inftl->mbd.mtd;
402 instr->addr = block * inftl->EraseSize;
403 instr->len = inftl->mbd.mtd->erasesize;
404 /* Erase one physical eraseblock at a time, even though the NAND api
405 allows us to group them. This way we if we have a failure, we can
406 mark only the failed block in the bbt. */
407 for (physblock = 0; physblock < inftl->EraseSize; physblock += instr->len, instr->addr += instr->len) {
408 MTD_ERASE(inftl->mbd.mtd, instr);
409
410 if (instr->state == MTD_ERASE_FAILED) {
411 printk(KERN_WARNING "INFTL: error while formatting block %d\n",
412 block);
413 goto fail;
414 }
415
416 /*
417 * Check the "freeness" of Erase Unit before updating metadata.
418 * FixMe: is this check really necessary? Since we have check the
419 * return code after the erase operation.
420 */
421 if (check_free_sectors(inftl, instr->addr, instr->len, 1) != 0)
422 goto fail;
423 }
424
425 uci.EraseMark = cpu_to_le16(ERASE_MARK);
426 uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
427 uci.Reserved[0] = 0;
428 uci.Reserved[1] = 0;
429 uci.Reserved[2] = 0;
430 uci.Reserved[3] = 0;
431 instr->addr = block * inftl->EraseSize + SECTORSIZE * 2;
432 if (MTD_WRITEOOB(inftl->mbd.mtd, instr->addr +
433 8, 8, &retlen, (char *)&uci) < 0)
434 goto fail;
435 return 0;
436fail:
437 /* could not format, update the bad block table (caller is responsible
438 for setting the PUtable to BLOCK_RESERVED on failure) */
439 inftl->mbd.mtd->block_markbad(inftl->mbd.mtd, instr->addr);
440 return -1;
441}
442
443/*
444 * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase
445 * Units in a Virtual Unit Chain, i.e. all the units are disconnected.
446 *
447 * Since the chain is invalid then we will have to erase it from its
448 * head (normally for INFTL we go from the oldest). But if it has a
449 * loop then there is no oldest...
450 */
451static void format_chain(struct INFTLrecord *inftl, unsigned int first_block)
452{
453 unsigned int block = first_block, block1;
454
455 printk(KERN_WARNING "INFTL: formatting chain at block %d\n",
456 first_block);
457
458 for (;;) {
459 block1 = inftl->PUtable[block];
460
461 printk(KERN_WARNING "INFTL: formatting block %d\n", block);
462 if (INFTL_formatblock(inftl, block) < 0) {
463 /*
464 * Cannot format !!!! Mark it as Bad Unit,
465 */
466 inftl->PUtable[block] = BLOCK_RESERVED;
467 } else {
468 inftl->PUtable[block] = BLOCK_FREE;
469 }
470
471 /* Goto next block on the chain */
472 block = block1;
473
474 if (block == BLOCK_NIL || block >= inftl->lastEUN)
475 break;
476 }
477}
478
479void INFTL_dumptables(struct INFTLrecord *s)
480{
481 int i;
482
483 printk("-------------------------------------------"
484 "----------------------------------\n");
485
486 printk("VUtable[%d] ->", s->nb_blocks);
487 for (i = 0; i < s->nb_blocks; i++) {
488 if ((i % 8) == 0)
489 printk("\n%04x: ", i);
490 printk("%04x ", s->VUtable[i]);
491 }
492
493 printk("\n-------------------------------------------"
494 "----------------------------------\n");
495
496 printk("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks);
497 for (i = 0; i <= s->lastEUN; i++) {
498 if ((i % 8) == 0)
499 printk("\n%04x: ", i);
500 printk("%04x ", s->PUtable[i]);
501 }
502
503 printk("\n-------------------------------------------"
504 "----------------------------------\n");
505
506 printk("INFTL ->\n"
507 " EraseSize = %d\n"
508 " h/s/c = %d/%d/%d\n"
509 " numvunits = %d\n"
510 " firstEUN = %d\n"
511 " lastEUN = %d\n"
512 " numfreeEUNs = %d\n"
513 " LastFreeEUN = %d\n"
514 " nb_blocks = %d\n"
515 " nb_boot_blocks = %d",
516 s->EraseSize, s->heads, s->sectors, s->cylinders,
517 s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs,
518 s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks);
519
520 printk("\n-------------------------------------------"
521 "----------------------------------\n");
522}
523
524void INFTL_dumpVUchains(struct INFTLrecord *s)
525{
526 int logical, block, i;
527
528 printk("-------------------------------------------"
529 "----------------------------------\n");
530
531 printk("INFTL Virtual Unit Chains:\n");
532 for (logical = 0; logical < s->nb_blocks; logical++) {
533 block = s->VUtable[logical];
534 if (block > s->nb_blocks)
535 continue;
536 printk(" LOGICAL %d --> %d ", logical, block);
537 for (i = 0; i < s->nb_blocks; i++) {
538 if (s->PUtable[block] == BLOCK_NIL)
539 break;
540 block = s->PUtable[block];
541 printk("%d ", block);
542 }
543 printk("\n");
544 }
545
546 printk("-------------------------------------------"
547 "----------------------------------\n");
548}
549
550int INFTL_mount(struct INFTLrecord *s)
551{
552 unsigned int block, first_block, prev_block, last_block;
553 unsigned int first_logical_block, logical_block, erase_mark;
554 int chain_length, do_format_chain;
555 struct inftl_unithead1 h0;
556 struct inftl_unittail h1;
557 size_t retlen;
558 int i;
559 u8 *ANACtable, ANAC;
560
561 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_mount(inftl=%p)\n", s);
562
563 /* Search for INFTL MediaHeader and Spare INFTL Media Header */
564 if (find_boot_record(s) < 0) {
565 printk(KERN_WARNING "INFTL: could not find valid boot record?\n");
566 return -1;
567 }
568
569 /* Init the logical to physical table */
570 for (i = 0; i < s->nb_blocks; i++)
571 s->VUtable[i] = BLOCK_NIL;
572
573 logical_block = block = BLOCK_NIL;
574
575 /* Temporary buffer to store ANAC numbers. */
576 ANACtable = kmalloc(s->nb_blocks * sizeof(u8), GFP_KERNEL);
577 memset(ANACtable, 0, s->nb_blocks);
578
579 /*
580 * First pass is to explore each physical unit, and construct the
581 * virtual chains that exist (newest physical unit goes into VUtable).
582 * Any block that is in any way invalid will be left in the
583 * NOTEXPLORED state. Then at the end we will try to format it and
584 * mark it as free.
585 */
586 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 1, explore each unit\n");
587 for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) {
588 if (s->PUtable[first_block] != BLOCK_NOTEXPLORED)
589 continue;
590
591 do_format_chain = 0;
592 first_logical_block = BLOCK_NIL;
593 last_block = BLOCK_NIL;
594 block = first_block;
595
596 for (chain_length = 0; ; chain_length++) {
597
598 if ((chain_length == 0) &&
599 (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
600 /* Nothing to do here, onto next block */
601 break;
602 }
603
604 if (MTD_READOOB(s->mbd.mtd, block * s->EraseSize + 8,
605 8, &retlen, (char *)&h0) < 0 ||
606 MTD_READOOB(s->mbd.mtd, block * s->EraseSize +
607 2 * SECTORSIZE + 8, 8, &retlen, (char *)&h1) < 0) {
608 /* Should never happen? */
609 do_format_chain++;
610 break;
611 }
612
613 logical_block = le16_to_cpu(h0.virtualUnitNo);
614 prev_block = le16_to_cpu(h0.prevUnitNo);
615 erase_mark = le16_to_cpu((h1.EraseMark | h1.EraseMark1));
616 ANACtable[block] = h0.ANAC;
617
618 /* Previous block is relative to start of Partition */
619 if (prev_block < s->nb_blocks)
620 prev_block += s->firstEUN;
621
622 /* Already explored partial chain? */
623 if (s->PUtable[block] != BLOCK_NOTEXPLORED) {
624 /* Check if chain for this logical */
625 if (logical_block == first_logical_block) {
626 if (last_block != BLOCK_NIL)
627 s->PUtable[last_block] = block;
628 }
629 break;
630 }
631
632 /* Check for invalid block */
633 if (erase_mark != ERASE_MARK) {
634 printk(KERN_WARNING "INFTL: corrupt block %d "
635 "in chain %d, chain length %d, erase "
636 "mark 0x%x?\n", block, first_block,
637 chain_length, erase_mark);
638 /*
639 * Assume end of chain, probably incomplete
640 * fold/erase...
641 */
642 if (chain_length == 0)
643 do_format_chain++;
644 break;
645 }
646
647 /* Check for it being free already then... */
648 if ((logical_block == BLOCK_FREE) ||
649 (logical_block == BLOCK_NIL)) {
650 s->PUtable[block] = BLOCK_FREE;
651 break;
652 }
653
654 /* Sanity checks on block numbers */
655 if ((logical_block >= s->nb_blocks) ||
656 ((prev_block >= s->nb_blocks) &&
657 (prev_block != BLOCK_NIL))) {
658 if (chain_length > 0) {
659 printk(KERN_WARNING "INFTL: corrupt "
660 "block %d in chain %d?\n",
661 block, first_block);
662 do_format_chain++;
663 }
664 break;
665 }
666
667 if (first_logical_block == BLOCK_NIL) {
668 first_logical_block = logical_block;
669 } else {
670 if (first_logical_block != logical_block) {
671 /* Normal for folded chain... */
672 break;
673 }
674 }
675
676 /*
677 * Current block is valid, so if we followed a virtual
678 * chain to get here then we can set the previous
679 * block pointer in our PUtable now. Then move onto
680 * the previous block in the chain.
681 */
682 s->PUtable[block] = BLOCK_NIL;
683 if (last_block != BLOCK_NIL)
684 s->PUtable[last_block] = block;
685 last_block = block;
686 block = prev_block;
687
688 /* Check for end of chain */
689 if (block == BLOCK_NIL)
690 break;
691
692 /* Validate next block before following it... */
693 if (block > s->lastEUN) {
694 printk(KERN_WARNING "INFTL: invalid previous "
695 "block %d in chain %d?\n", block,
696 first_block);
697 do_format_chain++;
698 break;
699 }
700 }
701
702 if (do_format_chain) {
703 format_chain(s, first_block);
704 continue;
705 }
706
707 /*
708 * Looks like a valid chain then. It may not really be the
709 * newest block in the chain, but it is the newest we have
710 * found so far. We might update it in later iterations of
711 * this loop if we find something newer.
712 */
713 s->VUtable[first_logical_block] = first_block;
714 logical_block = BLOCK_NIL;
715 }
716
717#ifdef CONFIG_MTD_DEBUG_VERBOSE
718 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
719 INFTL_dumptables(s);
720#endif
721
722 /*
723 * Second pass, check for infinite loops in chains. These are
724 * possible because we don't update the previous pointers when
725 * we fold chains. No big deal, just fix them up in PUtable.
726 */
727 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 2, validate virtual chains\n");
728 for (logical_block = 0; logical_block < s->numvunits; logical_block++) {
729 block = s->VUtable[logical_block];
730 last_block = BLOCK_NIL;
731
732 /* Check for free/reserved/nil */
733 if (block >= BLOCK_RESERVED)
734 continue;
735
736 ANAC = ANACtable[block];
737 for (i = 0; i < s->numvunits; i++) {
738 if (s->PUtable[block] == BLOCK_NIL)
739 break;
740 if (s->PUtable[block] > s->lastEUN) {
741 printk(KERN_WARNING "INFTL: invalid prev %d, "
742 "in virtual chain %d\n",
743 s->PUtable[block], logical_block);
744 s->PUtable[block] = BLOCK_NIL;
745
746 }
747 if (ANACtable[block] != ANAC) {
748 /*
749 * Chain must point back to itself. This is ok,
750 * but we will need adjust the tables with this
751 * newest block and oldest block.
752 */
753 s->VUtable[logical_block] = block;
754 s->PUtable[last_block] = BLOCK_NIL;
755 break;
756 }
757
758 ANAC--;
759 last_block = block;
760 block = s->PUtable[block];
761 }
762
763 if (i >= s->nb_blocks) {
764 /*
765 * Uhoo, infinite chain with valid ANACS!
766 * Format whole chain...
767 */
768 format_chain(s, first_block);
769 }
770 }
771
772#ifdef CONFIG_MTD_DEBUG_VERBOSE
773 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
774 INFTL_dumptables(s);
775 if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
776 INFTL_dumpVUchains(s);
777#endif
778
779 /*
780 * Third pass, format unreferenced blocks and init free block count.
781 */
782 s->numfreeEUNs = 0;
783 s->LastFreeEUN = BLOCK_NIL;
784
785 DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 3, format unused blocks\n");
786 for (block = s->firstEUN; block <= s->lastEUN; block++) {
787 if (s->PUtable[block] == BLOCK_NOTEXPLORED) {
788 printk("INFTL: unreferenced block %d, formatting it\n",
789 block);
790 if (INFTL_formatblock(s, block) < 0)
791 s->PUtable[block] = BLOCK_RESERVED;
792 else
793 s->PUtable[block] = BLOCK_FREE;
794 }
795 if (s->PUtable[block] == BLOCK_FREE) {
796 s->numfreeEUNs++;
797 if (s->LastFreeEUN == BLOCK_NIL)
798 s->LastFreeEUN = block;
799 }
800 }
801
802 kfree(ANACtable);
803 return 0;
804}