inftlmount.c 23 KB

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