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