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. struct mtd_info *mtd = inftl->mbd.mtd;
  316. size_t retlen;
  317. int i;
  318. for (i = 0; i < len; i += SECTORSIZE) {
  319. if (mtd->read(mtd, address, SECTORSIZE, &retlen, buf))
  320. return -1;
  321. if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
  322. return -1;
  323. if (check_oob) {
  324. if(mtd->read_oob(mtd, address, mtd->oobsize,
  325. &retlen, &buf[SECTORSIZE]) < 0)
  326. return -1;
  327. if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
  328. return -1;
  329. }
  330. address += SECTORSIZE;
  331. }
  332. return 0;
  333. }
  334. /*
  335. * INFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase
  336. * Unit and Update INFTL metadata. Each erase operation is
  337. * checked with check_free_sectors.
  338. *
  339. * Return: 0 when succeed, -1 on error.
  340. *
  341. * ToDo: 1. Is it neceressary to check_free_sector after erasing ??
  342. */
  343. int INFTL_formatblock(struct INFTLrecord *inftl, int block)
  344. {
  345. size_t retlen;
  346. struct inftl_unittail uci;
  347. struct erase_info *instr = &inftl->instr;
  348. int physblock;
  349. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_formatblock(inftl=%p,"
  350. "block=%d)\n", inftl, block);
  351. memset(instr, 0, sizeof(struct erase_info));
  352. /* FIXME: Shouldn't we be setting the 'discarded' flag to zero
  353. _first_? */
  354. /* Use async erase interface, test return code */
  355. instr->mtd = inftl->mbd.mtd;
  356. instr->addr = block * inftl->EraseSize;
  357. instr->len = inftl->mbd.mtd->erasesize;
  358. /* Erase one physical eraseblock at a time, even though the NAND api
  359. allows us to group them. This way we if we have a failure, we can
  360. mark only the failed block in the bbt. */
  361. for (physblock = 0; physblock < inftl->EraseSize; physblock += instr->len, instr->addr += instr->len) {
  362. MTD_ERASE(inftl->mbd.mtd, instr);
  363. if (instr->state == MTD_ERASE_FAILED) {
  364. printk(KERN_WARNING "INFTL: error while formatting block %d\n",
  365. block);
  366. goto fail;
  367. }
  368. /*
  369. * Check the "freeness" of Erase Unit before updating metadata.
  370. * FixMe: is this check really necessary? Since we have check the
  371. * return code after the erase operation.
  372. */
  373. if (check_free_sectors(inftl, instr->addr, instr->len, 1) != 0)
  374. goto fail;
  375. }
  376. uci.EraseMark = cpu_to_le16(ERASE_MARK);
  377. uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
  378. uci.Reserved[0] = 0;
  379. uci.Reserved[1] = 0;
  380. uci.Reserved[2] = 0;
  381. uci.Reserved[3] = 0;
  382. instr->addr = block * inftl->EraseSize + SECTORSIZE * 2;
  383. if (MTD_WRITEOOB(inftl->mbd.mtd, instr->addr +
  384. 8, 8, &retlen, (char *)&uci) < 0)
  385. goto fail;
  386. return 0;
  387. fail:
  388. /* could not format, update the bad block table (caller is responsible
  389. for setting the PUtable to BLOCK_RESERVED on failure) */
  390. inftl->mbd.mtd->block_markbad(inftl->mbd.mtd, instr->addr);
  391. return -1;
  392. }
  393. /*
  394. * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase
  395. * Units in a Virtual Unit Chain, i.e. all the units are disconnected.
  396. *
  397. * Since the chain is invalid then we will have to erase it from its
  398. * head (normally for INFTL we go from the oldest). But if it has a
  399. * loop then there is no oldest...
  400. */
  401. static void format_chain(struct INFTLrecord *inftl, unsigned int first_block)
  402. {
  403. unsigned int block = first_block, block1;
  404. printk(KERN_WARNING "INFTL: formatting chain at block %d\n",
  405. first_block);
  406. for (;;) {
  407. block1 = inftl->PUtable[block];
  408. printk(KERN_WARNING "INFTL: formatting block %d\n", block);
  409. if (INFTL_formatblock(inftl, block) < 0) {
  410. /*
  411. * Cannot format !!!! Mark it as Bad Unit,
  412. */
  413. inftl->PUtable[block] = BLOCK_RESERVED;
  414. } else {
  415. inftl->PUtable[block] = BLOCK_FREE;
  416. }
  417. /* Goto next block on the chain */
  418. block = block1;
  419. if (block == BLOCK_NIL || block >= inftl->lastEUN)
  420. break;
  421. }
  422. }
  423. void INFTL_dumptables(struct INFTLrecord *s)
  424. {
  425. int i;
  426. printk("-------------------------------------------"
  427. "----------------------------------\n");
  428. printk("VUtable[%d] ->", s->nb_blocks);
  429. for (i = 0; i < s->nb_blocks; i++) {
  430. if ((i % 8) == 0)
  431. printk("\n%04x: ", i);
  432. printk("%04x ", s->VUtable[i]);
  433. }
  434. printk("\n-------------------------------------------"
  435. "----------------------------------\n");
  436. printk("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks);
  437. for (i = 0; i <= s->lastEUN; i++) {
  438. if ((i % 8) == 0)
  439. printk("\n%04x: ", i);
  440. printk("%04x ", s->PUtable[i]);
  441. }
  442. printk("\n-------------------------------------------"
  443. "----------------------------------\n");
  444. printk("INFTL ->\n"
  445. " EraseSize = %d\n"
  446. " h/s/c = %d/%d/%d\n"
  447. " numvunits = %d\n"
  448. " firstEUN = %d\n"
  449. " lastEUN = %d\n"
  450. " numfreeEUNs = %d\n"
  451. " LastFreeEUN = %d\n"
  452. " nb_blocks = %d\n"
  453. " nb_boot_blocks = %d",
  454. s->EraseSize, s->heads, s->sectors, s->cylinders,
  455. s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs,
  456. s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks);
  457. printk("\n-------------------------------------------"
  458. "----------------------------------\n");
  459. }
  460. void INFTL_dumpVUchains(struct INFTLrecord *s)
  461. {
  462. int logical, block, i;
  463. printk("-------------------------------------------"
  464. "----------------------------------\n");
  465. printk("INFTL Virtual Unit Chains:\n");
  466. for (logical = 0; logical < s->nb_blocks; logical++) {
  467. block = s->VUtable[logical];
  468. if (block > s->nb_blocks)
  469. continue;
  470. printk(" LOGICAL %d --> %d ", logical, block);
  471. for (i = 0; i < s->nb_blocks; i++) {
  472. if (s->PUtable[block] == BLOCK_NIL)
  473. break;
  474. block = s->PUtable[block];
  475. printk("%d ", block);
  476. }
  477. printk("\n");
  478. }
  479. printk("-------------------------------------------"
  480. "----------------------------------\n");
  481. }
  482. int INFTL_mount(struct INFTLrecord *s)
  483. {
  484. unsigned int block, first_block, prev_block, last_block;
  485. unsigned int first_logical_block, logical_block, erase_mark;
  486. int chain_length, do_format_chain;
  487. struct inftl_unithead1 h0;
  488. struct inftl_unittail h1;
  489. size_t retlen;
  490. int i;
  491. u8 *ANACtable, ANAC;
  492. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_mount(inftl=%p)\n", s);
  493. /* Search for INFTL MediaHeader and Spare INFTL Media Header */
  494. if (find_boot_record(s) < 0) {
  495. printk(KERN_WARNING "INFTL: could not find valid boot record?\n");
  496. return -ENXIO;
  497. }
  498. /* Init the logical to physical table */
  499. for (i = 0; i < s->nb_blocks; i++)
  500. s->VUtable[i] = BLOCK_NIL;
  501. logical_block = block = BLOCK_NIL;
  502. /* Temporary buffer to store ANAC numbers. */
  503. ANACtable = kmalloc(s->nb_blocks * sizeof(u8), GFP_KERNEL);
  504. if (!ANACtable) {
  505. printk(KERN_WARNING "INFTL: allocation of ANACtable "
  506. "failed (%zd bytes)\n",
  507. s->nb_blocks * sizeof(u8));
  508. return -ENOMEM;
  509. }
  510. memset(ANACtable, 0, s->nb_blocks);
  511. /*
  512. * First pass is to explore each physical unit, and construct the
  513. * virtual chains that exist (newest physical unit goes into VUtable).
  514. * Any block that is in any way invalid will be left in the
  515. * NOTEXPLORED state. Then at the end we will try to format it and
  516. * mark it as free.
  517. */
  518. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 1, explore each unit\n");
  519. for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) {
  520. if (s->PUtable[first_block] != BLOCK_NOTEXPLORED)
  521. continue;
  522. do_format_chain = 0;
  523. first_logical_block = BLOCK_NIL;
  524. last_block = BLOCK_NIL;
  525. block = first_block;
  526. for (chain_length = 0; ; chain_length++) {
  527. if ((chain_length == 0) &&
  528. (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
  529. /* Nothing to do here, onto next block */
  530. break;
  531. }
  532. if (MTD_READOOB(s->mbd.mtd, block * s->EraseSize + 8,
  533. 8, &retlen, (char *)&h0) < 0 ||
  534. MTD_READOOB(s->mbd.mtd, block * s->EraseSize +
  535. 2 * SECTORSIZE + 8, 8, &retlen, (char *)&h1) < 0) {
  536. /* Should never happen? */
  537. do_format_chain++;
  538. break;
  539. }
  540. logical_block = le16_to_cpu(h0.virtualUnitNo);
  541. prev_block = le16_to_cpu(h0.prevUnitNo);
  542. erase_mark = le16_to_cpu((h1.EraseMark | h1.EraseMark1));
  543. ANACtable[block] = h0.ANAC;
  544. /* Previous block is relative to start of Partition */
  545. if (prev_block < s->nb_blocks)
  546. prev_block += s->firstEUN;
  547. /* Already explored partial chain? */
  548. if (s->PUtable[block] != BLOCK_NOTEXPLORED) {
  549. /* Check if chain for this logical */
  550. if (logical_block == first_logical_block) {
  551. if (last_block != BLOCK_NIL)
  552. s->PUtable[last_block] = block;
  553. }
  554. break;
  555. }
  556. /* Check for invalid block */
  557. if (erase_mark != ERASE_MARK) {
  558. printk(KERN_WARNING "INFTL: corrupt block %d "
  559. "in chain %d, chain length %d, erase "
  560. "mark 0x%x?\n", block, first_block,
  561. chain_length, erase_mark);
  562. /*
  563. * Assume end of chain, probably incomplete
  564. * fold/erase...
  565. */
  566. if (chain_length == 0)
  567. do_format_chain++;
  568. break;
  569. }
  570. /* Check for it being free already then... */
  571. if ((logical_block == BLOCK_FREE) ||
  572. (logical_block == BLOCK_NIL)) {
  573. s->PUtable[block] = BLOCK_FREE;
  574. break;
  575. }
  576. /* Sanity checks on block numbers */
  577. if ((logical_block >= s->nb_blocks) ||
  578. ((prev_block >= s->nb_blocks) &&
  579. (prev_block != BLOCK_NIL))) {
  580. if (chain_length > 0) {
  581. printk(KERN_WARNING "INFTL: corrupt "
  582. "block %d in chain %d?\n",
  583. block, first_block);
  584. do_format_chain++;
  585. }
  586. break;
  587. }
  588. if (first_logical_block == BLOCK_NIL) {
  589. first_logical_block = logical_block;
  590. } else {
  591. if (first_logical_block != logical_block) {
  592. /* Normal for folded chain... */
  593. break;
  594. }
  595. }
  596. /*
  597. * Current block is valid, so if we followed a virtual
  598. * chain to get here then we can set the previous
  599. * block pointer in our PUtable now. Then move onto
  600. * the previous block in the chain.
  601. */
  602. s->PUtable[block] = BLOCK_NIL;
  603. if (last_block != BLOCK_NIL)
  604. s->PUtable[last_block] = block;
  605. last_block = block;
  606. block = prev_block;
  607. /* Check for end of chain */
  608. if (block == BLOCK_NIL)
  609. break;
  610. /* Validate next block before following it... */
  611. if (block > s->lastEUN) {
  612. printk(KERN_WARNING "INFTL: invalid previous "
  613. "block %d in chain %d?\n", block,
  614. first_block);
  615. do_format_chain++;
  616. break;
  617. }
  618. }
  619. if (do_format_chain) {
  620. format_chain(s, first_block);
  621. continue;
  622. }
  623. /*
  624. * Looks like a valid chain then. It may not really be the
  625. * newest block in the chain, but it is the newest we have
  626. * found so far. We might update it in later iterations of
  627. * this loop if we find something newer.
  628. */
  629. s->VUtable[first_logical_block] = first_block;
  630. logical_block = BLOCK_NIL;
  631. }
  632. #ifdef CONFIG_MTD_DEBUG_VERBOSE
  633. if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
  634. INFTL_dumptables(s);
  635. #endif
  636. /*
  637. * Second pass, check for infinite loops in chains. These are
  638. * possible because we don't update the previous pointers when
  639. * we fold chains. No big deal, just fix them up in PUtable.
  640. */
  641. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 2, validate virtual chains\n");
  642. for (logical_block = 0; logical_block < s->numvunits; logical_block++) {
  643. block = s->VUtable[logical_block];
  644. last_block = BLOCK_NIL;
  645. /* Check for free/reserved/nil */
  646. if (block >= BLOCK_RESERVED)
  647. continue;
  648. ANAC = ANACtable[block];
  649. for (i = 0; i < s->numvunits; i++) {
  650. if (s->PUtable[block] == BLOCK_NIL)
  651. break;
  652. if (s->PUtable[block] > s->lastEUN) {
  653. printk(KERN_WARNING "INFTL: invalid prev %d, "
  654. "in virtual chain %d\n",
  655. s->PUtable[block], logical_block);
  656. s->PUtable[block] = BLOCK_NIL;
  657. }
  658. if (ANACtable[block] != ANAC) {
  659. /*
  660. * Chain must point back to itself. This is ok,
  661. * but we will need adjust the tables with this
  662. * newest block and oldest block.
  663. */
  664. s->VUtable[logical_block] = block;
  665. s->PUtable[last_block] = BLOCK_NIL;
  666. break;
  667. }
  668. ANAC--;
  669. last_block = block;
  670. block = s->PUtable[block];
  671. }
  672. if (i >= s->nb_blocks) {
  673. /*
  674. * Uhoo, infinite chain with valid ANACS!
  675. * Format whole chain...
  676. */
  677. format_chain(s, first_block);
  678. }
  679. }
  680. #ifdef CONFIG_MTD_DEBUG_VERBOSE
  681. if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
  682. INFTL_dumptables(s);
  683. if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
  684. INFTL_dumpVUchains(s);
  685. #endif
  686. /*
  687. * Third pass, format unreferenced blocks and init free block count.
  688. */
  689. s->numfreeEUNs = 0;
  690. s->LastFreeEUN = BLOCK_NIL;
  691. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 3, format unused blocks\n");
  692. for (block = s->firstEUN; block <= s->lastEUN; block++) {
  693. if (s->PUtable[block] == BLOCK_NOTEXPLORED) {
  694. printk("INFTL: unreferenced block %d, formatting it\n",
  695. block);
  696. if (INFTL_formatblock(s, block) < 0)
  697. s->PUtable[block] = BLOCK_RESERVED;
  698. else
  699. s->PUtable[block] = BLOCK_FREE;
  700. }
  701. if (s->PUtable[block] == BLOCK_FREE) {
  702. s->numfreeEUNs++;
  703. if (s->LastFreeEUN == BLOCK_NIL)
  704. s->LastFreeEUN = block;
  705. }
  706. }
  707. kfree(ANACtable);
  708. return 0;
  709. }