VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / mtd / inftlmount.c
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.14 2004/08/09 13:57:42 dwmw2 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
44 char inftlmountrev[]="$Revision: 1.14 $";
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  */
53 static 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                                 /*
226                                  *      Most likely this is using the
227                                  *      undocumented qiuck mount feature.
228                                  *      We don't support that, we will need
229                                  *      to erase the hidden block for full
230                                  *      compatibility.
231                                  */
232                                 instr->addr = ip->Reserved0 * inftl->EraseSize;
233                                 instr->len = inftl->EraseSize;
234                                 MTD_ERASE(inftl->mbd.mtd, instr);
235                         }
236                         if ((ip->lastUnit - ip->firstUnit + 1) < ip->virtualUnits) {
237                                 printk(KERN_WARNING "INFTL: Media Header "
238                                         "Partition %d sanity check failed\n"
239                                         "    firstUnit %d : lastUnit %d  >  "
240                                         "virtualUnits %d\n", i, ip->lastUnit,
241                                         ip->firstUnit, ip->Reserved0);
242                                 return -1;
243                         }
244                         if (ip->Reserved1 != 0) {
245                                 printk(KERN_WARNING "INFTL: Media Header "
246                                         "Partition %d sanity check failed: "
247                                         "Reserved1 %d != 0\n",
248                                         i, ip->Reserved1);
249                                 return -1;
250                         }
251
252                         if (ip->flags & INFTL_BDTL)
253                                 break;
254                 }
255
256                 if (i >= 4) {
257                         printk(KERN_WARNING "INFTL: Media Header Partition "
258                                 "sanity check failed:\n       No partition "
259                                 "marked as Disk Partition\n");
260                         return -1;
261                 }
262
263                 inftl->nb_boot_blocks = ip->firstUnit;
264                 inftl->numvunits = ip->virtualUnits;
265                 if (inftl->numvunits > (inftl->nb_blocks -
266                     inftl->nb_boot_blocks - 2)) {
267                         printk(KERN_WARNING "INFTL: Media Header sanity check "
268                                 "failed:\n        numvunits (%d) > nb_blocks "
269                                 "(%d) - nb_boot_blocks(%d) - 2\n",
270                                 inftl->numvunits, inftl->nb_blocks,
271                                 inftl->nb_boot_blocks);
272                         return -1;
273                 }
274                 
275                 inftl->mbd.size  = inftl->numvunits *
276                         (inftl->EraseSize / SECTORSIZE);
277
278                 /*
279                  * Block count is set to last used EUN (we won't need to keep
280                  * any meta-data past that point).
281                  */
282                 inftl->firstEUN = ip->firstUnit;
283                 inftl->lastEUN = ip->lastUnit;
284                 inftl->nb_blocks = ip->lastUnit + 1;
285
286                 /* Memory alloc */
287                 inftl->PUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
288                 if (!inftl->PUtable) {
289                         printk(KERN_WARNING "INFTL: allocation of PUtable "
290                                 "failed (%zd bytes)\n",
291                                 inftl->nb_blocks * sizeof(u16));
292                         return -ENOMEM;
293                 }
294
295                 inftl->VUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
296                 if (!inftl->VUtable) {
297                         kfree(inftl->PUtable);
298                         printk(KERN_WARNING "INFTL: allocation of VUtable "
299                                 "failed (%zd bytes)\n",
300                                 inftl->nb_blocks * sizeof(u16));
301                         return -ENOMEM;
302                 }
303                 
304                 /* Mark the blocks before INFTL MediaHeader as reserved */
305                 for (i = 0; i < inftl->nb_boot_blocks; i++)
306                         inftl->PUtable[i] = BLOCK_RESERVED;
307                 /* Mark all remaining blocks as potentially containing data */
308                 for (; i < inftl->nb_blocks; i++)
309                         inftl->PUtable[i] = BLOCK_NOTEXPLORED;
310
311                 /* Mark this boot record (NFTL MediaHeader) block as reserved */
312                 inftl->PUtable[block] = BLOCK_RESERVED;
313
314                 /* Read Bad Erase Unit Table and modify PUtable[] accordingly */
315                 for (i = 0; i < inftl->nb_blocks; i++) {
316                         int physblock;
317                         /* If any of the physical eraseblocks are bad, don't
318                            use the unit. */
319                         for (physblock = 0; physblock < inftl->EraseSize; physblock += inftl->mbd.mtd->erasesize) {
320                                 if (inftl->mbd.mtd->block_isbad(inftl->mbd.mtd, i * inftl->EraseSize + physblock))
321                                         inftl->PUtable[i] = BLOCK_RESERVED;
322                         }
323                 }
324
325                 inftl->MediaUnit = block;
326                 return 0;
327         }
328
329         /* Not found. */
330         return -1;
331 }
332
333 static int memcmpb(void *a, int c, int n)
334 {
335         int i;
336         for (i = 0; i < n; i++) {
337                 if (c != ((unsigned char *)a)[i])
338                         return 1;
339         }
340         return 0;
341 }
342
343 /*
344  * check_free_sector: check if a free sector is actually FREE,
345  *      i.e. All 0xff in data and oob area.
346  */
347 static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
348         int len, int check_oob)
349 {
350         u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
351         size_t retlen;
352         int i;
353
354         DEBUG(MTD_DEBUG_LEVEL3, "INFTL: check_free_sectors(inftl=%p,"
355                 "address=0x%x,len=%d,check_oob=%d)\n", inftl,
356                 address, len, check_oob);
357
358         for (i = 0; i < len; i += SECTORSIZE) {
359                 if (MTD_READECC(inftl->mbd.mtd, address, SECTORSIZE, &retlen, buf, &buf[SECTORSIZE], &inftl->oobinfo) < 0)
360                         return -1;
361                 if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
362                         return -1;
363
364                 if (check_oob) {
365                         if (memcmpb(buf + SECTORSIZE, 0xff, inftl->mbd.mtd->oobsize) != 0)
366                                 return -1;
367                 }
368                 address += SECTORSIZE;
369         }
370
371         return 0;
372 }
373
374 /*
375  * INFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase
376  *               Unit and Update INFTL metadata. Each erase operation is
377  *               checked with check_free_sectors.
378  *
379  * Return: 0 when succeed, -1 on error.
380  *
381  * ToDo: 1. Is it neceressary to check_free_sector after erasing ?? 
382  */
383 int INFTL_formatblock(struct INFTLrecord *inftl, int block)
384 {
385         size_t retlen;
386         struct inftl_unittail uci;
387         struct erase_info *instr = &inftl->instr;
388         int physblock;
389
390         DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_formatblock(inftl=%p,"
391                 "block=%d)\n", inftl, block);
392
393         memset(instr, 0, sizeof(struct erase_info));
394
395         /* FIXME: Shouldn't we be setting the 'discarded' flag to zero
396            _first_? */
397
398         /* Use async erase interface, test return code */
399         instr->addr = block * inftl->EraseSize;
400         instr->len = inftl->mbd.mtd->erasesize;
401         /* Erase one physical eraseblock at a time, even though the NAND api
402            allows us to group them.  This way we if we have a failure, we can
403            mark only the failed block in the bbt. */
404         for (physblock = 0; physblock < inftl->EraseSize; physblock += instr->len, instr->addr += instr->len) {
405                 MTD_ERASE(inftl->mbd.mtd, instr);
406
407                 if (instr->state == MTD_ERASE_FAILED) {
408                         printk(KERN_WARNING "INFTL: error while formatting block %d\n",
409                                 block);
410                         goto fail;
411                 }
412
413                 /*
414                 * Check the "freeness" of Erase Unit before updating metadata.
415                 * FixMe: is this check really necessary? Since we have check the
416                 *        return code after the erase operation.
417                 */
418                 if (check_free_sectors(inftl, instr->addr, instr->len, 1) != 0)
419                         goto fail;
420         }
421
422         uci.EraseMark = cpu_to_le16(ERASE_MARK);
423         uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
424         uci.Reserved[0] = 0;
425         uci.Reserved[1] = 0;
426         uci.Reserved[2] = 0;
427         uci.Reserved[3] = 0;
428         instr->addr = block * inftl->EraseSize + SECTORSIZE * 2;
429         if (MTD_WRITEOOB(inftl->mbd.mtd, instr->addr +
430             8, 8, &retlen, (char *)&uci) < 0)
431                 goto fail;
432         return 0;
433 fail:
434         /* could not format, update the bad block table (caller is responsible
435            for setting the PUtable to BLOCK_RESERVED on failure) */
436         inftl->mbd.mtd->block_markbad(inftl->mbd.mtd, instr->addr);
437         return -1;
438 }
439
440 /*
441  * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase
442  *      Units in a Virtual Unit Chain, i.e. all the units are disconnected.
443  *
444  *      Since the chain is invalid then we will have to erase it from its
445  *      head (normally for INFTL we go from the oldest). But if it has a
446  *      loop then there is no oldest...
447  */
448 static void format_chain(struct INFTLrecord *inftl, unsigned int first_block)
449 {
450         unsigned int block = first_block, block1;
451
452         printk(KERN_WARNING "INFTL: formatting chain at block %d\n",
453                 first_block);
454
455         for (;;) {
456                 block1 = inftl->PUtable[block];
457
458                 printk(KERN_WARNING "INFTL: formatting block %d\n", block);
459                 if (INFTL_formatblock(inftl, block) < 0) {
460                         /*
461                          * Cannot format !!!! Mark it as Bad Unit,
462                          */
463                         inftl->PUtable[block] = BLOCK_RESERVED;
464                 } else {
465                         inftl->PUtable[block] = BLOCK_FREE;
466                 }
467
468                 /* Goto next block on the chain */
469                 block = block1;
470
471                 if (block == BLOCK_NIL || block >= inftl->lastEUN)
472                         break;
473         }
474 }
475
476 void INFTL_dumptables(struct INFTLrecord *s)
477 {
478         int i;
479
480         printk("-------------------------------------------"
481                 "----------------------------------\n");
482
483         printk("VUtable[%d] ->", s->nb_blocks);
484         for (i = 0; i < s->nb_blocks; i++) {
485                 if ((i % 8) == 0)
486                         printk("\n%04x: ", i);
487                 printk("%04x ", s->VUtable[i]);
488         }
489
490         printk("\n-------------------------------------------"
491                 "----------------------------------\n");
492
493         printk("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks);
494         for (i = 0; i <= s->lastEUN; i++) {
495                 if ((i % 8) == 0)
496                         printk("\n%04x: ", i);
497                 printk("%04x ", s->PUtable[i]);
498         }
499
500         printk("\n-------------------------------------------"
501                 "----------------------------------\n");
502
503         printk("INFTL ->\n"
504                 "  EraseSize       = %d\n"
505                 "  h/s/c           = %d/%d/%d\n"
506                 "  numvunits       = %d\n"
507                 "  firstEUN        = %d\n"
508                 "  lastEUN         = %d\n"
509                 "  numfreeEUNs     = %d\n"
510                 "  LastFreeEUN     = %d\n"
511                 "  nb_blocks       = %d\n"
512                 "  nb_boot_blocks  = %d",
513                 s->EraseSize, s->heads, s->sectors, s->cylinders,
514                 s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs,
515                 s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks);
516
517         printk("\n-------------------------------------------"
518                 "----------------------------------\n");
519 }
520
521 void INFTL_dumpVUchains(struct INFTLrecord *s)
522 {
523         int logical, block, i;
524
525         printk("-------------------------------------------"
526                 "----------------------------------\n");
527
528         printk("INFTL Virtual Unit Chains:\n");
529         for (logical = 0; logical < s->nb_blocks; logical++) {
530                 block = s->VUtable[logical];
531                 if (block > s->nb_blocks)
532                         continue;
533                 printk("  LOGICAL %d --> %d ", logical, block);
534                 for (i = 0; i < s->nb_blocks; i++) {
535                         if (s->PUtable[block] == BLOCK_NIL)
536                                 break;
537                         block = s->PUtable[block];
538                         printk("%d ", block);
539                 }
540                 printk("\n");
541         }
542
543         printk("-------------------------------------------"
544                 "----------------------------------\n");
545 }
546
547 int INFTL_mount(struct INFTLrecord *s)
548 {
549         unsigned int block, first_block, prev_block, last_block;
550         unsigned int first_logical_block, logical_block, erase_mark;
551         int chain_length, do_format_chain;
552         struct inftl_unithead1 h0;
553         struct inftl_unittail h1;
554         size_t retlen;
555         int i;
556         u8 *ANACtable, ANAC;
557
558         DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_mount(inftl=%p)\n", s);
559
560         /* Search for INFTL MediaHeader and Spare INFTL Media Header */
561         if (find_boot_record(s) < 0) {
562                 printk(KERN_WARNING "INFTL: could not find valid boot record?\n");
563                 return -1;
564         }
565
566         /* Init the logical to physical table */
567         for (i = 0; i < s->nb_blocks; i++)
568                 s->VUtable[i] = BLOCK_NIL;
569
570         logical_block = block = BLOCK_NIL;
571
572         /* Temporary buffer to store ANAC numbers. */
573         ANACtable = kmalloc(s->nb_blocks * sizeof(u8), GFP_KERNEL);
574         memset(ANACtable, 0, s->nb_blocks);
575
576         /*
577          * First pass is to explore each physical unit, and construct the
578          * virtual chains that exist (newest physical unit goes into VUtable).
579          * Any block that is in any way invalid will be left in the
580          * NOTEXPLORED state. Then at the end we will try to format it and
581          * mark it as free.
582          */
583         DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 1, explore each unit\n");
584         for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) {
585                 if (s->PUtable[first_block] != BLOCK_NOTEXPLORED)
586                         continue;
587
588                 do_format_chain = 0;
589                 first_logical_block = BLOCK_NIL;
590                 last_block = BLOCK_NIL;
591                 block = first_block;
592
593                 for (chain_length = 0; ; chain_length++) {
594
595                         if ((chain_length == 0) && 
596                             (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
597                                 /* Nothing to do here, onto next block */
598                                 break;
599                         }
600
601                         if (MTD_READOOB(s->mbd.mtd, block * s->EraseSize + 8,
602                             8, &retlen, (char *)&h0) < 0 ||
603                             MTD_READOOB(s->mbd.mtd, block * s->EraseSize +
604                             2 * SECTORSIZE + 8, 8, &retlen, (char *)&h1) < 0) {
605                                 /* Should never happen? */
606                                 do_format_chain++;
607                                 break;
608                         }
609
610                         logical_block = le16_to_cpu(h0.virtualUnitNo);
611                         prev_block = le16_to_cpu(h0.prevUnitNo);
612                         erase_mark = le16_to_cpu((h1.EraseMark | h1.EraseMark1));
613                         ANACtable[block] = h0.ANAC;
614
615                         /* Previous block is relative to start of Partition */
616                         if (prev_block < s->nb_blocks)
617                                 prev_block += s->firstEUN;
618
619                         /* Already explored partial chain? */
620                         if (s->PUtable[block] != BLOCK_NOTEXPLORED) {
621                                 /* Check if chain for this logical */
622                                 if (logical_block == first_logical_block) {
623                                         if (last_block != BLOCK_NIL)
624                                                 s->PUtable[last_block] = block;
625                                 }
626                                 break;
627                         }
628
629                         /* Check for invalid block */
630                         if (erase_mark != ERASE_MARK) {
631                                 printk(KERN_WARNING "INFTL: corrupt block %d "
632                                         "in chain %d, chain length %d, erase "
633                                         "mark 0x%x?\n", block, first_block,
634                                         chain_length, erase_mark);
635                                 /*
636                                  * Assume end of chain, probably incomplete
637                                  * fold/erase...
638                                  */
639                                 if (chain_length == 0)
640                                         do_format_chain++;
641                                 break;
642                         }
643
644                         /* Check for it being free already then... */
645                         if ((logical_block == BLOCK_FREE) ||
646                             (logical_block == BLOCK_NIL)) {
647                                 s->PUtable[block] = BLOCK_FREE;
648                                 break;
649                         }
650
651                         /* Sanity checks on block numbers */
652                         if ((logical_block >= s->nb_blocks) ||
653                             ((prev_block >= s->nb_blocks) &&
654                              (prev_block != BLOCK_NIL))) {
655                                 if (chain_length > 0) {
656                                         printk(KERN_WARNING "INFTL: corrupt "
657                                                 "block %d in chain %d?\n",
658                                                 block, first_block);
659                                         do_format_chain++;
660                                 }
661                                 break;
662                         }
663
664                         if (first_logical_block == BLOCK_NIL) {
665                                 first_logical_block = logical_block;
666                         } else {
667                                 if (first_logical_block != logical_block) {
668                                         /* Normal for folded chain... */
669                                         break;
670                                 }
671                         }
672
673                         /*
674                          * Current block is valid, so if we followed a virtual
675                          * chain to get here then we can set the previous
676                          * block pointer in our PUtable now. Then move onto
677                          * the previous block in the chain.
678                          */
679                         s->PUtable[block] = BLOCK_NIL;
680                         if (last_block != BLOCK_NIL)
681                                 s->PUtable[last_block] = block;
682                         last_block = block;
683                         block = prev_block;
684
685                         /* Check for end of chain */
686                         if (block == BLOCK_NIL)
687                                 break;
688
689                         /* Validate next block before following it... */
690                         if (block > s->lastEUN) {
691                                 printk(KERN_WARNING "INFTL: invalid previous "
692                                         "block %d in chain %d?\n", block,
693                                         first_block);
694                                 do_format_chain++;
695                                 break;
696                         }
697                 }
698
699                 if (do_format_chain) {
700                         format_chain(s, first_block);
701                         continue;
702                 }
703
704                 /*
705                  * Looks like a valid chain then. It may not really be the
706                  * newest block in the chain, but it is the newest we have
707                  * found so far. We might update it in later iterations of
708                  * this loop if we find something newer.
709                  */
710                 s->VUtable[first_logical_block] = first_block;
711                 logical_block = BLOCK_NIL;
712         }
713
714 #ifdef CONFIG_MTD_DEBUG_VERBOSE
715         if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
716                 INFTL_dumptables(s);
717 #endif
718
719         /*
720          * Second pass, check for infinite loops in chains. These are
721          * possible because we don't update the previous pointers when
722          * we fold chains. No big deal, just fix them up in PUtable.
723          */
724         DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 2, validate virtual chains\n");
725         for (logical_block = 0; logical_block < s->numvunits; logical_block++) {
726                 block = s->VUtable[logical_block];
727                 last_block = BLOCK_NIL;
728
729                 /* Check for free/reserved/nil */
730                 if (block >= BLOCK_RESERVED)
731                         continue;
732
733                 ANAC = ANACtable[block];
734                 for (i = 0; i < s->numvunits; i++) {
735                         if (s->PUtable[block] == BLOCK_NIL)
736                                 break;
737                         if (s->PUtable[block] > s->lastEUN) {
738                                 printk(KERN_WARNING "INFTL: invalid prev %d, "
739                                         "in virtual chain %d\n",
740                                         s->PUtable[block], logical_block);
741                                 s->PUtable[block] = BLOCK_NIL;
742                                         
743                         }
744                         if (ANACtable[block] != ANAC) {
745                                 /*
746                                  * Chain must point back to itself. This is ok,
747                                  * but we will need adjust the tables with this
748                                  * newest block and oldest block.
749                                  */
750                                 s->VUtable[logical_block] = block;
751                                 s->PUtable[last_block] = BLOCK_NIL;
752                                 break;
753                         }
754
755                         ANAC--;
756                         last_block = block;
757                         block = s->PUtable[block];
758                 }
759
760                 if (i >= s->nb_blocks) {
761                         /*
762                          * Uhoo, infinite chain with valid ANACS!
763                          * Format whole chain...
764                          */
765                         format_chain(s, first_block);
766                 }
767         }
768
769 #ifdef CONFIG_MTD_DEBUG_VERBOSE
770         if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
771                 INFTL_dumptables(s);
772         if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
773                 INFTL_dumpVUchains(s);
774 #endif
775
776         /*
777          * Third pass, format unreferenced blocks and init free block count.
778          */
779         s->numfreeEUNs = 0;
780         s->LastFreeEUN = BLOCK_NIL;
781
782         DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 3, format unused blocks\n");
783         for (block = s->firstEUN; block <= s->lastEUN; block++) {
784                 if (s->PUtable[block] == BLOCK_NOTEXPLORED) {
785                         printk("INFTL: unreferenced block %d, formatting it\n",
786                                 block);
787                         if (INFTL_formatblock(s, block) < 0)
788                                 s->PUtable[block] = BLOCK_RESERVED;
789                         else
790                                 s->PUtable[block] = BLOCK_FREE;
791                 }
792                 if (s->PUtable[block] == BLOCK_FREE) {
793                         s->numfreeEUNs++;
794                         if (s->LastFreeEUN == BLOCK_NIL)
795                                 s->LastFreeEUN = block;
796                 }
797         }
798
799         kfree(ANACtable);
800         return 0;
801 }