inftlmount.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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 INFTLPartition *ip;
  58. size_t retlen;
  59. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: find_boot_record(inftl=%p)\n", inftl);
  60. /*
  61. * Assume logical EraseSize == physical erasesize for starting the
  62. * scan. We'll sort it out later if we find a MediaHeader which says
  63. * otherwise.
  64. */
  65. inftl->EraseSize = inftl->mbd.mtd->erasesize;
  66. inftl->nb_blocks = inftl->mbd.mtd->size / inftl->EraseSize;
  67. inftl->MediaUnit = BLOCK_NIL;
  68. /* Search for a valid boot record */
  69. for (block = 0; block < inftl->nb_blocks; block++) {
  70. int ret;
  71. /*
  72. * Check for BNAND header first. Then whinge if it's found
  73. * but later checks fail.
  74. */
  75. ret = MTD_READ(inftl->mbd.mtd, block * inftl->EraseSize,
  76. SECTORSIZE, &retlen, buf);
  77. /* We ignore ret in case the ECC of the MediaHeader is invalid
  78. (which is apparently acceptable) */
  79. if (retlen != SECTORSIZE) {
  80. static int warncount = 5;
  81. if (warncount) {
  82. printk(KERN_WARNING "INFTL: block read at 0x%x "
  83. "of mtd%d failed: %d\n",
  84. block * inftl->EraseSize,
  85. inftl->mbd.mtd->index, ret);
  86. if (!--warncount)
  87. printk(KERN_WARNING "INFTL: further "
  88. "failures for this block will "
  89. "not be printed\n");
  90. }
  91. continue;
  92. }
  93. if (retlen < 6 || memcmp(buf, "BNAND", 6)) {
  94. /* BNAND\0 not found. Continue */
  95. continue;
  96. }
  97. /* To be safer with BIOS, also use erase mark as discriminant */
  98. if ((ret = MTD_READOOB(inftl->mbd.mtd, block * inftl->EraseSize +
  99. SECTORSIZE + 8, 8, &retlen, (char *)&h1) < 0)) {
  100. printk(KERN_WARNING "INFTL: ANAND header found at "
  101. "0x%x in mtd%d, but OOB data read failed "
  102. "(err %d)\n", block * inftl->EraseSize,
  103. inftl->mbd.mtd->index, ret);
  104. continue;
  105. }
  106. /*
  107. * This is the first we've seen.
  108. * Copy the media header structure into place.
  109. */
  110. memcpy(mh, buf, sizeof(struct INFTLMediaHeader));
  111. /* Read the spare media header at offset 4096 */
  112. MTD_READ(inftl->mbd.mtd, block * inftl->EraseSize + 4096,
  113. SECTORSIZE, &retlen, buf);
  114. if (retlen != SECTORSIZE) {
  115. printk(KERN_WARNING "INFTL: Unable to read spare "
  116. "Media Header\n");
  117. return -1;
  118. }
  119. /* Check if this one is the same as the first one we found. */
  120. if (memcmp(mh, buf, sizeof(struct INFTLMediaHeader))) {
  121. printk(KERN_WARNING "INFTL: Primary and spare Media "
  122. "Headers disagree.\n");
  123. return -1;
  124. }
  125. mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
  126. mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
  127. mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
  128. mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
  129. mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
  130. mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
  131. #ifdef CONFIG_MTD_DEBUG_VERBOSE
  132. if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
  133. printk("INFTL: Media Header ->\n"
  134. " bootRecordID = %s\n"
  135. " NoOfBootImageBlocks = %d\n"
  136. " NoOfBinaryPartitions = %d\n"
  137. " NoOfBDTLPartitions = %d\n"
  138. " BlockMultiplerBits = %d\n"
  139. " FormatFlgs = %d\n"
  140. " OsakVersion = 0x%x\n"
  141. " PercentUsed = %d\n",
  142. mh->bootRecordID, mh->NoOfBootImageBlocks,
  143. mh->NoOfBinaryPartitions,
  144. mh->NoOfBDTLPartitions,
  145. mh->BlockMultiplierBits, mh->FormatFlags,
  146. mh->OsakVersion, mh->PercentUsed);
  147. }
  148. #endif
  149. if (mh->NoOfBDTLPartitions == 0) {
  150. printk(KERN_WARNING "INFTL: Media Header sanity check "
  151. "failed: NoOfBDTLPartitions (%d) == 0, "
  152. "must be at least 1\n", mh->NoOfBDTLPartitions);
  153. return -1;
  154. }
  155. if ((mh->NoOfBDTLPartitions + mh->NoOfBinaryPartitions) > 4) {
  156. printk(KERN_WARNING "INFTL: Media Header sanity check "
  157. "failed: Total Partitions (%d) > 4, "
  158. "BDTL=%d Binary=%d\n", mh->NoOfBDTLPartitions +
  159. mh->NoOfBinaryPartitions,
  160. mh->NoOfBDTLPartitions,
  161. mh->NoOfBinaryPartitions);
  162. return -1;
  163. }
  164. if (mh->BlockMultiplierBits > 1) {
  165. printk(KERN_WARNING "INFTL: sorry, we don't support "
  166. "UnitSizeFactor 0x%02x\n",
  167. mh->BlockMultiplierBits);
  168. return -1;
  169. } else if (mh->BlockMultiplierBits == 1) {
  170. printk(KERN_WARNING "INFTL: support for INFTL with "
  171. "UnitSizeFactor 0x%02x is experimental\n",
  172. mh->BlockMultiplierBits);
  173. inftl->EraseSize = inftl->mbd.mtd->erasesize <<
  174. mh->BlockMultiplierBits;
  175. inftl->nb_blocks = inftl->mbd.mtd->size / inftl->EraseSize;
  176. block >>= mh->BlockMultiplierBits;
  177. }
  178. /* Scan the partitions */
  179. for (i = 0; (i < 4); i++) {
  180. ip = &mh->Partitions[i];
  181. ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
  182. ip->firstUnit = le32_to_cpu(ip->firstUnit);
  183. ip->lastUnit = le32_to_cpu(ip->lastUnit);
  184. ip->flags = le32_to_cpu(ip->flags);
  185. ip->spareUnits = le32_to_cpu(ip->spareUnits);
  186. ip->Reserved0 = le32_to_cpu(ip->Reserved0);
  187. #ifdef CONFIG_MTD_DEBUG_VERBOSE
  188. if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
  189. printk(" PARTITION[%d] ->\n"
  190. " virtualUnits = %d\n"
  191. " firstUnit = %d\n"
  192. " lastUnit = %d\n"
  193. " flags = 0x%x\n"
  194. " spareUnits = %d\n",
  195. i, ip->virtualUnits, ip->firstUnit,
  196. ip->lastUnit, ip->flags,
  197. ip->spareUnits);
  198. }
  199. #endif
  200. if (ip->Reserved0 != ip->firstUnit) {
  201. struct erase_info *instr = &inftl->instr;
  202. instr->mtd = inftl->mbd.mtd;
  203. /*
  204. * Most likely this is using the
  205. * undocumented qiuck mount feature.
  206. * We don't support that, we will need
  207. * to erase the hidden block for full
  208. * compatibility.
  209. */
  210. instr->addr = ip->Reserved0 * inftl->EraseSize;
  211. instr->len = inftl->EraseSize;
  212. MTD_ERASE(inftl->mbd.mtd, instr);
  213. }
  214. if ((ip->lastUnit - ip->firstUnit + 1) < ip->virtualUnits) {
  215. printk(KERN_WARNING "INFTL: Media Header "
  216. "Partition %d sanity check failed\n"
  217. " firstUnit %d : lastUnit %d > "
  218. "virtualUnits %d\n", i, ip->lastUnit,
  219. ip->firstUnit, ip->Reserved0);
  220. return -1;
  221. }
  222. if (ip->Reserved1 != 0) {
  223. printk(KERN_WARNING "INFTL: Media Header "
  224. "Partition %d sanity check failed: "
  225. "Reserved1 %d != 0\n",
  226. i, ip->Reserved1);
  227. return -1;
  228. }
  229. if (ip->flags & INFTL_BDTL)
  230. break;
  231. }
  232. if (i >= 4) {
  233. printk(KERN_WARNING "INFTL: Media Header Partition "
  234. "sanity check failed:\n No partition "
  235. "marked as Disk Partition\n");
  236. return -1;
  237. }
  238. inftl->nb_boot_blocks = ip->firstUnit;
  239. inftl->numvunits = ip->virtualUnits;
  240. if (inftl->numvunits > (inftl->nb_blocks -
  241. inftl->nb_boot_blocks - 2)) {
  242. printk(KERN_WARNING "INFTL: Media Header sanity check "
  243. "failed:\n numvunits (%d) > nb_blocks "
  244. "(%d) - nb_boot_blocks(%d) - 2\n",
  245. inftl->numvunits, inftl->nb_blocks,
  246. inftl->nb_boot_blocks);
  247. return -1;
  248. }
  249. inftl->mbd.size = inftl->numvunits *
  250. (inftl->EraseSize / SECTORSIZE);
  251. /*
  252. * Block count is set to last used EUN (we won't need to keep
  253. * any meta-data past that point).
  254. */
  255. inftl->firstEUN = ip->firstUnit;
  256. inftl->lastEUN = ip->lastUnit;
  257. inftl->nb_blocks = ip->lastUnit + 1;
  258. /* Memory alloc */
  259. inftl->PUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
  260. if (!inftl->PUtable) {
  261. printk(KERN_WARNING "INFTL: allocation of PUtable "
  262. "failed (%zd bytes)\n",
  263. inftl->nb_blocks * sizeof(u16));
  264. return -ENOMEM;
  265. }
  266. inftl->VUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
  267. if (!inftl->VUtable) {
  268. kfree(inftl->PUtable);
  269. printk(KERN_WARNING "INFTL: allocation of VUtable "
  270. "failed (%zd bytes)\n",
  271. inftl->nb_blocks * sizeof(u16));
  272. return -ENOMEM;
  273. }
  274. /* Mark the blocks before INFTL MediaHeader as reserved */
  275. for (i = 0; i < inftl->nb_boot_blocks; i++)
  276. inftl->PUtable[i] = BLOCK_RESERVED;
  277. /* Mark all remaining blocks as potentially containing data */
  278. for (; i < inftl->nb_blocks; i++)
  279. inftl->PUtable[i] = BLOCK_NOTEXPLORED;
  280. /* Mark this boot record (NFTL MediaHeader) block as reserved */
  281. inftl->PUtable[block] = BLOCK_RESERVED;
  282. /* Read Bad Erase Unit Table and modify PUtable[] accordingly */
  283. for (i = 0; i < inftl->nb_blocks; i++) {
  284. int physblock;
  285. /* If any of the physical eraseblocks are bad, don't
  286. use the unit. */
  287. for (physblock = 0; physblock < inftl->EraseSize; physblock += inftl->mbd.mtd->erasesize) {
  288. if (inftl->mbd.mtd->block_isbad(inftl->mbd.mtd, i * inftl->EraseSize + physblock))
  289. inftl->PUtable[i] = BLOCK_RESERVED;
  290. }
  291. }
  292. inftl->MediaUnit = block;
  293. return 0;
  294. }
  295. /* Not found. */
  296. return -1;
  297. }
  298. static int memcmpb(void *a, int c, int n)
  299. {
  300. int i;
  301. for (i = 0; i < n; i++) {
  302. if (c != ((unsigned char *)a)[i])
  303. return 1;
  304. }
  305. return 0;
  306. }
  307. /*
  308. * check_free_sector: check if a free sector is actually FREE,
  309. * i.e. All 0xff in data and oob area.
  310. */
  311. static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
  312. int len, int check_oob)
  313. {
  314. u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
  315. size_t retlen;
  316. int i;
  317. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: check_free_sectors(inftl=%p,"
  318. "address=0x%x,len=%d,check_oob=%d)\n", inftl,
  319. address, len, check_oob);
  320. for (i = 0; i < len; i += SECTORSIZE) {
  321. if (MTD_READECC(inftl->mbd.mtd, address, SECTORSIZE, &retlen, buf, &buf[SECTORSIZE], &inftl->oobinfo) < 0)
  322. return -1;
  323. if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
  324. return -1;
  325. if (check_oob) {
  326. if (memcmpb(buf + SECTORSIZE, 0xff, inftl->mbd.mtd->oobsize) != 0)
  327. return -1;
  328. }
  329. address += SECTORSIZE;
  330. }
  331. return 0;
  332. }
  333. /*
  334. * INFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase
  335. * Unit and Update INFTL metadata. Each erase operation is
  336. * checked with check_free_sectors.
  337. *
  338. * Return: 0 when succeed, -1 on error.
  339. *
  340. * ToDo: 1. Is it neceressary to check_free_sector after erasing ??
  341. */
  342. int INFTL_formatblock(struct INFTLrecord *inftl, int block)
  343. {
  344. size_t retlen;
  345. struct inftl_unittail uci;
  346. struct erase_info *instr = &inftl->instr;
  347. int physblock;
  348. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_formatblock(inftl=%p,"
  349. "block=%d)\n", inftl, block);
  350. memset(instr, 0, sizeof(struct erase_info));
  351. /* FIXME: Shouldn't we be setting the 'discarded' flag to zero
  352. _first_? */
  353. /* Use async erase interface, test return code */
  354. instr->mtd = inftl->mbd.mtd;
  355. instr->addr = block * inftl->EraseSize;
  356. instr->len = inftl->mbd.mtd->erasesize;
  357. /* Erase one physical eraseblock at a time, even though the NAND api
  358. allows us to group them. This way we if we have a failure, we can
  359. mark only the failed block in the bbt. */
  360. for (physblock = 0; physblock < inftl->EraseSize; 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 the
  370. * 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 (MTD_WRITEOOB(inftl->mbd.mtd, instr->addr +
  383. 8, 8, &retlen, (char *)&uci) < 0)
  384. goto fail;
  385. return 0;
  386. fail:
  387. /* could not format, update the bad block table (caller is responsible
  388. for setting the PUtable to BLOCK_RESERVED on failure) */
  389. inftl->mbd.mtd->block_markbad(inftl->mbd.mtd, instr->addr);
  390. return -1;
  391. }
  392. /*
  393. * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase
  394. * Units in a Virtual Unit Chain, i.e. all the units are disconnected.
  395. *
  396. * Since the chain is invalid then we will have to erase it from its
  397. * head (normally for INFTL we go from the oldest). But if it has a
  398. * loop then there is no oldest...
  399. */
  400. static void format_chain(struct INFTLrecord *inftl, unsigned int first_block)
  401. {
  402. unsigned int block = first_block, block1;
  403. printk(KERN_WARNING "INFTL: formatting chain at block %d\n",
  404. first_block);
  405. for (;;) {
  406. block1 = inftl->PUtable[block];
  407. printk(KERN_WARNING "INFTL: formatting block %d\n", block);
  408. if (INFTL_formatblock(inftl, block) < 0) {
  409. /*
  410. * Cannot format !!!! Mark it as Bad Unit,
  411. */
  412. inftl->PUtable[block] = BLOCK_RESERVED;
  413. } else {
  414. inftl->PUtable[block] = BLOCK_FREE;
  415. }
  416. /* Goto next block on the chain */
  417. block = block1;
  418. if (block == BLOCK_NIL || block >= inftl->lastEUN)
  419. break;
  420. }
  421. }
  422. void INFTL_dumptables(struct INFTLrecord *s)
  423. {
  424. int i;
  425. printk("-------------------------------------------"
  426. "----------------------------------\n");
  427. printk("VUtable[%d] ->", s->nb_blocks);
  428. for (i = 0; i < s->nb_blocks; i++) {
  429. if ((i % 8) == 0)
  430. printk("\n%04x: ", i);
  431. printk("%04x ", s->VUtable[i]);
  432. }
  433. printk("\n-------------------------------------------"
  434. "----------------------------------\n");
  435. printk("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks);
  436. for (i = 0; i <= s->lastEUN; i++) {
  437. if ((i % 8) == 0)
  438. printk("\n%04x: ", i);
  439. printk("%04x ", s->PUtable[i]);
  440. }
  441. printk("\n-------------------------------------------"
  442. "----------------------------------\n");
  443. printk("INFTL ->\n"
  444. " EraseSize = %d\n"
  445. " h/s/c = %d/%d/%d\n"
  446. " numvunits = %d\n"
  447. " firstEUN = %d\n"
  448. " lastEUN = %d\n"
  449. " numfreeEUNs = %d\n"
  450. " LastFreeEUN = %d\n"
  451. " nb_blocks = %d\n"
  452. " nb_boot_blocks = %d",
  453. s->EraseSize, s->heads, s->sectors, s->cylinders,
  454. s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs,
  455. s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks);
  456. printk("\n-------------------------------------------"
  457. "----------------------------------\n");
  458. }
  459. void INFTL_dumpVUchains(struct INFTLrecord *s)
  460. {
  461. int logical, block, i;
  462. printk("-------------------------------------------"
  463. "----------------------------------\n");
  464. printk("INFTL Virtual Unit Chains:\n");
  465. for (logical = 0; logical < s->nb_blocks; logical++) {
  466. block = s->VUtable[logical];
  467. if (block > s->nb_blocks)
  468. continue;
  469. printk(" LOGICAL %d --> %d ", logical, block);
  470. for (i = 0; i < s->nb_blocks; i++) {
  471. if (s->PUtable[block] == BLOCK_NIL)
  472. break;
  473. block = s->PUtable[block];
  474. printk("%d ", block);
  475. }
  476. printk("\n");
  477. }
  478. printk("-------------------------------------------"
  479. "----------------------------------\n");
  480. }
  481. int INFTL_mount(struct INFTLrecord *s)
  482. {
  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 = kmalloc(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. memset(ANACtable, 0, s->nb_blocks);
  510. /*
  511. * First pass is to explore each physical unit, and construct the
  512. * virtual chains that exist (newest physical unit goes into VUtable).
  513. * Any block that is in any way invalid will be left in the
  514. * NOTEXPLORED state. Then at the end we will try to format it and
  515. * mark it as free.
  516. */
  517. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 1, explore each unit\n");
  518. for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) {
  519. if (s->PUtable[first_block] != BLOCK_NOTEXPLORED)
  520. continue;
  521. do_format_chain = 0;
  522. first_logical_block = BLOCK_NIL;
  523. last_block = BLOCK_NIL;
  524. block = first_block;
  525. for (chain_length = 0; ; chain_length++) {
  526. if ((chain_length == 0) &&
  527. (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
  528. /* Nothing to do here, onto next block */
  529. break;
  530. }
  531. if (MTD_READOOB(s->mbd.mtd, block * s->EraseSize + 8,
  532. 8, &retlen, (char *)&h0) < 0 ||
  533. MTD_READOOB(s->mbd.mtd, block * s->EraseSize +
  534. 2 * SECTORSIZE + 8, 8, &retlen, (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. }