inftlmount.c 21 KB

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