inftlmount.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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/delay.h>
  34. #include <linux/slab.h>
  35. #include <linux/init.h>
  36. #include <linux/mtd/mtd.h>
  37. #include <linux/mtd/nftl.h>
  38. #include <linux/mtd/inftl.h>
  39. #include <linux/mtd/compatmac.h>
  40. char inftlmountrev[]="$Revision: 1.18 $";
  41. /*
  42. * find_boot_record: Find the INFTL Media Header and its Spare copy which
  43. * contains the various device information of the INFTL partition and
  44. * Bad Unit Table. Update the PUtable[] table according to the Bad
  45. * Unit Table. PUtable[] is used for management of Erase Unit in
  46. * other routines in inftlcore.c and inftlmount.c.
  47. */
  48. static int find_boot_record(struct INFTLrecord *inftl)
  49. {
  50. struct inftl_unittail h1;
  51. //struct inftl_oob oob;
  52. unsigned int i, block;
  53. u8 buf[SECTORSIZE];
  54. struct INFTLMediaHeader *mh = &inftl->MediaHdr;
  55. struct mtd_info *mtd = inftl->mbd.mtd;
  56. struct INFTLPartition *ip;
  57. size_t retlen;
  58. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: find_boot_record(inftl=%p)\n", inftl);
  59. /*
  60. * Assume logical EraseSize == physical erasesize for starting the
  61. * scan. We'll sort it out later if we find a MediaHeader which says
  62. * otherwise.
  63. */
  64. inftl->EraseSize = inftl->mbd.mtd->erasesize;
  65. inftl->nb_blocks = inftl->mbd.mtd->size / inftl->EraseSize;
  66. inftl->MediaUnit = BLOCK_NIL;
  67. /* Search for a valid boot record */
  68. for (block = 0; block < inftl->nb_blocks; block++) {
  69. int ret;
  70. /*
  71. * Check for BNAND header first. Then whinge if it's found
  72. * but later checks fail.
  73. */
  74. ret = mtd->read(mtd, block * inftl->EraseSize,
  75. SECTORSIZE, &retlen, buf);
  76. /* We ignore ret in case the ECC of the MediaHeader is invalid
  77. (which is apparently acceptable) */
  78. if (retlen != SECTORSIZE) {
  79. static int warncount = 5;
  80. if (warncount) {
  81. printk(KERN_WARNING "INFTL: block read at 0x%x "
  82. "of mtd%d failed: %d\n",
  83. block * inftl->EraseSize,
  84. inftl->mbd.mtd->index, ret);
  85. if (!--warncount)
  86. printk(KERN_WARNING "INFTL: further "
  87. "failures for this block will "
  88. "not be printed\n");
  89. }
  90. continue;
  91. }
  92. if (retlen < 6 || memcmp(buf, "BNAND", 6)) {
  93. /* BNAND\0 not found. Continue */
  94. continue;
  95. }
  96. /* To be safer with BIOS, also use erase mark as discriminant */
  97. if ((ret = inftl_read_oob(mtd, block * inftl->EraseSize +
  98. SECTORSIZE + 8, 8, &retlen,
  99. (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(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(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(inftl_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. struct mtd_info *mtd = inftl->mbd.mtd;
  349. int physblock;
  350. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_formatblock(inftl=%p,"
  351. "block=%d)\n", inftl, block);
  352. memset(instr, 0, sizeof(struct erase_info));
  353. /* FIXME: Shouldn't we be setting the 'discarded' flag to zero
  354. _first_? */
  355. /* Use async erase interface, test return code */
  356. instr->mtd = inftl->mbd.mtd;
  357. instr->addr = block * inftl->EraseSize;
  358. instr->len = inftl->mbd.mtd->erasesize;
  359. /* Erase one physical eraseblock at a time, even though the NAND api
  360. allows us to group them. This way we if we have a failure, we can
  361. mark only the failed block in the bbt. */
  362. for (physblock = 0; physblock < inftl->EraseSize;
  363. physblock += instr->len, instr->addr += instr->len) {
  364. mtd->erase(inftl->mbd.mtd, instr);
  365. if (instr->state == MTD_ERASE_FAILED) {
  366. printk(KERN_WARNING "INFTL: error while formatting block %d\n",
  367. block);
  368. goto fail;
  369. }
  370. /*
  371. * Check the "freeness" of Erase Unit before updating metadata.
  372. * FixMe: is this check really necessary? Since we have check
  373. * the return code after the erase operation.
  374. */
  375. if (check_free_sectors(inftl, instr->addr, instr->len, 1) != 0)
  376. goto fail;
  377. }
  378. uci.EraseMark = cpu_to_le16(ERASE_MARK);
  379. uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
  380. uci.Reserved[0] = 0;
  381. uci.Reserved[1] = 0;
  382. uci.Reserved[2] = 0;
  383. uci.Reserved[3] = 0;
  384. instr->addr = block * inftl->EraseSize + SECTORSIZE * 2;
  385. if (inftl_write_oob(mtd, instr->addr + 8, 8, &retlen, (char *)&uci) < 0)
  386. goto fail;
  387. return 0;
  388. fail:
  389. /* could not format, update the bad block table (caller is responsible
  390. for setting the PUtable to BLOCK_RESERVED on failure) */
  391. inftl->mbd.mtd->block_markbad(inftl->mbd.mtd, instr->addr);
  392. return -1;
  393. }
  394. /*
  395. * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase
  396. * Units in a Virtual Unit Chain, i.e. all the units are disconnected.
  397. *
  398. * Since the chain is invalid then we will have to erase it from its
  399. * head (normally for INFTL we go from the oldest). But if it has a
  400. * loop then there is no oldest...
  401. */
  402. static void format_chain(struct INFTLrecord *inftl, unsigned int first_block)
  403. {
  404. unsigned int block = first_block, block1;
  405. printk(KERN_WARNING "INFTL: formatting chain at block %d\n",
  406. first_block);
  407. for (;;) {
  408. block1 = inftl->PUtable[block];
  409. printk(KERN_WARNING "INFTL: formatting block %d\n", block);
  410. if (INFTL_formatblock(inftl, block) < 0) {
  411. /*
  412. * Cannot format !!!! Mark it as Bad Unit,
  413. */
  414. inftl->PUtable[block] = BLOCK_RESERVED;
  415. } else {
  416. inftl->PUtable[block] = BLOCK_FREE;
  417. }
  418. /* Goto next block on the chain */
  419. block = block1;
  420. if (block == BLOCK_NIL || block >= inftl->lastEUN)
  421. break;
  422. }
  423. }
  424. void INFTL_dumptables(struct INFTLrecord *s)
  425. {
  426. int i;
  427. printk("-------------------------------------------"
  428. "----------------------------------\n");
  429. printk("VUtable[%d] ->", s->nb_blocks);
  430. for (i = 0; i < s->nb_blocks; i++) {
  431. if ((i % 8) == 0)
  432. printk("\n%04x: ", i);
  433. printk("%04x ", s->VUtable[i]);
  434. }
  435. printk("\n-------------------------------------------"
  436. "----------------------------------\n");
  437. printk("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks);
  438. for (i = 0; i <= s->lastEUN; i++) {
  439. if ((i % 8) == 0)
  440. printk("\n%04x: ", i);
  441. printk("%04x ", s->PUtable[i]);
  442. }
  443. printk("\n-------------------------------------------"
  444. "----------------------------------\n");
  445. printk("INFTL ->\n"
  446. " EraseSize = %d\n"
  447. " h/s/c = %d/%d/%d\n"
  448. " numvunits = %d\n"
  449. " firstEUN = %d\n"
  450. " lastEUN = %d\n"
  451. " numfreeEUNs = %d\n"
  452. " LastFreeEUN = %d\n"
  453. " nb_blocks = %d\n"
  454. " nb_boot_blocks = %d",
  455. s->EraseSize, s->heads, s->sectors, s->cylinders,
  456. s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs,
  457. s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks);
  458. printk("\n-------------------------------------------"
  459. "----------------------------------\n");
  460. }
  461. void INFTL_dumpVUchains(struct INFTLrecord *s)
  462. {
  463. int logical, block, i;
  464. printk("-------------------------------------------"
  465. "----------------------------------\n");
  466. printk("INFTL Virtual Unit Chains:\n");
  467. for (logical = 0; logical < s->nb_blocks; logical++) {
  468. block = s->VUtable[logical];
  469. if (block > s->nb_blocks)
  470. continue;
  471. printk(" LOGICAL %d --> %d ", logical, block);
  472. for (i = 0; i < s->nb_blocks; i++) {
  473. if (s->PUtable[block] == BLOCK_NIL)
  474. break;
  475. block = s->PUtable[block];
  476. printk("%d ", block);
  477. }
  478. printk("\n");
  479. }
  480. printk("-------------------------------------------"
  481. "----------------------------------\n");
  482. }
  483. int INFTL_mount(struct INFTLrecord *s)
  484. {
  485. struct mtd_info *mtd = s->mbd.mtd;
  486. unsigned int block, first_block, prev_block, last_block;
  487. unsigned int first_logical_block, logical_block, erase_mark;
  488. int chain_length, do_format_chain;
  489. struct inftl_unithead1 h0;
  490. struct inftl_unittail h1;
  491. size_t retlen;
  492. int i;
  493. u8 *ANACtable, ANAC;
  494. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_mount(inftl=%p)\n", s);
  495. /* Search for INFTL MediaHeader and Spare INFTL Media Header */
  496. if (find_boot_record(s) < 0) {
  497. printk(KERN_WARNING "INFTL: could not find valid boot record?\n");
  498. return -ENXIO;
  499. }
  500. /* Init the logical to physical table */
  501. for (i = 0; i < s->nb_blocks; i++)
  502. s->VUtable[i] = BLOCK_NIL;
  503. logical_block = block = BLOCK_NIL;
  504. /* Temporary buffer to store ANAC numbers. */
  505. ANACtable = kcalloc(s->nb_blocks, sizeof(u8), GFP_KERNEL);
  506. if (!ANACtable) {
  507. printk(KERN_WARNING "INFTL: allocation of ANACtable "
  508. "failed (%zd bytes)\n",
  509. s->nb_blocks * sizeof(u8));
  510. return -ENOMEM;
  511. }
  512. /*
  513. * First pass is to explore each physical unit, and construct the
  514. * virtual chains that exist (newest physical unit goes into VUtable).
  515. * Any block that is in any way invalid will be left in the
  516. * NOTEXPLORED state. Then at the end we will try to format it and
  517. * mark it as free.
  518. */
  519. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 1, explore each unit\n");
  520. for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) {
  521. if (s->PUtable[first_block] != BLOCK_NOTEXPLORED)
  522. continue;
  523. do_format_chain = 0;
  524. first_logical_block = BLOCK_NIL;
  525. last_block = BLOCK_NIL;
  526. block = first_block;
  527. for (chain_length = 0; ; chain_length++) {
  528. if ((chain_length == 0) &&
  529. (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
  530. /* Nothing to do here, onto next block */
  531. break;
  532. }
  533. if (inftl_read_oob(mtd, block * s->EraseSize + 8,
  534. 8, &retlen, (char *)&h0) < 0 ||
  535. inftl_read_oob(mtd, block * s->EraseSize +
  536. 2 * SECTORSIZE + 8, 8, &retlen,
  537. (char *)&h1) < 0) {
  538. /* Should never happen? */
  539. do_format_chain++;
  540. break;
  541. }
  542. logical_block = le16_to_cpu(h0.virtualUnitNo);
  543. prev_block = le16_to_cpu(h0.prevUnitNo);
  544. erase_mark = le16_to_cpu((h1.EraseMark | h1.EraseMark1));
  545. ANACtable[block] = h0.ANAC;
  546. /* Previous block is relative to start of Partition */
  547. if (prev_block < s->nb_blocks)
  548. prev_block += s->firstEUN;
  549. /* Already explored partial chain? */
  550. if (s->PUtable[block] != BLOCK_NOTEXPLORED) {
  551. /* Check if chain for this logical */
  552. if (logical_block == first_logical_block) {
  553. if (last_block != BLOCK_NIL)
  554. s->PUtable[last_block] = block;
  555. }
  556. break;
  557. }
  558. /* Check for invalid block */
  559. if (erase_mark != ERASE_MARK) {
  560. printk(KERN_WARNING "INFTL: corrupt block %d "
  561. "in chain %d, chain length %d, erase "
  562. "mark 0x%x?\n", block, first_block,
  563. chain_length, erase_mark);
  564. /*
  565. * Assume end of chain, probably incomplete
  566. * fold/erase...
  567. */
  568. if (chain_length == 0)
  569. do_format_chain++;
  570. break;
  571. }
  572. /* Check for it being free already then... */
  573. if ((logical_block == BLOCK_FREE) ||
  574. (logical_block == BLOCK_NIL)) {
  575. s->PUtable[block] = BLOCK_FREE;
  576. break;
  577. }
  578. /* Sanity checks on block numbers */
  579. if ((logical_block >= s->nb_blocks) ||
  580. ((prev_block >= s->nb_blocks) &&
  581. (prev_block != BLOCK_NIL))) {
  582. if (chain_length > 0) {
  583. printk(KERN_WARNING "INFTL: corrupt "
  584. "block %d in chain %d?\n",
  585. block, first_block);
  586. do_format_chain++;
  587. }
  588. break;
  589. }
  590. if (first_logical_block == BLOCK_NIL) {
  591. first_logical_block = logical_block;
  592. } else {
  593. if (first_logical_block != logical_block) {
  594. /* Normal for folded chain... */
  595. break;
  596. }
  597. }
  598. /*
  599. * Current block is valid, so if we followed a virtual
  600. * chain to get here then we can set the previous
  601. * block pointer in our PUtable now. Then move onto
  602. * the previous block in the chain.
  603. */
  604. s->PUtable[block] = BLOCK_NIL;
  605. if (last_block != BLOCK_NIL)
  606. s->PUtable[last_block] = block;
  607. last_block = block;
  608. block = prev_block;
  609. /* Check for end of chain */
  610. if (block == BLOCK_NIL)
  611. break;
  612. /* Validate next block before following it... */
  613. if (block > s->lastEUN) {
  614. printk(KERN_WARNING "INFTL: invalid previous "
  615. "block %d in chain %d?\n", block,
  616. first_block);
  617. do_format_chain++;
  618. break;
  619. }
  620. }
  621. if (do_format_chain) {
  622. format_chain(s, first_block);
  623. continue;
  624. }
  625. /*
  626. * Looks like a valid chain then. It may not really be the
  627. * newest block in the chain, but it is the newest we have
  628. * found so far. We might update it in later iterations of
  629. * this loop if we find something newer.
  630. */
  631. s->VUtable[first_logical_block] = first_block;
  632. logical_block = BLOCK_NIL;
  633. }
  634. #ifdef CONFIG_MTD_DEBUG_VERBOSE
  635. if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
  636. INFTL_dumptables(s);
  637. #endif
  638. /*
  639. * Second pass, check for infinite loops in chains. These are
  640. * possible because we don't update the previous pointers when
  641. * we fold chains. No big deal, just fix them up in PUtable.
  642. */
  643. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 2, validate virtual chains\n");
  644. for (logical_block = 0; logical_block < s->numvunits; logical_block++) {
  645. block = s->VUtable[logical_block];
  646. last_block = BLOCK_NIL;
  647. /* Check for free/reserved/nil */
  648. if (block >= BLOCK_RESERVED)
  649. continue;
  650. ANAC = ANACtable[block];
  651. for (i = 0; i < s->numvunits; i++) {
  652. if (s->PUtable[block] == BLOCK_NIL)
  653. break;
  654. if (s->PUtable[block] > s->lastEUN) {
  655. printk(KERN_WARNING "INFTL: invalid prev %d, "
  656. "in virtual chain %d\n",
  657. s->PUtable[block], logical_block);
  658. s->PUtable[block] = BLOCK_NIL;
  659. }
  660. if (ANACtable[block] != ANAC) {
  661. /*
  662. * Chain must point back to itself. This is ok,
  663. * but we will need adjust the tables with this
  664. * newest block and oldest block.
  665. */
  666. s->VUtable[logical_block] = block;
  667. s->PUtable[last_block] = BLOCK_NIL;
  668. break;
  669. }
  670. ANAC--;
  671. last_block = block;
  672. block = s->PUtable[block];
  673. }
  674. if (i >= s->nb_blocks) {
  675. /*
  676. * Uhoo, infinite chain with valid ANACS!
  677. * Format whole chain...
  678. */
  679. format_chain(s, first_block);
  680. }
  681. }
  682. #ifdef CONFIG_MTD_DEBUG_VERBOSE
  683. if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
  684. INFTL_dumptables(s);
  685. if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
  686. INFTL_dumpVUchains(s);
  687. #endif
  688. /*
  689. * Third pass, format unreferenced blocks and init free block count.
  690. */
  691. s->numfreeEUNs = 0;
  692. s->LastFreeEUN = BLOCK_NIL;
  693. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 3, format unused blocks\n");
  694. for (block = s->firstEUN; block <= s->lastEUN; block++) {
  695. if (s->PUtable[block] == BLOCK_NOTEXPLORED) {
  696. printk("INFTL: unreferenced block %d, formatting it\n",
  697. block);
  698. if (INFTL_formatblock(s, block) < 0)
  699. s->PUtable[block] = BLOCK_RESERVED;
  700. else
  701. s->PUtable[block] = BLOCK_FREE;
  702. }
  703. if (s->PUtable[block] == BLOCK_FREE) {
  704. s->numfreeEUNs++;
  705. if (s->LastFreeEUN == BLOCK_NIL)
  706. s->LastFreeEUN = block;
  707. }
  708. }
  709. kfree(ANACtable);
  710. return 0;
  711. }