inftlmount.c 23 KB

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