1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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>
31#include <linux/delay.h>
32#include <linux/slab.h>
33#include <linux/mtd/mtd.h>
34#include <linux/mtd/nftl.h>
35#include <linux/mtd/inftl.h>
36
37
38
39
40
41
42
43
44static int find_boot_record(struct INFTLrecord *inftl)
45{
46 struct inftl_unittail h1;
47
48 unsigned int i, block;
49 u8 buf[SECTORSIZE];
50 struct INFTLMediaHeader *mh = &inftl->MediaHdr;
51 struct mtd_info *mtd = inftl->mbd.mtd;
52 struct INFTLPartition *ip;
53 size_t retlen;
54
55 pr_debug("INFTL: find_boot_record(inftl=%p)\n", inftl);
56
57
58
59
60
61
62 inftl->EraseSize = inftl->mbd.mtd->erasesize;
63 inftl->nb_blocks = (u32)inftl->mbd.mtd->size / inftl->EraseSize;
64
65 inftl->MediaUnit = BLOCK_NIL;
66
67
68 for (block = 0; block < inftl->nb_blocks; block++) {
69 int ret;
70
71
72
73
74
75 ret = mtd_read(mtd, block * inftl->EraseSize, SECTORSIZE,
76 &retlen, buf);
77
78
79 if (retlen != SECTORSIZE) {
80 static int warncount = 5;
81
82 if (warncount) {
83 printk(KERN_WARNING "INFTL: block read at 0x%x "
84 "of mtd%d failed: %d\n",
85 block * inftl->EraseSize,
86 inftl->mbd.mtd->index, ret);
87 if (!--warncount)
88 printk(KERN_WARNING "INFTL: further "
89 "failures for this block will "
90 "not be printed\n");
91 }
92 continue;
93 }
94
95 if (retlen < 6 || memcmp(buf, "BNAND", 6)) {
96
97 continue;
98 }
99
100
101 ret = inftl_read_oob(mtd,
102 block * inftl->EraseSize + SECTORSIZE + 8,
103 8, &retlen,(char *)&h1);
104 if (ret < 0) {
105 printk(KERN_WARNING "INFTL: ANAND header found at "
106 "0x%x in mtd%d, but OOB data read failed "
107 "(err %d)\n", block * inftl->EraseSize,
108 inftl->mbd.mtd->index, ret);
109 continue;
110 }
111
112
113
114
115
116
117 memcpy(mh, buf, sizeof(struct INFTLMediaHeader));
118
119
120 mtd_read(mtd, block * inftl->EraseSize + 4096, SECTORSIZE,
121 &retlen, buf);
122 if (retlen != SECTORSIZE) {
123 printk(KERN_WARNING "INFTL: Unable to read spare "
124 "Media Header\n");
125 return -1;
126 }
127
128 if (memcmp(mh, buf, sizeof(struct INFTLMediaHeader))) {
129 printk(KERN_WARNING "INFTL: Primary and spare Media "
130 "Headers disagree.\n");
131 return -1;
132 }
133
134 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
135 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
136 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
137 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
138 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
139 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
140
141 pr_debug("INFTL: Media Header ->\n"
142 " bootRecordID = %s\n"
143 " NoOfBootImageBlocks = %d\n"
144 " NoOfBinaryPartitions = %d\n"
145 " NoOfBDTLPartitions = %d\n"
146 " BlockMultiplerBits = %d\n"
147 " FormatFlgs = %d\n"
148 " OsakVersion = 0x%x\n"
149 " PercentUsed = %d\n",
150 mh->bootRecordID, mh->NoOfBootImageBlocks,
151 mh->NoOfBinaryPartitions,
152 mh->NoOfBDTLPartitions,
153 mh->BlockMultiplierBits, mh->FormatFlags,
154 mh->OsakVersion, mh->PercentUsed);
155
156 if (mh->NoOfBDTLPartitions == 0) {
157 printk(KERN_WARNING "INFTL: Media Header sanity check "
158 "failed: NoOfBDTLPartitions (%d) == 0, "
159 "must be at least 1\n", mh->NoOfBDTLPartitions);
160 return -1;
161 }
162
163 if ((mh->NoOfBDTLPartitions + mh->NoOfBinaryPartitions) > 4) {
164 printk(KERN_WARNING "INFTL: Media Header sanity check "
165 "failed: Total Partitions (%d) > 4, "
166 "BDTL=%d Binary=%d\n", mh->NoOfBDTLPartitions +
167 mh->NoOfBinaryPartitions,
168 mh->NoOfBDTLPartitions,
169 mh->NoOfBinaryPartitions);
170 return -1;
171 }
172
173 if (mh->BlockMultiplierBits > 1) {
174 printk(KERN_WARNING "INFTL: sorry, we don't support "
175 "UnitSizeFactor 0x%02x\n",
176 mh->BlockMultiplierBits);
177 return -1;
178 } else if (mh->BlockMultiplierBits == 1) {
179 printk(KERN_WARNING "INFTL: support for INFTL with "
180 "UnitSizeFactor 0x%02x is experimental\n",
181 mh->BlockMultiplierBits);
182 inftl->EraseSize = inftl->mbd.mtd->erasesize <<
183 mh->BlockMultiplierBits;
184 inftl->nb_blocks = (u32)inftl->mbd.mtd->size / inftl->EraseSize;
185 block >>= mh->BlockMultiplierBits;
186 }
187
188
189 for (i = 0; (i < 4); i++) {
190 ip = &mh->Partitions[i];
191 ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
192 ip->firstUnit = le32_to_cpu(ip->firstUnit);
193 ip->lastUnit = le32_to_cpu(ip->lastUnit);
194 ip->flags = le32_to_cpu(ip->flags);
195 ip->spareUnits = le32_to_cpu(ip->spareUnits);
196 ip->Reserved0 = le32_to_cpu(ip->Reserved0);
197
198 pr_debug(" PARTITION[%d] ->\n"
199 " virtualUnits = %d\n"
200 " firstUnit = %d\n"
201 " lastUnit = %d\n"
202 " flags = 0x%x\n"
203 " spareUnits = %d\n",
204 i, ip->virtualUnits, ip->firstUnit,
205 ip->lastUnit, ip->flags,
206 ip->spareUnits);
207
208 if (ip->Reserved0 != ip->firstUnit) {
209 struct erase_info *instr = &inftl->instr;
210
211 instr->mtd = inftl->mbd.mtd;
212
213
214
215
216
217
218
219
220 instr->addr = ip->Reserved0 * inftl->EraseSize;
221 instr->len = inftl->EraseSize;
222 mtd_erase(mtd, instr);
223 }
224 if ((ip->lastUnit - ip->firstUnit + 1) < ip->virtualUnits) {
225 printk(KERN_WARNING "INFTL: Media Header "
226 "Partition %d sanity check failed\n"
227 " firstUnit %d : lastUnit %d > "
228 "virtualUnits %d\n", i, ip->lastUnit,
229 ip->firstUnit, ip->Reserved0);
230 return -1;
231 }
232 if (ip->Reserved1 != 0) {
233 printk(KERN_WARNING "INFTL: Media Header "
234 "Partition %d sanity check failed: "
235 "Reserved1 %d != 0\n",
236 i, ip->Reserved1);
237 return -1;
238 }
239
240 if (ip->flags & INFTL_BDTL)
241 break;
242 }
243
244 if (i >= 4) {
245 printk(KERN_WARNING "INFTL: Media Header Partition "
246 "sanity check failed:\n No partition "
247 "marked as Disk Partition\n");
248 return -1;
249 }
250
251 inftl->nb_boot_blocks = ip->firstUnit;
252 inftl->numvunits = ip->virtualUnits;
253 if (inftl->numvunits > (inftl->nb_blocks -
254 inftl->nb_boot_blocks - 2)) {
255 printk(KERN_WARNING "INFTL: Media Header sanity check "
256 "failed:\n numvunits (%d) > nb_blocks "
257 "(%d) - nb_boot_blocks(%d) - 2\n",
258 inftl->numvunits, inftl->nb_blocks,
259 inftl->nb_boot_blocks);
260 return -1;
261 }
262
263 inftl->mbd.size = inftl->numvunits *
264 (inftl->EraseSize / SECTORSIZE);
265
266
267
268
269
270 inftl->firstEUN = ip->firstUnit;
271 inftl->lastEUN = ip->lastUnit;
272 inftl->nb_blocks = ip->lastUnit + 1;
273
274
275 inftl->PUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
276 if (!inftl->PUtable) {
277 printk(KERN_WARNING "INFTL: allocation of PUtable "
278 "failed (%zd bytes)\n",
279 inftl->nb_blocks * sizeof(u16));
280 return -ENOMEM;
281 }
282
283 inftl->VUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
284 if (!inftl->VUtable) {
285 kfree(inftl->PUtable);
286 printk(KERN_WARNING "INFTL: allocation of VUtable "
287 "failed (%zd bytes)\n",
288 inftl->nb_blocks * sizeof(u16));
289 return -ENOMEM;
290 }
291
292
293 for (i = 0; i < inftl->nb_boot_blocks; i++)
294 inftl->PUtable[i] = BLOCK_RESERVED;
295
296 for (; i < inftl->nb_blocks; i++)
297 inftl->PUtable[i] = BLOCK_NOTEXPLORED;
298
299
300 inftl->PUtable[block] = BLOCK_RESERVED;
301
302
303 for (i = 0; i < inftl->nb_blocks; i++) {
304 int physblock;
305
306
307 for (physblock = 0; physblock < inftl->EraseSize; physblock += inftl->mbd.mtd->erasesize) {
308 if (mtd_block_isbad(inftl->mbd.mtd,
309 i * inftl->EraseSize + physblock))
310 inftl->PUtable[i] = BLOCK_RESERVED;
311 }
312 }
313
314 inftl->MediaUnit = block;
315 return 0;
316 }
317
318
319 return -1;
320}
321
322static int memcmpb(void *a, int c, int n)
323{
324 int i;
325 for (i = 0; i < n; i++) {
326 if (c != ((unsigned char *)a)[i])
327 return 1;
328 }
329 return 0;
330}
331
332
333
334
335
336static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
337 int len, int check_oob)
338{
339 u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
340 struct mtd_info *mtd = inftl->mbd.mtd;
341 size_t retlen;
342 int i;
343
344 for (i = 0; i < len; i += SECTORSIZE) {
345 if (mtd_read(mtd, address, SECTORSIZE, &retlen, buf))
346 return -1;
347 if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
348 return -1;
349
350 if (check_oob) {
351 if(inftl_read_oob(mtd, address, mtd->oobsize,
352 &retlen, &buf[SECTORSIZE]) < 0)
353 return -1;
354 if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
355 return -1;
356 }
357 address += SECTORSIZE;
358 }
359
360 return 0;
361}
362
363
364
365
366
367
368
369
370
371
372int INFTL_formatblock(struct INFTLrecord *inftl, int block)
373{
374 size_t retlen;
375 struct inftl_unittail uci;
376 struct erase_info *instr = &inftl->instr;
377 struct mtd_info *mtd = inftl->mbd.mtd;
378 int physblock;
379
380 pr_debug("INFTL: INFTL_formatblock(inftl=%p,block=%d)\n", inftl, block);
381
382 memset(instr, 0, sizeof(struct erase_info));
383
384
385
386
387
388 instr->mtd = inftl->mbd.mtd;
389 instr->addr = block * inftl->EraseSize;
390 instr->len = inftl->mbd.mtd->erasesize;
391
392
393
394 for (physblock = 0; physblock < inftl->EraseSize;
395 physblock += instr->len, instr->addr += instr->len) {
396 mtd_erase(inftl->mbd.mtd, instr);
397
398 if (instr->state == MTD_ERASE_FAILED) {
399 printk(KERN_WARNING "INFTL: error while formatting block %d\n",
400 block);
401 goto fail;
402 }
403
404
405
406
407
408
409 if (check_free_sectors(inftl, instr->addr, instr->len, 1) != 0)
410 goto fail;
411 }
412
413 uci.EraseMark = cpu_to_le16(ERASE_MARK);
414 uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
415 uci.Reserved[0] = 0;
416 uci.Reserved[1] = 0;
417 uci.Reserved[2] = 0;
418 uci.Reserved[3] = 0;
419 instr->addr = block * inftl->EraseSize + SECTORSIZE * 2;
420 if (inftl_write_oob(mtd, instr->addr + 8, 8, &retlen, (char *)&uci) < 0)
421 goto fail;
422 return 0;
423fail:
424
425
426 mtd_block_markbad(inftl->mbd.mtd, instr->addr);
427 return -1;
428}
429
430
431
432
433
434
435
436
437
438static void format_chain(struct INFTLrecord *inftl, unsigned int first_block)
439{
440 unsigned int block = first_block, block1;
441
442 printk(KERN_WARNING "INFTL: formatting chain at block %d\n",
443 first_block);
444
445 for (;;) {
446 block1 = inftl->PUtable[block];
447
448 printk(KERN_WARNING "INFTL: formatting block %d\n", block);
449 if (INFTL_formatblock(inftl, block) < 0) {
450
451
452
453 inftl->PUtable[block] = BLOCK_RESERVED;
454 } else {
455 inftl->PUtable[block] = BLOCK_FREE;
456 }
457
458
459 block = block1;
460
461 if (block == BLOCK_NIL || block >= inftl->lastEUN)
462 break;
463 }
464}
465
466void INFTL_dumptables(struct INFTLrecord *s)
467{
468 int i;
469
470 pr_debug("-------------------------------------------"
471 "----------------------------------\n");
472
473 pr_debug("VUtable[%d] ->", s->nb_blocks);
474 for (i = 0; i < s->nb_blocks; i++) {
475 if ((i % 8) == 0)
476 pr_debug("\n%04x: ", i);
477 pr_debug("%04x ", s->VUtable[i]);
478 }
479
480 pr_debug("\n-------------------------------------------"
481 "----------------------------------\n");
482
483 pr_debug("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks);
484 for (i = 0; i <= s->lastEUN; i++) {
485 if ((i % 8) == 0)
486 pr_debug("\n%04x: ", i);
487 pr_debug("%04x ", s->PUtable[i]);
488 }
489
490 pr_debug("\n-------------------------------------------"
491 "----------------------------------\n");
492
493 pr_debug("INFTL ->\n"
494 " EraseSize = %d\n"
495 " h/s/c = %d/%d/%d\n"
496 " numvunits = %d\n"
497 " firstEUN = %d\n"
498 " lastEUN = %d\n"
499 " numfreeEUNs = %d\n"
500 " LastFreeEUN = %d\n"
501 " nb_blocks = %d\n"
502 " nb_boot_blocks = %d",
503 s->EraseSize, s->heads, s->sectors, s->cylinders,
504 s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs,
505 s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks);
506
507 pr_debug("\n-------------------------------------------"
508 "----------------------------------\n");
509}
510
511void INFTL_dumpVUchains(struct INFTLrecord *s)
512{
513 int logical, block, i;
514
515 pr_debug("-------------------------------------------"
516 "----------------------------------\n");
517
518 pr_debug("INFTL Virtual Unit Chains:\n");
519 for (logical = 0; logical < s->nb_blocks; logical++) {
520 block = s->VUtable[logical];
521 if (block > s->nb_blocks)
522 continue;
523 pr_debug(" LOGICAL %d --> %d ", logical, block);
524 for (i = 0; i < s->nb_blocks; i++) {
525 if (s->PUtable[block] == BLOCK_NIL)
526 break;
527 block = s->PUtable[block];
528 pr_debug("%d ", block);
529 }
530 pr_debug("\n");
531 }
532
533 pr_debug("-------------------------------------------"
534 "----------------------------------\n");
535}
536
537int INFTL_mount(struct INFTLrecord *s)
538{
539 struct mtd_info *mtd = s->mbd.mtd;
540 unsigned int block, first_block, prev_block, last_block;
541 unsigned int first_logical_block, logical_block, erase_mark;
542 int chain_length, do_format_chain;
543 struct inftl_unithead1 h0;
544 struct inftl_unittail h1;
545 size_t retlen;
546 int i;
547 u8 *ANACtable, ANAC;
548
549 pr_debug("INFTL: INFTL_mount(inftl=%p)\n", s);
550
551
552 if (find_boot_record(s) < 0) {
553 printk(KERN_WARNING "INFTL: could not find valid boot record?\n");
554 return -ENXIO;
555 }
556
557
558 for (i = 0; i < s->nb_blocks; i++)
559 s->VUtable[i] = BLOCK_NIL;
560
561 logical_block = block = BLOCK_NIL;
562
563
564 ANACtable = kcalloc(s->nb_blocks, sizeof(u8), GFP_KERNEL);
565 if (!ANACtable) {
566 printk(KERN_WARNING "INFTL: allocation of ANACtable "
567 "failed (%zd bytes)\n",
568 s->nb_blocks * sizeof(u8));
569 return -ENOMEM;
570 }
571
572
573
574
575
576
577
578
579 pr_debug("INFTL: pass 1, explore each unit\n");
580 for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) {
581 if (s->PUtable[first_block] != BLOCK_NOTEXPLORED)
582 continue;
583
584 do_format_chain = 0;
585 first_logical_block = BLOCK_NIL;
586 last_block = BLOCK_NIL;
587 block = first_block;
588
589 for (chain_length = 0; ; chain_length++) {
590
591 if ((chain_length == 0) &&
592 (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
593
594 break;
595 }
596
597 if (inftl_read_oob(mtd, block * s->EraseSize + 8,
598 8, &retlen, (char *)&h0) < 0 ||
599 inftl_read_oob(mtd, block * s->EraseSize +
600 2 * SECTORSIZE + 8, 8, &retlen,
601 (char *)&h1) < 0) {
602
603 do_format_chain++;
604 break;
605 }
606
607 logical_block = le16_to_cpu(h0.virtualUnitNo);
608 prev_block = le16_to_cpu(h0.prevUnitNo);
609 erase_mark = le16_to_cpu((h1.EraseMark | h1.EraseMark1));
610 ANACtable[block] = h0.ANAC;
611
612
613 if (prev_block < s->nb_blocks)
614 prev_block += s->firstEUN;
615
616
617 if (s->PUtable[block] != BLOCK_NOTEXPLORED) {
618
619 if (logical_block == first_logical_block) {
620 if (last_block != BLOCK_NIL)
621 s->PUtable[last_block] = block;
622 }
623 break;
624 }
625
626
627 if (erase_mark != ERASE_MARK) {
628 printk(KERN_WARNING "INFTL: corrupt block %d "
629 "in chain %d, chain length %d, erase "
630 "mark 0x%x?\n", block, first_block,
631 chain_length, erase_mark);
632
633
634
635
636 if (chain_length == 0)
637 do_format_chain++;
638 break;
639 }
640
641
642 if ((logical_block == BLOCK_FREE) ||
643 (logical_block == BLOCK_NIL)) {
644 s->PUtable[block] = BLOCK_FREE;
645 break;
646 }
647
648
649 if ((logical_block >= s->nb_blocks) ||
650 ((prev_block >= s->nb_blocks) &&
651 (prev_block != BLOCK_NIL))) {
652 if (chain_length > 0) {
653 printk(KERN_WARNING "INFTL: corrupt "
654 "block %d in chain %d?\n",
655 block, first_block);
656 do_format_chain++;
657 }
658 break;
659 }
660
661 if (first_logical_block == BLOCK_NIL) {
662 first_logical_block = logical_block;
663 } else {
664 if (first_logical_block != logical_block) {
665
666 break;
667 }
668 }
669
670
671
672
673
674
675
676 s->PUtable[block] = BLOCK_NIL;
677 if (last_block != BLOCK_NIL)
678 s->PUtable[last_block] = block;
679 last_block = block;
680 block = prev_block;
681
682
683 if (block == BLOCK_NIL)
684 break;
685
686
687 if (block > s->lastEUN) {
688 printk(KERN_WARNING "INFTL: invalid previous "
689 "block %d in chain %d?\n", block,
690 first_block);
691 do_format_chain++;
692 break;
693 }
694 }
695
696 if (do_format_chain) {
697 format_chain(s, first_block);
698 continue;
699 }
700
701
702
703
704
705
706
707 s->VUtable[first_logical_block] = first_block;
708 logical_block = BLOCK_NIL;
709 }
710
711 INFTL_dumptables(s);
712
713
714
715
716
717
718 pr_debug("INFTL: pass 2, validate virtual chains\n");
719 for (logical_block = 0; logical_block < s->numvunits; logical_block++) {
720 block = s->VUtable[logical_block];
721 last_block = BLOCK_NIL;
722
723
724 if (block >= BLOCK_RESERVED)
725 continue;
726
727 ANAC = ANACtable[block];
728 for (i = 0; i < s->numvunits; i++) {
729 if (s->PUtable[block] == BLOCK_NIL)
730 break;
731 if (s->PUtable[block] > s->lastEUN) {
732 printk(KERN_WARNING "INFTL: invalid prev %d, "
733 "in virtual chain %d\n",
734 s->PUtable[block], logical_block);
735 s->PUtable[block] = BLOCK_NIL;
736
737 }
738 if (ANACtable[block] != ANAC) {
739
740
741
742
743
744 s->VUtable[logical_block] = block;
745 s->PUtable[last_block] = BLOCK_NIL;
746 break;
747 }
748
749 ANAC--;
750 last_block = block;
751 block = s->PUtable[block];
752 }
753
754 if (i >= s->nb_blocks) {
755
756
757
758
759 format_chain(s, first_block);
760 }
761 }
762
763 INFTL_dumptables(s);
764 INFTL_dumpVUchains(s);
765
766
767
768
769 s->numfreeEUNs = 0;
770 s->LastFreeEUN = BLOCK_NIL;
771
772 pr_debug("INFTL: pass 3, format unused blocks\n");
773 for (block = s->firstEUN; block <= s->lastEUN; block++) {
774 if (s->PUtable[block] == BLOCK_NOTEXPLORED) {
775 printk("INFTL: unreferenced block %d, formatting it\n",
776 block);
777 if (INFTL_formatblock(s, block) < 0)
778 s->PUtable[block] = BLOCK_RESERVED;
779 else
780 s->PUtable[block] = BLOCK_FREE;
781 }
782 if (s->PUtable[block] == BLOCK_FREE) {
783 s->numfreeEUNs++;
784 if (s->LastFreeEUN == BLOCK_NIL)
785 s->LastFreeEUN = block;
786 }
787 }
788
789 kfree(ANACtable);
790 return 0;
791}
792